package com.casic.br.view

import android.content.Context
import android.os.CountDownTimer
import com.casic.br.R
import com.casic.br.extensions.initLayoutImmersionBar
import com.casic.br.utils.LoadingDialogHub
import com.gyf.immersionbar.ImmersionBar
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.isPhoneNumber
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.extensions.show
import com.tuya.smart.android.user.api.IRegisterCallback
import com.tuya.smart.android.user.bean.User
import com.tuya.smart.home.sdk.TuyaHomeSdk
import com.tuya.smart.sdk.api.IResultCallback
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.include_left_back_title.*
import java.util.*

class RegisterActivity : KotlinBaseActivity() {

    private val kTag = "RegisterActivity"
    private val context: Context = this@RegisterActivity

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

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        initLayoutImmersionBar(rootView)
        leftBackView.setOnClickListener { finish() }
        titleView.text = "注册账号"
    }

    override fun initData() {

    }

    override fun initEvent() {
        requestCodeButton.setOnClickListener {
            if (userPhoneView.text.toString().isBlank()) {
                "请输入手机号".show(this)
                return@setOnClickListener
            }
            if (!userPhoneView.text.toString().isPhoneNumber()) {
                "手机号不正确,请检查".show(this)
                return@setOnClickListener
            }
            //发送验证码
            TuyaHomeSdk.getUserInstance().sendVerifyCodeWithUserName(
                userPhoneView.text.toString(), "", "86", 1,
                object : IResultCallback {
                    override fun onSuccess() {
                        "发送验证码成功".show(context)
                    }

                    override fun onError(code: String?, error: String?) {
                        "发送验证码失败".show(context)
                    }
                })

            //开始倒计时
            object : CountDownTimer(60 * 1000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    requestCodeButton.text = String.format("${millisUntilFinished / 1000}s")
                    requestCodeButton.isEnabled = false
                }

                override fun onFinish() {
                    requestCodeButton.text = "获取验证码"
                    requestCodeButton.isEnabled = true
                }
            }.start()
        }

        registerButton.setOnClickListener {
            val userPhone = userPhoneView.text.toString()
            if (userPhone.isBlank()) {
                "请输入手机号".show(this)
                return@setOnClickListener
            }
            val smsCode = requestCodeView.text.toString()
            if (smsCode.isBlank()) {
                "请输入验证码".show(this)
                return@setOnClickListener
            }
            val userPassword = userPasswordView.text.toString()
            if (userPassword.isBlank()) {
                "请输入密码".show(this)
                return@setOnClickListener
            }

            LoadingDialogHub.show(this, "注册中，请稍后")
            TuyaHomeSdk.getUserInstance().registerAccountWithPhone(
                "86", userPhone, userPassword, smsCode,
                object : IRegisterCallback {
                    override fun onSuccess(user: User?) {
                        LoadingDialogHub.dismiss()
                        navigatePageTo<RegisterResultActivity>()
                    }

                    override fun onError(code: String?, error: String?) {
                        LoadingDialogHub.dismiss()
                        "用户注册失败".show(context)
                    }
                })
        }
    }
}