package com.develop.foodcooking

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.PowerManager
import android.os.PowerManager.WakeLock
import android.provider.Settings
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.PathUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.Utils
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.alee.component.skin.compat.ConstraintLayoutCompat
import org.alee.component.skin.page.WindowManager
import org.alee.component.skin.service.Config
import org.alee.component.skin.service.IOptionFactory
import org.alee.component.skin.service.IThemeSkinOption
import org.alee.component.skin.service.ThemeSkinService
import org.greenrobot.eventbus.Subscribe
import java.io.File
import java.util.*


class FoodCookApp : FoodCookBaseApp() {
    var mWakeLock: PowerManager.WakeLock? = null

    companion object {
        init {
            Config.getInstance().skinMode = Config.SkinMode.REPLACE_ALL
            Config.getInstance().performanceMode = Config.PerformanceMode.EXPERIENCE_FIRST
            ConstraintLayoutCompat.init()
            RecyclerViewCompat.init()
        }
    }


    override fun onCreate() {
        super.onCreate()
        MmkvUtils.initialize(this)
        ARouterUtils.init(this, BuildConfig.DEBUG)
        CrashReport.initCrashReport(this, "0c1b27f35b", BuildConfig.DEBUG)
        ResourceUtils.copyFileFromAssets(
            ASSETS_NIGHT_SKIN_PACK, SKIN_PACK_PATH + NIGHT_SKIN_PACK_NAME
        )
        WindowManager.getInstance().init(this, OptionFactory())
        val isNight = BuildConfig.UpdatePlatform.equals("night")
        ThemeSkinService.getInstance().switchThemeSkin(if (isNight) 1 else 0)
        setNightTheme(isNight)
        SerialportUtils.init()
        CofarSDK.register(this)
        CofarSDK.stopNative()
        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 minute = MmkvUtils.getInt("ScreenSaver")
        if (minute == 0) {
            minute = 3;
        }
        Settings.System.putInt(
            contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, (1000 * 60 * minute).toInt()
        )
        val uri: Uri = Settings.System.getUriFor(Settings.System.SCREEN_OFF_TIMEOUT)
        contentResolver.notifyChange(uri, null)
    }


    var keepScreen = false

    var timer: Timer? = null;

    var ledLock = false;


    @SuppressLint("InvalidWakeLockTag")
    @Subscribe
    fun onTuyaEvent(event: TuyaEvent) {

        TuyaUtils.uploadData()
        val pm = getSystemService(POWER_SERVICE) as PowerManager

        if (mWakeLock == null) {
            mWakeLock = pm.newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ON_AFTER_RELEASE or PowerManager.ACQUIRE_CAUSES_WAKEUP,
                "tag"
            );
            mWakeLock?.setReferenceCounted(false);
            mWakeLock?.acquire()
        }

        mWakeLock?.release()
        mWakeLock = null
        val intent = Intent()
        intent.action = "android.intent.action.SS_DEVICE_TOUCH"
        sendBroadcast(intent)
    }


    @SuppressLint("InvalidWakeLockTag")
    @Subscribe
    @Synchronized
    fun devMsg(event: DevStatusEvent) {

//        return
        TuyaUtils.uploadData()

        val devInfo = event.devInfo;
        val pm = getSystemService(POWER_SERVICE) as PowerManager
        var minute = MmkvUtils.getInt("ScreenSaver")
        if (minute == 0) {
            minute = 3;
        }

        if (devInfo.status == DevStatus.RUNNING.toInt() || devInfo.isWeightStatus) {
            if (mWakeLock == null) {
                mWakeLock = pm.newWakeLock(
                    PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ON_AFTER_RELEASE or PowerManager.ACQUIRE_CAUSES_WAKEUP,
                    "tag"
                );
                mWakeLock?.setReferenceCounted(false);
                mWakeLock?.acquire()
            }


        } else if (devInfo.status != DevStatus.RUNNING.toInt()) {
            if (mWakeLock != null) {
                mWakeLock?.release()
                mWakeLock = null
                val intent = Intent()
                intent.action = "android.intent.action.SS_DEVICE_TOUCH"
                sendBroadcast(intent)
            }

        }




        if (!ledLock) {
            ledLock = true;
            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;
        }



        println(pm.isScreenOn)

    }


}

class OptionFactory : IOptionFactory {
    override fun defaultTheme(): Int {
        return 0
    }

    override fun requireOption(theme: Int): IThemeSkinOption? {
        return when (theme) {
            1 -> NightOption()
            else -> null
        }
    }
}

class NightOption : IThemeSkinOption {
    override fun getStandardSkinPackPath(): LinkedHashSet<String> {
        val pathSet = LinkedHashSet<String>()

        pathSet.add(SKIN_PACK_PATH + NIGHT_SKIN_PACK_NAME)
        return pathSet
    }
}

const val NIGHT_SKIN_PACK_NAME = "skins/night.skin"
const val ASSETS_NIGHT_SKIN_PACK = "skins/night.skin"

/**
 * 应用存储空间根路径
 */
private val ROOT_PATH =
    Utils.getApp().getExternalFilesDir(null)?.absolutePath?.run { this + File.separator }
        ?: (PathUtils.getExternalAppDataPath() + "." + AppUtils.getAppPackageName() + File.separator)

/**
 * 皮肤包根路径
 */
val SKIN_PACK_PATH = ROOT_PATH + ".SkinPack" + File.separator