package com.develop.foodcooking

import android.os.PowerManager
import android.os.PowerManager.WakeLock
import android.view.WindowManager
import com.develop.food.base.FoodCookBaseApp
import com.develop.food.base.router.ARouterUtils
import com.develop.food.base.utils.*
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 com.tencent.bugly.crashreport.CrashReport
import org.greenrobot.eventbus.Subscribe


class FoodCookApp : FoodCookBaseApp() {

    override fun onCreate() {
        super.onCreate()
        MmkvUtils.initialize(this)
        ARouterUtils.init(this, BuildConfig.DEBUG)
        CrashReport.initCrashReport(this, "0c1b27f35b", BuildConfig.DEBUG)
        SerialportUtils.init()
        CofarSDK.register(this)

        val mContext = applicationContext
        val powerManager = mContext.getSystemService(android.content.Context.POWER_SERVICE) as PowerManager
        val  wakeLock: WakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "motionDetection:keepAwake")
         wakeLock.acquire()
        NetWorkManager.get().init(this)
        AppVerUtils.checkRecipeUpdate()


    }





    var keepScreen = false




    @Subscribe
    fun devMsg(event: DevStatusEvent){
//        return
        val devInfo = event.devInfo;
        val pm = getSystemService(POWER_SERVICE) as PowerManager


        if(devInfo.status == DevStatus.RUNNING.toInt()){
            ThreadUtils.runOnMainThread {
                TopResumedActivityHolder.getCurrentActivity()?.window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
                keepScreen=true
            }
        }else if (devInfo.status != DevStatus.RUNNING.toInt()){
            ThreadUtils.runOnMainThread {
                TopResumedActivityHolder.getCurrentActivity()?.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
                keepScreen = false
            }
        }


        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()
                }


            }
        }

        println(pm.isScreenOn)

    }



}