package com.casic.br.fragment.rangehood

import android.os.Handler
import android.util.Log
import com.alibaba.fastjson.JSONObject
import com.casic.br.R
import com.casic.br.extensions.diffDate
import com.casic.br.utils.LocaleConstant
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.pengxh.kt.lite.utils.WeakReferenceHandler
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.home.sdk.bean.HomeBean
import com.tuya.smart.home.sdk.callback.ITuyaHomeResultCallback
import com.tuya.smart.sdk.api.ITuyaDevice
import com.tuya.smart.sdk.bean.DeviceBean
import kotlinx.android.synthetic.main.fragment_device_rangehood_status.*

class StatusPageFragment(private val deviceInstance: ITuyaDevice) : KotlinBaseFragment() {

    companion object {
        lateinit var weakReference: WeakReferenceHandler
    }

    private val kTag = "StatusPageFragment"
    private var isDeviceWorking = false

    override fun initLayoutView(): Int = R.layout.fragment_device_rangehood_status

    override fun setupTopBarLayout() {

    }

    override fun initData() {
        weakReference = WeakReferenceHandler(callback)

        val homeId = SaveKeyValues.getValue(LocaleConstant.HOME_ID, "") as String
        if (homeId == "") {
            return
        }
        TuyaHomeSdk.newHomeInstance(homeId.toLong())
            .getHomeDetail(object : ITuyaHomeResultCallback {
                override fun onSuccess(bean: HomeBean) {
                    val deviceList: List<DeviceBean> = bean.deviceList
                    deviceList.forEach {
                        if (it.deviceCategory == "yyj") {
                            deviceUsedTimeView.text =
                                String.format("${it.time.toString().diffDate()}天")
                        }
                        return@forEach
                    }
                }

                override fun onError(errorCode: String, errorMsg: String) {
                    Log.d(kTag, "onError: $errorMsg")
                }
            })
    }

    override fun onResume() {
        super.onResume()
        //每次获取焦点都查询设备的开关机状态
        deviceStateView.text = if (!isDeviceWorking) {
            "已关机"
        } else {
            "工作中"
        }
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {

    }

    private val callback = Handler.Callback { message ->
        if (message.what == 2022092302) {
            val parseObject = JSONObject.parseObject(message.obj as String)
            parseObject.keys.forEach {
                when (it) {
                    "1" -> {//开关机
                        isDeviceWorking = parseObject[it] as Boolean
                    }
                    "11" -> {//设备状态，[off, low, high, strong, air, fangdaoguan, wash, delay, xunhang]
                        val status = parseObject[it] as String
                        deviceStateView.text = if (status == "off") {
                            "已关机"
                        } else {
                            "工作中"
                        }
                    }
                    "12" -> {//累计工作时间
                        workingTimeView.text = if (isDeviceWorking) {
                            "${parseObject[it]}分"
                        } else {
                            "0分"
                        }
                    }
                    "106" -> {//烟灶联动
                        val isLinkage = parseObject[it] as Boolean
                        linkageView.text = if (isLinkage) {
                            "开"
                        } else {
                            "关"
                        }
                    }
                    "107" -> {//倒油提醒
                        val isPourOil = parseObject[it] as Boolean
                        pourOilView.text = if (isPourOil) {
                            "开"
                        } else {
                            "关"
                        }
                    }
                }
            }
        }
        true
    }

    override fun onDestroy() {
        super.onDestroy()
        deviceInstance.unRegisterDevListener()
        deviceInstance.onDestroy()
    }
}