package com.casic.br.view.device.robot

import android.util.Log
import com.alibaba.fastjson.JSONObject
import com.casic.br.R
import com.casic.br.utils.CommandManager
import com.casic.br.utils.TuyaDeviceListener
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.convertColor
import com.pengxh.kt.lite.utils.Constant
import com.pengxh.kt.lite.utils.ImmerseStatusBarUtil
import com.pengxh.kt.lite.widget.dialog.AlertControlDialog
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.sdk.api.ITuyaDevice
import kotlinx.android.synthetic.main.activity_robot_settings.*
import kotlinx.android.synthetic.main.include_left_back_title.*

class RobotSettingsActivity : KotlinBaseActivity() {

    private lateinit var deviceInstance: ITuyaDevice

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

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        ImmerseStatusBarUtil.setColor(this, R.color.mainBackColor.convertColor(this))

        leftBackView.setOnClickListener { finish() }
        titleView.text = "耗材维护"
    }

    override fun initData() {
        val deviceId = intent.getStringExtra(Constant.INTENT_PARAM)!!
        deviceInstance = TuyaHomeSdk.newDeviceInstance(deviceId)
        deviceInstance.registerDevListener(object : TuyaDeviceListener() {
            override fun onDpUpdate(devId: String?, dpStr: String?) {
                Log.d("Casic", "RobotSettingsActivity => onDpUpdate: $dpStr")
                val parseObject = JSONObject.parseObject(dpStr)
                parseObject.keys.forEach {
                    if (it == "49") {
                        //TODO 待定
                        val any = parseObject[it]
                    }
                }
            }
        })
    }

    override fun initEvent() {
        clearBatteryButton.setOnClickListener {
            showDialog()
        }

        clearFilterButton.setOnClickListener {
            showDialog()
        }

        clearMiddleButton.setOnClickListener {
            showDialog()
        }

        clearEdgeButton.setOnClickListener {
            showDialog()
        }
    }

    private fun showDialog() {
        AlertControlDialog.Builder()
            .setContext(this)
            .setTitle("提示")
            .setMessage("是否清除记录？如果没有更换耗材而清除记录将会导致数据错误")
            .setNegativeButton("取消")
            .setPositiveButton("确定")
            .setOnDialogButtonClickListener(object :
                AlertControlDialog.OnDialogButtonClickListener {
                override fun onCancelClick() {

                }

                override fun onConfirmClick() {
                    //TODO 待定
//                    CommandManager.sendTuyaDp(deviceInstance, "49".createCommand("64"))
                }
            })
            .build().show()
    }

    override fun onResume() {
        super.onResume()
        //每次获取焦点都查询设备的开关机状态
        CommandManager.getTuyaDp(deviceInstance, "49")
    }

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