package com.casic.br.fragment.waterheater

import android.os.Handler
import com.alibaba.fastjson.JSONObject
import com.casic.br.R
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_waterheater_control.*
import kotlin.math.abs

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

    companion object {
        lateinit var weakReference: WeakReferenceHandler
    }

    private val kTag = "ControlPageFragment"
    private val minTemp = 36
    private var maxTemp = 60
    private var currentProgress = 0
    private val timeScope = intArrayOf(30, 60, 120, 180)
    private var currentTimeProgress = 0

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

    override fun setupTopBarLayout() {

    }

    override fun initData() {
        weakReference = WeakReferenceHandler(callback)
        //默认选中
        modeRadioGroup.check(R.id.kitchenRadioButton)
        setTemperatureScope()
        setTimeScope()
        modeRadioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.kitchenRadioButton -> {
                    maxTemp = 60
                }
                R.id.showerRadioButton -> {
                    maxTemp = 60
                }
                R.id.tempRadioButton -> {
                    maxTemp = 48
                }
            }
            setTemperatureScope()
        }
    }

    private val callback = Handler.Callback { message ->
        if (message.what == 2022092601) {
            val parseObject = JSONObject.parseObject(message.obj as String)
            parseObject.keys.forEach {
                when (it) {

                }
            }
        }
        true
    }

    /**
     * 设置温度范围
     * */
    private fun setTemperatureScope() {
        temperatureSlider.tickCount = abs(maxTemp - minTemp)
        temperatureSlider.currentProgress = currentProgress
        currentTempView.text = String.format("${minTemp + currentProgress}")
        minTempView.text = minTemp.toString()
        maxTempView.text = maxTemp.toString()
    }

    /**
     * 设置定时范围
     * */
    private fun setTimeScope() {
        timeSeekBar.tickCount = timeScope.size - 1
        timeSeekBar.currentProgress = currentTimeProgress
        currentTimeView.text = timeScope[currentTimeProgress].toString()
        minTimeView.text = "${timeScope.first()}"
        maxTimeView.text = "${timeScope.last()}"
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        temperatureSlider.setCallback(object : QMUISlider.Callback {
            override fun onProgressChange(
                slider: QMUISlider?, progress: Int, tickCount: Int, fromUser: Boolean
            ) {
                currentProgress = progress
                currentTempView.text = String.format("${minTemp + progress}")
            }

            override fun onTouchDown(
                slider: QMUISlider?, progress: Int, tickCount: Int, hitThumb: Boolean
            ) {

            }

            override fun onTouchUp(slider: QMUISlider?, progress: Int, tickCount: Int) {

            }

            override fun onStartMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {

            }

            override fun onStopMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {

            }
        })

        timeSeekBar.setCallback(object : QMUISlider.Callback {
            override fun onProgressChange(
                slider: QMUISlider?, progress: Int, tickCount: Int, fromUser: Boolean
            ) {
                currentTimeProgress = progress
                currentTimeView.text = timeScope[currentTimeProgress].toString()
            }

            override fun onTouchDown(
                slider: QMUISlider?, progress: Int, tickCount: Int, hitThumb: Boolean
            ) {

            }

            override fun onTouchUp(slider: QMUISlider?, progress: Int, tickCount: Int) {

            }

            override fun onStartMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {

            }

            override fun onStopMoving(slider: QMUISlider?, progress: Int, tickCount: Int) {

            }

        })
    }

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