package com.bc_chat.account

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bc_chat.account.contract.VerifySmsContract
import com.bc_chat.account.databinding.ActivityRegisterBinding
import com.bc_chat.account.presenter.VerifySmsCodePresenter
import com.bc_chat.bc_base.WebViewActivity
import com.bc_chat.bc_base.config.RouteConfig
import com.bc_chat.bc_base.utils.Const
import com.bc_chat.bc_base.utils.StringUtils
import com.zhaohaoting.framework.abs.BaseActivity
import com.zhaohaoting.framework.utils.StrValidate
import com.zhaohaoting.framework.utils.ToastUtils
import com.zhaohaoting.framework.utils.Utils
import kotlinx.android.synthetic.main.activity_register.*

/**
 * author：Created by zhaohaoting on 2019/2/21
 * email：526309416@qq.com
 * desc：
 */
@Route(path = RouteConfig.REGISTER_ACTIVITY)
class RegisterActivity : BaseActivity<VerifySmsCodePresenter<VerifySmsContract.View>>(), View.OnClickListener, VerifySmsContract.View {

    private val binding by lazy {
        rootDataBinding as ActivityRegisterBinding
    }

    override fun initPresenter(): VerifySmsCodePresenter<VerifySmsContract.View>? {
        return VerifySmsCodePresenter(this)
    }

    override fun initialize() {
        binding.onClickEvent = this
        val protocol = tv_protocol.text
        StringUtils.protocol(this, protocol, tv_protocol) {
            presenter.loadWebPage(Const.WebPageType.USER_PROTOCOL)
        }
    }

    override fun loadWebPageSuccess(content: String) {
        WebViewActivity.startActivity(this, content, "用户协议", 1)
    }

    override fun vercodeSendSuccess(s: String?) {
        ToastUtils.show(s)
        binding.getVerCode.btnVerCodeNoFos()
    }

    override fun vercodeSendFailure(exception: Exception?) {
        binding.getVerCode.isEnabled = true
    }

    override fun verifySuccess() {
        binding.btnNext.isEnabled = true
        ARouter.getInstance().build(RouteConfig.PERFECT_INF_ACTIVITY)
                .withString("phone", binding.etAccount.text.toString().trim()).withString("password", binding.etPassword.text.toString().trim())
                .navigation(this, REGISTER_NEXT)
    }

    override fun verifyFailure(exception: Exception?) {
        binding.btnNext.isEnabled = true
    }

    override fun onClick(v: View?) {
        val phone = binding.etAccount.text.toString().trim()
        when (v?.id) {
            R.id.get_ver_code -> {
                if (!Utils.isMobilePhone(phone)) {
                    ToastUtils.show("请输入正确的电话号码")
                    return
                }
                presenter.senVerCode(phone, Const.VerfiyCodeType.REGISTER_TYPE)
                binding.getVerCode.isEnabled = false
            }
            R.id.btn_next -> {
                val verCode = binding.etVerCode.text.toString().trim()
                val password = binding.etPassword.text.toString().trim()

                when {
                    StrValidate.isDigit(phone) || StrValidate.isLetter(phone) -> {
                        ToastUtils.show("账号必须是字母和数字组合")
                        return
                    }
                    TextUtils.isEmpty(phone) || phone.length < 6 -> {
                        ToastUtils.show("账号不能少于六位")
                        return
                    }
                    !Utils.isSimplePasswrod(password) -> {
                        ToastUtils.show("密码不能少于六位")
                        return
                    }
                }
//                presenter.verifySmsCode(phone, verCode, Const.VerfiyCodeType.REGISTER_TYPE)
//                binding.btnNext.isEnabled = false
                verifySuccess();
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return@onActivityResult
        when (requestCode) {
            REGISTER_NEXT -> {
                setResult(Activity.RESULT_OK)
                finish()
            }
        }
    }


    override fun getLayoutResId(): Int {
        return R.layout.activity_register
    }

    companion object {
        const val REGISTER_NEXT = 4
    }

    override fun onDestroy() {
        binding.getVerCode.destroy()
        super.onDestroy()
    }
}
