package com.casic.br.view

import android.view.View
import com.casic.br.R
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.utils.LoadingDialogHub
import com.casic.br.utils.LocaleConstant
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.Constant
import com.pengxh.kt.lite.utils.SaveKeyValues
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.home.sdk.builder.ActivatorBuilder
import com.tuya.smart.sdk.api.ITuyaActivatorGetToken
import com.tuya.smart.sdk.api.ITuyaSmartActivatorListener
import com.tuya.smart.sdk.bean.DeviceBean
import com.tuya.smart.sdk.enums.ActivatorModelEnum
import kotlinx.android.synthetic.main.activity_reset_device.*
import kotlinx.android.synthetic.main.include_left_back_title.*
import java.util.*

class ResetDeviceActivity : KotlinBaseActivity() {

    private lateinit var params: ArrayList<String>

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

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        initLayoutImmersionBar(rootView)
        leftBackView.setOnClickListener { finish() }
        titleView.visibility = View.GONE
    }

    override fun initData() {
        params = intent.getStringArrayListExtra(Constant.INTENT_PARAM)!!
    }

    override fun initEvent() {
        nextStepButton.setOnClickListener {
            if (!confirmCheckBox.isChecked) {
                "请确认设备指示灯是否闪烁".show(this)
                return@setOnClickListener
            }
            LoadingDialogHub.show(this, "设备配网中，请稍后")
            val homeId = SaveKeyValues.getValue(LocaleConstant.HOME_ID, "") as String
            TuyaHomeSdk.getActivatorInstance().getActivatorToken(homeId.toLong(),
                object : ITuyaActivatorGetToken {
                    override fun onSuccess(token: String) {
                        //调用 配网接口 需要提供路由器的 SSID（即 Wi-Fi 名称）、密码、从云端获取的 Token 等。
                        val builder = ActivatorBuilder().apply {
                            this.ssid = params[0]
                            this.context = this@ResetDeviceActivity
                            this.password = params[1]
                            this.activatorModel = ActivatorModelEnum.TY_EZ
                            this.timeOut = 100
                            this.token = token
                            this.listener = object : ITuyaSmartActivatorListener {
                                override fun onError(errorCode: String?, errorMsg: String?) {
                                    "添加设备超时".show(this@ResetDeviceActivity)
                                    LoadingDialogHub.dismiss()
                                }

                                override fun onActiveSuccess(devResp: DeviceBean?) {
                                    LoadingDialogHub.dismiss()
                                    navigatePageTo<AddDeviceResultActivity>()
                                }

                                override fun onStep(step: String?, data: Any?) {
                                    LoadingDialogHub.dismiss()
                                }
                            }
                        }
                        TuyaHomeSdk.getActivatorInstance().newMultiActivator(builder).start()
                    }

                    override fun onFailure(s: String, s1: String) {
                        LoadingDialogHub.dismiss()
                    }
                })
        }
    }
}