package com.develop.foodcooking

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.PowerManager
import android.provider.Settings
import com.develop.common.data_repo.FoodDataProvider
import com.develop.common.data_repo.net.Api
import com.develop.common.data_repo.net.converter.SerializationConverter
import com.develop.common.data_repo.net.interceptor.FoodRequestInterceptor
import com.develop.base.app.BaseApp
import com.develop.base.util.MMkvUtils
import com.develop.common.food_sdk.GlobalDevEvent
import com.develop.common.food_sdk.SerialPortUtils
import com.develop.common.utils.AppVersionUtil
import com.drake.net.convert.NetConverter
import com.drake.net.interceptor.RequestInterceptor
import com.drake.net.interfaces.NetErrorHandler
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

class FoodCookingApp : BaseApp() {
    var mWakeLock: PowerManager.WakeLock? = null
    private lateinit var pm: PowerManager
    var minute = 0
    override fun onCreate() {
        super.onCreate()
        SerialPortUtils.init()
        CofarSDK.register(this)
        CofarSDK.stopNative()

        val powerManager = getSystemService(POWER_SERVICE) as PowerManager
        val wakeLock: PowerManager.WakeLock =
            powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "motionDetection:keepAwake")
        wakeLock.acquire()
//        pm = getSystemService(POWER_SERVICE) as PowerManager
//        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)
    }

    override fun createHostUrl(): String {
        return Api.HOST
    }

    override fun createRequestInterceptor(): RequestInterceptor {
        return FoodRequestInterceptor()
    }

    override fun createConverter(): NetConverter {
        return SerializationConverter()
    }

    override fun createErrorHandle(): NetErrorHandler {
        return NetErrorHandler.DEFAULT
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        FoodDataProvider.prepareData(this)
    }


    override fun onTerminate() {
        super.onTerminate()
        SerialPortUtils.destroy()
        CofarSDK.unregister(this)
    }

    @SuppressLint("InvalidWakeLockTag")
    @Subscribe
    @Synchronized
    fun globalCoverEvent(event: DevStatusEvent) {
        val devInfo = event.devInfo;
        val pm = getSystemService(POWER_SERVICE) as PowerManager
        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)
            }
        }
        GlobalDevEvent.globalCoverEvent(event, pm)
    }
}