package com.dahe.pass.ui.mine.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.dahe.dispatch.utils.UserUtils
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.net.BaseObserver
import com.dahe.pass.net.DataManager
import com.dahe.pass.net.RxHttpCallBack
import com.dahe.pass.ui.account.LoginActivity
import com.dahe.pass.utils.SPUtils
import kotlinx.android.synthetic.main.activity_change_phone.*

/**
 * @ClassName ChangePwdActivity
 * @Author 用户
 * @Date 2022/1/13 16:59
 * @Description TODO
 */
class ChangePhoneActivity : BaseActivity(), View.OnClickListener {
    override fun getLayout() = R.layout.activity_change_phone

    override fun initView(savedInstanceState: Bundle?) {
        setStatusBarCancelLightColor()
        setTitleBar("修改手机号") { finish() }
        val phone = SPUtils.getUserInfo(this).userInfo.phone
        tvPhone.text = phone

        tvCode.setOnClickListener(this)
        btn_log.setOnClickListener(this)
    }

    override fun initDate() {

    }

    override fun onClick(view: View?) {
        super.onClick(view)
        when(view?.id){
            R.id.tvCode->getCode()
            R.id.btn_log->sub()
        }
    }

    fun sub(){

        val phone = etCode.text.toString()
        val code = etPass.text.toString()

        if (phone.isNullOrEmpty()||code.isNullOrEmpty()){
            showToast("请完善信息")
            return
        }

        DataManager.getInstance().phoneEdit(phone,code)
            .compose(DataManager.setThread())
            .subscribe(BaseObserver(mContext,object :RxHttpCallBack<Any>(){
                override fun onSuccess(t: CommonResponseBean<Any>?) {
                    super.onSuccess(t)
//                    finish()

                    UserUtils.instance.loginOut(this@ChangePhoneActivity)
                    val intent = Intent(mContext, LoginActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
                    startActivity(intent)
//                    ActivityUtils.finishToActivity(LoginActivity::class.java,true)
                }
            }))
    }

    fun getCode() {

        var phone = etCode.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("请输入正确手机号")
        }

    }
}