package com.yidian.shenghuoquan.newscontent.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.os.Bundle
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.yidian.common.widget.YdMaxRecyclerView
import com.yidian.nightmode.widget.YdImageView
import com.yidian.nightmode.widget.YdTextView
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.adapter.UpdateVersionDesAdapter
import com.yidian.shenghuoquan.newscontent.bean.IUpdateDialogCallback
import com.yidian.yac.pm.appupdate.AppUpdateEchoData
import com.yidian.yac.pm.appupdate.AppUpdateInfo


class UpdateVersionDialog(private val activity: Activity, themeResId: Int, private val updateDialogCallback :IUpdateDialogCallback): Dialog(activity, themeResId) {
    private lateinit var ivUpdateClose: YdImageView
    private lateinit var tvNewVersion: YdTextView
    private lateinit var tvDown: YdTextView
    private lateinit var rvDes: YdMaxRecyclerView
    private lateinit var updateInfo: AppUpdateInfo<AppUpdateEchoData>
    private val adapter = UpdateVersionDesAdapter()

    init {
        setDialog()
    }

    private fun setDialog(){
        this.setCanceledOnTouchOutside(false)
        this.setContentView(R.layout.update_version_dialog)
        this.setOwnerActivity(activity)
        ivUpdateClose = findViewById(R.id.iv_updateClose)
        tvNewVersion = findViewById(R.id.tv_newVersion)
        tvDown = findViewById(R.id.tv_down)
        rvDes = findViewById(R.id.rv_des)
        rvDes.layoutManager = LinearLayoutManager(activity)
        rvDes.adapter = adapter
        setClickListener()
    }

    fun setData(updateInfo: AppUpdateInfo<AppUpdateEchoData>){
        this.updateInfo = updateInfo
        val forceUpdate = updateInfo.echo_data?.force_update
        if(forceUpdate == true){
            ivUpdateClose.visibility = View.GONE
        }else{
            ivUpdateClose.visibility = View.VISIBLE
        }
        val resultInfo = updateInfo.result?.get(0)
        val versionShow = resultInfo?.package_version
        tvNewVersion.text = "发现新版本V$versionShow"
        var note = resultInfo?.note
        var desList: ArrayList<String>? = null
        if(!note.isNullOrEmpty()){
            if(note.endsWith(";") || note.endsWith("；")){
                note = note.substring(0, note.length - 1)
            }
            when {
                note.contains(";") -> {
                    desList = note.split(";") as ArrayList<String>
                }
                note.contains("；") -> {
                    desList = note.split("；") as ArrayList<String>
                }
                else -> {
                    desList = ArrayList()
                    desList.add(note)
                }
            }
        }

        if(desList != null){
            adapter.update(desList)
        }

    }

    private fun setClickListener(){
        ivUpdateClose.setOnClickListener {
            dismiss()
        }

        tvDown.setOnClickListener {
            tvDown.isEnabled = false
            tvDown.setBackgroundResource(R.drawable.bg_gray_radio)
            tvDown.text = "下载中..."
            updateDialogCallback.downloadCallback(updateInfo)
        }

        setOnKeyListener { _, keyCode, event -> keyCode == KeyEvent.KEYCODE_BACK && event?.repeatCount == 0 }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val params: WindowManager.LayoutParams? = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.attributes?.gravity = Gravity.CENTER
    }
}