package com.doverfuelingsolutions.issp.utils.download.dialog

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.ViewUtils
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.utils.DFSPaths
import com.doverfuelingsolutions.issp.utils.DFSToastUtil
import com.doverfuelingsolutions.issp.utils.download.DownloadCallback
import com.doverfuelingsolutions.issp.utils.download.DownloadUtil
import com.doverfuelingsolutions.issp.utils.download.entity.PackageVersion
import com.moos.library.CircleProgressView

class VersionDialog(val act: Activity,val version: PackageVersion):AlertDialog(act) {
    private var cpv_progress:CircleProgressView? = null
    private var tv_new_version:TextView? = null
    private var tv_old_version:TextView? = null

    private var btn_update:Button? = null
    private var btn_cancel:Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.version)
        setCancelable(false)
        initView()
    }

    private fun initView() {
        cpv_progress = findViewById(R.id.cpv_progress)

        tv_new_version = findViewById(R.id.tv_new_version)
        tv_new_version?.text = "新版本：" + version.VersionName

        tv_old_version = findViewById(R.id.tv_old_version)
        tv_old_version?.text = "当前版本：" + AppUtils.getAppVersionName()

        btn_update = findViewById(R.id.btn_update)
        btn_update?.setOnClickListener {
            loading()
            Thread{
                DownloadUtil.downloadApp("${version.Name}.apk", object : DownloadCallback {
                    override fun onLoading(loadLen: Int, contentLen: Int) {
                        ViewUtils.runOnUiThread {
                            cpv_progress?.progress = (loadLen.toFloat() / contentLen.toFloat()) * 100
                        }
                    }

                    override fun onDownload(isSuccess: Boolean) {
                        ViewUtils.runOnUiThread {
                            if (isSuccess) {
                                act.stopLockTask()
                                AppUtils.installApp("${DFSPaths.dowloadApp}/${version.Name}.apk")
                            } else {
                                DFSToastUtil.info("下载安装包失败")
                            }
                            loaded()
                        }
                    }
                })
            }.start()

        }

        btn_cancel = findViewById(R.id.btn_cancel)
        btn_cancel?.setOnClickListener {
            dismiss()
        }

        val attributes = window?.attributes
        attributes?.width = 700
        attributes?.height = 1400
        attributes?.y = 400
        window?.attributes = attributes
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    private fun loading() {
        btn_update?.alpha = 0.5F
        btn_update?.isClickable = false

        btn_cancel?.alpha = 0.5F
        btn_cancel?.isClickable = false
    }

    private fun loaded() {
        btn_update?.alpha = 1F
        btn_update?.isClickable = true

        btn_cancel?.alpha = 1F
        btn_cancel?.isClickable = true
    }
}