package com.qmai.android.plugin.alihotfix


import com.qmai.android.plugin.ZipUtils
import org.apache.tools.ant.taskdefs.condition.Os

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.ByteArrayOutputStream
import java.io.File

open class HotFixTask : DefaultTask() {
    var mZipPath: String = "./hotfix/zip/"
    var mUnzipPath: String = "./hotfix/unzip"
    var mZipName: String? = null
    var mConfigPath: String = "./hotfix/profiles.json"

    @TaskAction
    fun begin() {
        group = "hotfix"
        //判断是否存在加固文件
        //先判断系统
        val osList = arrayOf(Os.FAMILY_MAC, Os.FAMILY_WINDOWS, Os.FAMILY_UNIX)
        val osPath = osList.find { Os.isFamily(it) }
        println("当前系统是: $osPath")
        val downLoadUrl = when (osPath) {
            Os.FAMILY_MAC -> {
                mZipName = "SophixPatchTool_macos.zip"
                "http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_macos.zip?spm=a2c4g.11186623.0.0.48c652ffVg1sh2&file=SophixPatchTool_macos.zip"
            }
            Os.FAMILY_DOS -> {
                mZipName = "SophixPatchTool_linux.zip"
                "http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_linux.zip?spm=a2c4g.11186623.0.0.48c652ffVg1sh2&file=SophixPatchTool_linux.zip"
            }
            Os.FAMILY_WINDOWS -> {
                mZipName = "SophixPatchTool_windows.zip"
                "http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_windows.zip?spm=a2c4g.11186623.0.0.48c652ffVg1sh2&file=SophixPatchTool_windows.zip"
            }
            else -> {
                null
            }
        }
        mZipPath = StringBuilder(mZipPath).append(mZipName).toString()

        if (downLoadUrl.isNullOrEmpty()) {
            println("当前系统不支持 $osPath")
        } else {
            val zipPathDir = File(mZipPath)
            if (zipPathDir.exists()) {
                if (zipPathDir.list().isNotEmpty()) {
                } else {
                    //缺失文件
                    //下载文件
                    project.exec {
                        it.commandLine("curl", "-o", mZipPath, downLoadUrl)
                    }

                }
            } else {
                if (!zipPathDir!!.parentFile.exists()) {
                    zipPathDir.parentFile.mkdirs()
                }
                //下载文件
                project.exec {
                    it.commandLine("curl", "-o", mZipPath, downLoadUrl)
                }

            }
            val unzipParentFile = File(mUnzipPath)
            if (!unzipParentFile.exists()) {
                unzipParentFile.mkdirs()
            }
            try {
                val list = ZipUtils.unzipFile(zipPathDir!!, unzipParentFile)
                list?.forEach {
                    println("file name= ${it}")
                }

                val path = when (osPath) {
                    Os.FAMILY_MAC -> {
                        "./hotfix/unzip/SophixPatchTool_macos/SophixPatchTool.app/Contents/MacOS/SophixPatchTool"
                    }
                    Os.FAMILY_DOS -> {
                        "http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_linux.zip?spm=a2c4g.11186623.0.0.48c652ffVg1sh2&file=SophixPatchTool_linux.zip"
                    }
                    Os.FAMILY_WINDOWS -> {
                        "http://ams-hotfix-repo.oss-cn-shanghai.aliyuncs.com/SophixPatchTool_windows.zip?spm=a2c4g.11186623.0.0.48c652ffVg1sh2&file=SophixPatchTool_windows.zip"
                    }
                    else -> {
                        null
                    }
                }
                val pathFile = File(path)
                val configFile = File(mConfigPath)

                println("pathFile= ${pathFile.absolutePath}  configFile= ${configFile.absolutePath}")



                project.exec {
                    it.commandLine("chmod", "-R", "777", pathFile.absolutePath)
                }

                println("-------------------------")
                val byteOut =  ByteArrayOutputStream ()
           /*     project.exec{
                    it.executable = "java -version"
                    it.standardOutput = byteOut

                }*/

                val endResult = String(byteOut.toByteArray())
                println("end resoponse = $endResult")
               val command = "${pathFile.absolutePath} --profiles ${configFile.absolutePath}"
                project.exec {
                    /*it.workingDir = pathFile.parentFile
                 //   it.executable = "SophixPatchTool"
                    //   it.executable = "${pathFile.absolutePath} --profiles ${configFile.absolutePath}"
                    val args = ArrayList<String>()
                   args.add("SophixPatchTool")
                    args.add("--profiles")
                    args.add(configFile.absolutePath)
                    it.setArgs(args)*/
                    it.executable = "bash"
                    val args = ArrayList<String>()
                    args.add("-c")
                    args.add("${pathFile.absolutePath}")
                    args.add("--profiles")
                    args.add(configFile.absolutePath)
                    it.args = args

                }


                /* project.exec {

                     it.commandLine("${pathFile.absolutePath}","--profiles","${configFile.absolutePath}")
                 }*/


            } catch (e: Exception) {
                println("E= $e")
            }

        }
    }
}