package com.casic.br.fragment.rangehood

import android.graphics.Color
import android.os.Handler
import com.alibaba.fastjson.JSONObject
import com.casic.br.R
import com.casic.br.extensions.createCommand
import com.casic.br.extensions.disableLayout
import com.casic.br.extensions.enableLayout
import com.casic.br.utils.CommandManager
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.utils.WeakReferenceHandler
import com.pengxh.kt.lite.widget.dialog.BottomActionSheet
import com.tuya.smart.sdk.api.ITuyaDevice
import kotlinx.android.synthetic.main.fragment_device_rangehood_control.*

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

    companion object {
        lateinit var weakReference: WeakReferenceHandler
    }

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

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

    override fun setupTopBarLayout() {

    }

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

    override fun observeRequestState() {

    }

    override fun initEvent() {
        //开关机
        turnOffRadioButton.setOnClickListener {
            if (turnOffRadioButton.isChecked) {
                CommandManager.sendTuyaDp(deviceInstance, "1".createCommand(true))
            } else {
                CommandManager.sendTuyaDp(deviceInstance, "1".createCommand(false))
            }
        }

        speedRadioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.lowSpeedRadioButton -> {
                    CommandManager.sendTuyaDp(deviceInstance, "10".createCommand("low"))
                }
                R.id.mediumSpeedRadioButton -> {
                    CommandManager.sendTuyaDp(deviceInstance, "10".createCommand("high"))
                }
                R.id.highSpeedRadioButton -> {
                    CommandManager.sendTuyaDp(deviceInstance, "10".createCommand("strong"))
                }
            }
        }

        //清洁
        cleanSwitch.setOnCheckedChangeListener { _, isChecked ->
            CommandManager.sendTuyaDp(deviceInstance, "101".createCommand(isChecked))
        }

        //照明
        lightSwitch.setOnCheckedChangeListener { _, isChecked ->
            CommandManager.sendTuyaDp(deviceInstance, "4".createCommand(isChecked))
        }

        //巡航
        cruiseSwitch.setOnCheckedChangeListener { _, isChecked ->
            CommandManager.sendTuyaDp(deviceInstance, "104".createCommand(isChecked))
        }

        //延时开关
        delayTurnOffSwitch.setOnCheckedChangeListener { _, isChecked ->
            CommandManager.sendTuyaDp(deviceInstance, "2".createCommand(isChecked))
        }

        //假日通风
        ventilateSwitch.setOnCheckedChangeListener { _, isChecked ->
            CommandManager.sendTuyaDp(deviceInstance, "102".createCommand(isChecked))
        }

        //假日通风时长
        val timeArray = arrayListOf(
            "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120"
        )
        ventilateTimeLayout.setOnClickListener {
            BottomActionSheet.Builder()
                .setContext(requireContext())
                .setActionItemTitle(timeArray)
                .setItemTextColor(Color.BLUE)
                .setOnActionSheetListener(object : BottomActionSheet.OnActionSheetListener {
                    override fun onActionItemClick(position: Int) {
                        CommandManager.sendTuyaDp(
                            deviceInstance, "103".createCommand(timeArray[position].toInt())
                        )
                    }
                }).build().show()
        }

        //防倒灌
        antiPourDownSwitch.setOnCheckedChangeListener { _, isChecked ->
            CommandManager.sendTuyaDp(deviceInstance, "108".createCommand(isChecked))
        }
    }

    private val callback = Handler.Callback { message ->
        if (message.what == 2022092301) {
            val parseObject = JSONObject.parseObject(message.obj as String)
            parseObject.keys.forEach {
                when (it) {
                    "1" -> {//开关机
                        isDeviceWorking = parseObject[it] as Boolean
                        turnOffRadioButton.isChecked = isDeviceWorking
                        if (isDeviceWorking) {
                            /**
                             * 已开机
                             * */
                            deviceStateButton.text = "关机"

                            modeLayout.enableLayout(
                                lowSpeedRadioButton,
                                highSpeedRadioButton,
                                maxSpeedRadioButton
                            )
                            cleanLayout.enableLayout(cleanSwitch)
                            lightLayout.enableLayout(lightSwitch)
                            cruiseLayout.enableLayout(cruiseSwitch)
                            delayLayout.enableLayout(delayTurnOffSwitch)
                            ventilateLayout.disableLayout(ventilateSwitch)
                            ventilateTimeLayout.disableLayout(ventilateTimeView)
                            antiPourDownLayout.disableLayout(antiPourDownSwitch)
                        } else {
                            /**
                             * 已关机
                             * */
                            deviceStateButton.text = "开机"

                            modeLayout.enableLayout(
                                lowSpeedRadioButton,
                                highSpeedRadioButton,
                                maxSpeedRadioButton
                            )
                            cleanLayout.enableLayout(cleanSwitch)
                            lightLayout.enableLayout(lightSwitch)
                            cruiseLayout.enableLayout(cruiseSwitch)
                            delayLayout.disableLayout(delayTurnOffSwitch)
                            ventilateLayout.enableLayout(ventilateSwitch)
                            if (ventilateSwitch.isChecked) {
                                ventilateTimeLayout.enableLayout(ventilateTimeView)
                            } else {
                                ventilateTimeLayout.disableLayout(ventilateTimeView)
                            }
                            antiPourDownLayout.enableLayout(antiPourDownSwitch)
                        }
                    }
                    "2" -> {//延时关机状态,{"105":90,"11":"delay","2":true}｜{"11":"low","2":false}
                        val isDelay = parseObject[it] as Boolean
                        delayTurnOffSwitch.isChecked = isDelay
                        if (isDeviceWorking) {
                            delayLayout.enableLayout(delayTurnOffSwitch)
                        } else {
                            delayLayout.disableLayout(delayTurnOffSwitch)
                        }
                    }
                    "4" -> {//照明
                        val isLighting = parseObject[it] as Boolean
                        lightSwitch.isChecked = isLighting
                    }
                    "10" -> {//风速
                        when (parseObject[it] as String) {
                            "low" -> lowSpeedRadioButton.isChecked = true
                            "high" -> highSpeedRadioButton.isChecked = true
                            "strong" -> maxSpeedRadioButton.isChecked = true
                            else -> {
                                lowSpeedRadioButton.isChecked = false
                                highSpeedRadioButton.isChecked = false
                                maxSpeedRadioButton.isChecked = false
                            }
                        }
                    }
                    "101" -> {//清洁,{"10":"off","101":true,"11":"wash"}｜{"101":false,"11":"off"}
                        val isCleaning = parseObject[it] as Boolean
                        cleanSwitch.isChecked = isCleaning
                        if (isCleaning) {
                            modeLayout.disableLayout(
                                lowSpeedRadioButton, highSpeedRadioButton, maxSpeedRadioButton
                            )
                            lightLayout.enableLayout(lightSwitch)
                            cruiseLayout.disableLayout(cruiseSwitch)
                            delayLayout.disableLayout(delayTurnOffSwitch)
                            ventilateLayout.disableLayout(ventilateSwitch)
                            ventilateTimeLayout.disableLayout(ventilateTimeView)
                            antiPourDownLayout.disableLayout(antiPourDownSwitch)
                        } else {
                            modeLayout.enableLayout(
                                lowSpeedRadioButton,
                                highSpeedRadioButton,
                                maxSpeedRadioButton
                            )
                            lightLayout.enableLayout(lightSwitch)
                            cruiseLayout.enableLayout(cruiseSwitch)
                            delayLayout.enableLayout(delayTurnOffSwitch)
                            ventilateLayout.enableLayout(ventilateSwitch)
                            if (ventilateSwitch.isChecked) {
                                ventilateTimeLayout.enableLayout(ventilateTimeView)
                            } else {
                                ventilateTimeLayout.disableLayout(ventilateTimeView)
                            }
                            antiPourDownLayout.enableLayout(antiPourDownSwitch)
                        }
                    }
                    "102" -> {//假日通风
                        val isVentilating = parseObject[it] as Boolean
                        ventilateSwitch.isChecked = isVentilating
                        if (isVentilating) {
                            if (ventilateSwitch.isChecked) {
                                ventilateTimeLayout.enableLayout(ventilateTimeView)
                            } else {
                                ventilateTimeLayout.disableLayout(ventilateTimeView)
                            }
                        }
                    }
                    "103" -> {//通风时长
                        ventilateTimeView.text =
                            String.format("${parseObject[it].toString()}min")
                    }
                    "104" -> {//巡航
                        val isCruising = parseObject[it] as Boolean
                        cruiseSwitch.isChecked = isCruising
                        if (isCruising) {
                            if (isDeviceWorking) {
                                modeLayout.enableLayout(
                                    lowSpeedRadioButton,
                                    highSpeedRadioButton,
                                    maxSpeedRadioButton
                                )
                                cleanLayout.enableLayout(cleanSwitch)
                                lightLayout.enableLayout(lightSwitch)
                                delayLayout.enableLayout(delayTurnOffSwitch)
                                ventilateLayout.disableLayout(ventilateSwitch)
                                ventilateTimeLayout.disableLayout(ventilateTimeView)
                                antiPourDownLayout.disableLayout(antiPourDownSwitch)
                            } else {
                                modeLayout.enableLayout(
                                    lowSpeedRadioButton,
                                    highSpeedRadioButton,
                                    maxSpeedRadioButton
                                )
                                cleanLayout.enableLayout(cleanSwitch)
                                lightLayout.enableLayout(lightSwitch)
                                delayLayout.enableLayout(delayTurnOffSwitch)
                                ventilateLayout.enableLayout(ventilateSwitch)
                                if (ventilateSwitch.isChecked) {
                                    ventilateTimeLayout.enableLayout(ventilateTimeView)
                                } else {
                                    ventilateTimeLayout.disableLayout(ventilateTimeView)
                                }
                                antiPourDownLayout.enableLayout(antiPourDownSwitch)
                            }
                        } else {
                            modeLayout.enableLayout(
                                lowSpeedRadioButton,
                                highSpeedRadioButton,
                                maxSpeedRadioButton
                            )
                            cleanLayout.enableLayout(cleanSwitch)
                            lightLayout.enableLayout(lightSwitch)
                            delayLayout.enableLayout(delayTurnOffSwitch)
                            ventilateLayout.disableLayout(ventilateSwitch)
                            ventilateTimeLayout.disableLayout(ventilateTimeView)
                            antiPourDownLayout.disableLayout(antiPourDownSwitch)
                        }
                    }
                    "108" -> {//防倒灌
                        val isAntiPourDown = parseObject[it] as Boolean
                        antiPourDownSwitch.isChecked = isAntiPourDown
                    }
                }
            }
        }
        true
    }

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