package com.lukouguoji.module_base.update

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.google.android.material.progressindicator.LinearProgressIndicator
import com.lukouguoji.module_base.R
import com.lukouguoji.module_base.bean.AppUpdateResponseInfo
import com.lukouguoji.module_base.ktx.loge


class UpdateDialog(val activity: Activity, val updateInfo: AppUpdateResponseInfo, val uploadUtil: UploadUtil) : Dialog(activity, R.style.updateDialogTheme) {

    private lateinit var tvContent: TextView
    private lateinit var tvTitle: TextView
    private lateinit var progressBar: LinearProgressIndicator
    private lateinit var tvDownload: TextView
    private val handler = Handler(Looper.getMainLooper())
    private var isDownloadSuccess = false
    private var isDownloading = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_update)
        setCanceledOnTouchOutside(false)
        if(activity.resources.displayMetrics.widthPixels < activity.resources.displayMetrics.heightPixels){
                    window?.attributes?.width = (activity.resources.displayMetrics.widthPixels * 0.7F).toInt()
        }
        tvTitle = findViewById(R.id.tv_title)
        tvContent = findViewById(R.id.tv_content)
        progressBar = findViewById(R.id.progressBar)
        tvDownload = findViewById(R.id.tv_download)

        tvTitle.text = "发现新版本 V${updateInfo.versionNo}"
        tvContent.text = updateInfo.updateNotes

        tvDownload.setOnClickListener {
            download()
        }
    }

    private fun  download(){
        if(isDownloadSuccess){
            uploadUtil.installApk(activity)
        }else{
            if(!isDownloading){
                isDownloading = true
                tvDownload.isEnabled = false
                progressBar.visibility = View.VISIBLE
                uploadUtil.startDownload(activity, updateInfo,{
                    progressBar.progress = it
                    if(it == 100){
                        isDownloadSuccess = true
                        isDownloading = false
                        tvDownload.isEnabled = true
                    }
                },{
                    isDownloadSuccess = false
                    isDownloading = false
                    tvDownload.isEnabled = true
                    Toast.makeText(activity.applicationContext, it, Toast.LENGTH_SHORT).show()
                })
            }
        }
    }

    override fun dismiss() {
        super.dismiss()
        handler.removeCallbacksAndMessages(null)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            loge("按了 back 键")
        }
        return true
    }

}