package com.casic.br.fragment.fireplace

import android.os.Handler
import com.alibaba.fastjson.JSONObject
import com.casic.br.R
import com.casic.br.extensions.createCommand
import com.casic.br.utils.CommandManager
import com.casic.br.utils.SliderCallback
import com.pengxh.kt.lite.base.KotlinBaseFragment
import com.pengxh.kt.lite.utils.WeakReferenceHandler
import com.qmuiteam.qmui.widget.QMUISlider
import com.tuya.smart.sdk.api.ITuyaDevice
import kotlinx.android.synthetic.main.fragment_device_fireplace_control.*
import kotlin.math.abs

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

    companion object {
        lateinit var weakReference: WeakReferenceHandler
    }

    private val kTag = "ControlPageFragment"
    private val showerMinTemp = 35
    private var showerMaxTemp = 60
    private var showerCurrentProgress = 0
    private val heaterMinTemp = 35
    private var heaterMaxTemp = 60
    private var heaterCurrentProgress = 0
    private var isDeviceWorking = false

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

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {

    }

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

        showerTempSlider.tickCount = abs(showerMaxTemp - showerMinTemp)
        showerTempSlider.currentProgress = showerCurrentProgress
        showerTempView.text = String.format("${showerMinTemp + showerCurrentProgress}")
        showerMinTempView.text = showerMinTemp.toString()
        showerMaxTempView.text = showerMaxTemp.toString()

        heaterTempSlider.tickCount = abs(heaterMaxTemp - heaterMinTemp)
        heaterTempSlider.currentProgress = heaterCurrentProgress
        heaterTempView.text = String.format("${heaterMinTemp + heaterCurrentProgress}")
        heaterMinTempView.text = heaterMinTemp.toString()
        heaterMaxTempView.text = heaterMaxTemp.toString()
    }

    override fun initEvent() {
        showerTempSlider.setCallback(object : SliderCallback() {
            override fun onProgressChange(
                slider: QMUISlider?, progress: Int, tickCount: Int, fromUser: Boolean
            ) {
                showerCurrentProgress = progress
                showerTempView.text = String.format("${showerMinTemp + progress}")
            }

            override fun onStopMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {
                //温度设置
                CommandManager.sendTuyaDp(
                    deviceInstance, "3".createCommand(showerMinTemp + progress)
                )
            }
        })

        heaterTempSlider.setCallback(object : SliderCallback() {
            override fun onProgressChange(
                slider: QMUISlider?, progress: Int, tickCount: Int, fromUser: Boolean
            ) {
                heaterCurrentProgress = progress
                heaterTempView.text = String.format("${heaterMinTemp + progress}")
            }

            override fun onStopMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {
                //温度设置
                CommandManager.sendTuyaDp(
                    deviceInstance, "5".createCommand(heaterMinTemp + progress)
                )
            }
        })
    }

    private val callback = Handler.Callback { message ->
        if (message.what == 2022092601) {
            val parseObject = JSONObject.parseObject(message.obj as String)
            parseObject.keys.forEach {
                when (it) {
                    "1" -> {//开关机
                        isDeviceWorking = parseObject[it] as Boolean
                        turnOffRadioButton.isChecked = isDeviceWorking
                    }
                    "2" -> {
                        when (parseObject[it] as String) {
                            "comfortable" -> comfortableRadioButton.isChecked = true
                            "eco" -> ecoRadioButton.isChecked = true
                            "summer" -> summerRadioButton.isChecked = true
                            else -> {
                                comfortableRadioButton.isChecked = false
                                ecoRadioButton.isChecked = false
                                summerRadioButton.isChecked = false
                            }
                        }
                    }
                    "3" -> {//卫浴温度设置

                    }
                    "5" -> {//供暖温度设置

                    }
                    "14" -> {
                        totalGasUsedView.text = String.format(
                            "${parseObject[it] as String}${requireContext().resources.getString(R.string.volume_unit)}"
                        )
                    }
                }
            }
        }
        true
    }
}