package com.dahe.pass.ui.account.activity

import android.os.Bundle
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.dahe.mylibrary.base.BaseActivity
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.CountDownTimerUtils
import com.dahe.mylibrary.utils.PhoneFormatCheckUtils
import com.dahe.pass.R
import com.dahe.pass.base.AppConfig
import com.dahe.pass.bean.BusInfoBean
import com.dahe.pass.net.BaseObserver
import com.dahe.pass.net.DataManager
import com.dahe.pass.net.RxHttpCallBack
import com.dahe.pass.ui.HomeActivity
import com.dahe.pass.utils.AppActivityUtils
import kotlinx.android.synthetic.main.activity_register.*
import java.util.*

class RegisterActivity : BaseActivity(), View.OnClickListener {


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

    override fun initView(savedInstanceState: Bundle?) {
//        setStatusBarCancelLightColor()
        setStatusBarColor(R.color.white)


        btn_log.setOnClickListener(this)
        tv_yhxy.setOnClickListener(this)
        tv_yszc.setOnClickListener(this)
        tvXyh.setOnClickListener(this)
        tvZc.setOnClickListener(this)
        tvCode.setOnClickListener(this)
        cb.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                etPass.setTransformationMethod(PasswordTransformationMethod.getInstance())
            } else {
                etPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance())
            }
            etPass.setSelection(etPass.getText().toString().length)
        }
    }

    override fun initDate() {
//        getCode()
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.btn_log -> next()
//            R.id.tv_forget -> ActivityUtils.startActivity(mContext, ForgetPwdActivity::class.java)
            R.id.tv_yhxy -> AppActivityUtils.openWebViewActivity(this,
                "用户服务协议",
                "https://tms.dahehuoyun.com/profile/upload/static/serviceAgreement.html");
            R.id.tv_yszc -> AppActivityUtils.openWebViewActivity(this, "隐私政策",
                "https://tms.dahehuoyun.com/profile/upload/static/privacyPolicy.html")

            R.id.tvZc, R.id.tvXyh -> finish()

            R.id.tvCode -> getCode()

        }
    }


    fun getCode() {

        var phone = etPhone.text.toString()
        if (PhoneFormatCheckUtils.isPhoneLegal(phone)) {
            DataManager.getInstance().getSmsCode(phone)
                .compose(DataManager.setThread())
                .subscribe(BaseObserver(mContext, object : RxHttpCallBack<Any>() {
                    override fun onSuccess(t: CommonResponseBean<Any>?) {
                        super.onSuccess(t)
                        val countDownTimerUtils = CountDownTimerUtils(tvCode, 60000, 1000)
                        countDownTimerUtils.start()
                    }
                }))
        } else {
            showToast("请输入正确手机号")
        }

    }

    fun next() {
        val phone = etPhone.text.toString()
        val code = etCode.text.toString()
        val pwd = etPass.text.toString()

        if (phone.isNullOrEmpty() || code.isNullOrEmpty() || pwd.isNullOrEmpty()) {
            showToast("请完善信息")
            return
        }
        val busInfoBean = BusInfoBean()
        busInfoBean.phone = phone

        val paramsDao = BusInfoBean.ParamsDao()
        paramsDao.code = code
        busInfoBean.params = paramsDao

        busInfoBean.pwd = pwd
        val bundle = Bundle()
        bundle.putSerializable(AppConfig.BEAN, busInfoBean)
        ActivityUtils.startActivity(mContext, BusinessInfoActivity::class.java, bundle)
    }

}