package com.develop.common.food_sdk

import android.content.Context.POWER_SERVICE
import android.content.res.Resources
import android.os.PowerManager
import android.util.Log
import android.view.WindowManager
import androidx.fragment.app.FragmentManager
import com.develop.base.ext.globalApp
import com.develop.base.ext.globalRes
import com.develop.base.ext.toJson
import com.develop.base.util.ThreadUtils
import com.develop.base.util.TopResumedAtyHolder
import com.develop.common.R
import com.develop.common.dialog.CancelConfirmDialog
import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevPromptEvent
import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevStatusEvent
import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK

/**
 * 全局处理烹饪设备回调的事件
 */
object GlobalDevEvent {

    var keepScreen = false
    var weight = false

    private val weightAlignDialog by lazy {
        CancelConfirmDialog()
    }

    private val devMsgDialog by lazy {
        CancelConfirmDialog()
    }

    /**处理设备合盖问题*/
    var ledLock = false;
    fun globalCoverEvent(event: DevStatusEvent, pm: PowerManager) {
        if (!ledLock) {
            ledLock = true
            val devInfo = event.devInfo
            if (!pm.isScreenOn) {

                //合盖
                if (devInfo.potCloverStatus.toInt() == 0) {
                    CofarSDK.flashWhiteRedLed()
                } else {
                    CofarSDK.switchWhiteLed('0');
                    CofarSDK.flashRedLed()
                }

            } else {
                if (devInfo.status == DevStatus.STOP.toInt()) {

                    if (devInfo.potStatus.toInt() == 0) {
                        if (devInfo.potCloverStatus.toInt() == 0) {
                            CofarSDK.flashWhiteRedLed()
                        } else {
                            CofarSDK.switchWhiteLed('0');
                            CofarSDK.flashRedLed()
                        }
                    } else {
                        CofarSDK.flashWhiteRedLed();
                    }

                }

                if (devInfo.status == DevStatus.RUNNING.toInt()) {

                    if (devInfo.temp < 60) {
                        CofarSDK.switchRedLed('0');
                        CofarSDK.switchWhiteLed('1');
                    } else {
                        CofarSDK.switchRedLed('1');
                        CofarSDK.switchWhiteLed('0');
                    }

                }

                if (devInfo.status == DevStatus.PAUSE.toInt()) {

                    //判断盖子是否打开
                    //合盖
                    if (devInfo.potCloverStatus.toInt() == 1) {
                        CofarSDK.switchWhiteLed('0');
                        CofarSDK.flashRedLed()
                    } else {
                        CofarSDK.flashWhiteRedLed()
                    }


                }
                ledLock = false
            }
        }
    }


    /**处理设备称重问题*/
    fun globalWeightEvent(event: DevStatusEvent) {
        TopResumedAtyHolder.getCurrentActivity()!!.apply {
            if(!weight){
                return;
            }
            ThreadUtils.runOnMainThread {



                if(event.devInfo.weightAlignStatus == (0).toByte()){ //正在标定不是
                    weightAlignDialog.dialog?.hide()
                    weightAlignDialog.showCancel = false;
                    weightAlignDialog.confirmStr = getString(R.string.exit_weight_align)

                    if (!weightAlignDialog.isAdded){
                        weightAlignDialog.title = getString(R.string.weight_aligning)
                        weightAlignDialog.showDialog(supportFragmentManager,"weight_align_dialog")
                    } else  {
                        if(event.devInfo.weightPol == (1).toByte()){
                            weightAlignDialog.binding.tvTitle.text = getString(R.string.weight_one_kg_tips)
                        }

                        if(event.devInfo.weightPol == (2).toByte()){
                            weightAlignDialog.binding.tvTitle.text = getString(R.string.weight_two_kg_tips)
                        }

                        weightAlignDialog.dialog?.show()
                    }

                    weightAlignDialog.onDialogClickListener = object :CancelConfirmDialog.OnDialogClickListener{

                        override fun onConfirm() {
                            exitWeightAlign()
                            weight = false;
                        }

                        override fun onCancel() {
                            exitWeightAlign()
                            weight = false
                        }

                    }
                }

                if(event.devInfo.weightAlignStatus == (2).toByte()){ //正在标定不是
//                CofarSDK.unregister(this)
                    weightAlignDialog.dialog?.hide()
                    weightAlignDialog.showCancel = false;
                    weightAlignDialog.confirmStr = getString(R.string.exit_weight_align)

                    if (!weightAlignDialog.isAdded){
                        weightAlignDialog.title = getString(R.string.weight_align_success)
                        weightAlignDialog.showDialog(supportFragmentManager,"weight_align_dialog")
                    } else  {
                        weightAlignDialog.binding.tvTitle.text =  getString(R.string.weight_align_success)
                        weightAlignDialog.dialog?.show()
                    }

                    weightAlignDialog.onDialogClickListener = object :CancelConfirmDialog.OnDialogClickListener{

                        override fun onConfirm() {
                            exitWeightAlign()
                        }

                        override fun onCancel() {
                            exitWeightAlign()
                        }

                    }

                    exitWeightAlign()
                }

            }
        }
    }

    /**处理设备提示弹窗*/
    fun globalDevMsgEvent(
        event: DevPromptEvent,
        fragmentManager: FragmentManager,
        resources: Resources
    ) {
        ThreadUtils.runOnMainThread {
            devMsgDialog.onDialogClickListener =
                object : CancelConfirmDialog.OnDialogClickListener {
                    override fun onConfirm() {
                        event.confirm?.callback()
                    }

                    override fun onCancel() {
                        event.confirm?.callback()
                    }

                }
            devMsgDialog.apply {
                showCancel = event.isShowCancelBtn
                showConfirm = event.isShowConfirmBtn
                cancelStr = resources.getString(
                    resources.getIdentifier(
                        event.cancelBtnText, "string", globalApp().packageName
                    )
                )
                confirmStr = resources.getString(
                    resources.getIdentifier(
                        event.confirmButtonText, "string", globalApp().packageName
                    )
                )
                title = resources.getString(
                    resources.getIdentifier(
                        event.msg, "string", globalApp().packageName
                    )
                )
            }
            devMsgDialog.showDialog(fragmentManager, "devMsgDialog")
        }
    }


    //退出称重校准
    private fun exitWeightAlign() {
        CofarSDK.stoptWeightAlign()
        weight = false
    }


    fun dismissDialog() {
        weightAlignDialog.removeSelf()
        devMsgDialog.removeSelf()
    }

}