package com.swago.home.update

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import com.swago.baseswago.constant.UrlConstant
import com.swago.baseswago.databinding.DialogUpdateBinding
import com.swago.baseswago.dialog.BaseXDFragment
import com.swago.baseswago.util.AppContext
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.baseswago.util.SwagoInfo

/**
 *@date 2022/3/31 13:35
 *description:
 */
class UpdateDialog : BaseXDFragment<DialogUpdateBinding>() {

    init {
        setCanCancel(false)
    }

    companion object {
        fun newInstance(): UpdateDialog {
            val args = Bundle()
            val fragment = UpdateDialog()
            fragment.arguments = args
            return fragment
        }
    }

    override fun initOther() {
        UrlConstant.getConfigModel()?.let {
            dialog?.setOnKeyListener { _, keyCode, _ ->
                return@setOnKeyListener !(keyCode == KeyEvent.KEYCODE_BACK && it.force == 0)
            }

            if (it.force == 1) {
                binding.ivClose.visibility = View.GONE
            } else {
                binding.ivClose.visibility = View.VISIBLE
            }

            binding.tvDescribe.text = it.upgrade

            binding.ivClose.setOnClickListener(object : NoDoubleClickListener() {
                override fun onClick() {
                    dismissAllowingStateLoss()
                }
            })

            binding.tvUpdate.setOnClickListener(object : NoDoubleClickListener() {
                override fun onClick() {
                    if (SwagoInfo.isGooglePackage()) {
                        val intent = Intent(Intent.ACTION_VIEW)
                        intent.data =
                            Uri.parse("market://details?id=${AppContext.getContext().packageName}")
                        startActivity(intent)
                    } else {
                        val intent = Intent()
                        intent.action = Intent.ACTION_VIEW
                        intent.data = Uri.parse(it.link_url)
                        AppContext.getContext().startActivity(Intent.createChooser(intent, ""))
                    }

                    if (it.force != 1) {
                        dismissAllowingStateLoss()
                    }
                }
            })
        }
    }

    override fun initLiveData() {
    }


}