package com.develop.setting.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.azhon.appupdate.listener.OnDownloadListener
import com.blankj.utilcode.util.AppUtils
import com.develop.base.ext.getSN
import com.develop.base.ext.updateText
import com.develop.base.util.GlobalToast
import com.develop.common.food_sdk.GlobalDevEvent
import com.develop.common.router.Screens
import com.develop.common.ui.CommonBVMActivity
import com.develop.common.utils.UpdateUtil
import com.develop.common.dialog.CancelConfirmDialog
import com.develop.common.dialog.CommonDialog
import com.develop.common.dialog.RecipeUpdateDialog
import com.develop.common.utils.AppVersionUtil
import com.develop.setting.databinding.ActivityAboutBinding
import com.develop.setting.ui.viewmodel.AboutViewModel
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevStatusEvent
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
import org.greenrobot.eventbus.Subscribe
import java.io.File

@Route(path = Screens.Setting.ABOUT)
class AboutActivity : CommonBVMActivity<ActivityAboutBinding, AboutViewModel>() {
    override fun createViewBinding(inflater: LayoutInflater): ActivityAboutBinding {
        return ActivityAboutBinding.inflate(layoutInflater)
    }

    override fun createViewModel(): AboutViewModel {
        return getViewModel(AboutViewModel::class.java)
    }

    private var updateApkUrl = ""

    private var weightAlignCount = 0;

    private val cancelConfirmDialog by lazy {
        CancelConfirmDialog()
    }

    private val commonDialog by lazy {
        CommonDialog()
    }


    private val recipeUpdateDialog by lazy {
        RecipeUpdateDialog()
    }

    @Subscribe
    fun onDevStateEvent(event: DevStatusEvent) {
        GlobalDevEvent.globalWeightEvent(event)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.tvTitle.updateText(getString(com.develop.common.R.string.About))
        binding.ivClose.setOnClickListener {
            finish()
        }

        binding.devSn.text = getSN()

        recipeUpdateDialog.onDialogClickListener =
            object : RecipeUpdateDialog.OnDialogClickListener {
                override fun onConfirm() {

                }

                override fun onCancel() {

                }

            }

        //点击食谱更新
        binding.updateRecipes.setOnClickListener {
            AppVersionUtil.checkRecipeUpdate(true)
        }

        binding.ivLogo.setOnClickListener {
            weightAlignCount++
            if (weightAlignCount >= 7) {
                weightAlignCount = 0
                cancelConfirmDialog.title = getString(com.develop.common.R.string.change_lang_tips)
                cancelConfirmDialog.showCancel = false
                cancelConfirmDialog.onDialogClickListener =
                    object : CancelConfirmDialog.OnDialogClickListener {
                        override fun onConfirm() {

                        }

                        override fun onCancel() {
                        }

                    }
                if (CofarSDK.devInfo().runningStatus == DevStatus.RUNNING) {
                    cancelConfirmDialog.showDialog(supportFragmentManager, "confirm")
                    return@setOnClickListener
                } else {
                    //进入称重标定模式
                    CofarSDK.startWeightAlign()
                    GlobalDevEvent.weight = true
                }
            }

        }

        commonDialog.msg = getString(com.develop.common.R.string.update_msg)
        commonDialog.hasOKBtn = false
        cancelConfirmDialog.title = getString(com.develop.common.R.string.update_title)
        cancelConfirmDialog.onDialogClickListener =
            object : CancelConfirmDialog.OnDialogClickListener {
                override fun onConfirm() {
                    commonDialog.showDialog(supportFragmentManager, "commonDialog")
                    UpdateUtil.updateApp(
                        this@AboutActivity,
                        updateApkUrl,
                        object : OnDownloadListener {
                            override fun cancel() {
                                runOnUiThread {
                                    commonDialog.removeSelf()
                                }
                            }

                            override fun done(apk: File) {
                                GlobalToast.showToast(getString(com.develop.common.R.string.finish_download))
                                runOnUiThread {
                                    commonDialog.removeSelf()
                                }
                            }

                            override fun downloading(max: Int, progress: Int) {
                                runOnUiThread {
                                    commonDialog.updateProgress(
                                        "${
                                            String.format(
                                                "%.0f",
                                                ((progress.toFloat() / max.toFloat()) * 100f)
                                            )
                                        }%"
                                    )
                                }
                            }

                            override fun error(e: Throwable) {
                                GlobalToast.showToast(getString(com.develop.common.R.string.download_fail))
                                runOnUiThread {
                                    commonDialog.removeSelf()
                                }
                            }

                            override fun start() {
                                GlobalToast.showToast(getString(com.develop.common.R.string.start_download))
                            }

                        })
                }

                override fun onCancel() {

                }
            }

        binding.tvUpdate.setOnClickListener {
            cancelConfirmDialog.showDialog(supportFragmentManager, "cancelConfirmDialog")
        }

        binding.tvAppName.text = AppUtils.getAppName()
        binding.tvAppVersion.text = "v${AppUtils.getAppVersionName()}"

        viewModel.devInfoLiveData.observe(this) {
            updateApkUrl = it?.apkUrl ?: ""
            if (it?.apkUpdate == true) {
                binding.tvUpdate.visibility = View.VISIBLE
            } else {
                binding.tvUpdate.visibility = View.INVISIBLE
            }
        }

    }

    override fun onResume() {
        super.onResume()
        viewModel.getDevInfo()
    }

    override fun onDestroy() {
        super.onDestroy()
        commonDialog.removeSelf()
        cancelConfirmDialog.removeSelf()
    }


}