package com.zj365.mime.act

import android.text.Editable
import android.text.TextWatcher
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxRegTool
import com.xty.base.act.BaseVmAct
import com.xty.common.Const
import com.xty.common.arouter.ARouterUrl
import com.xty.common.util.CommonToastUtils
import com.zj365.mime.R
import com.zj365.mime.databinding.ActChangePwdBinding
import com.zj365.mime.vm.ChangePwdVm
import com.zj365.mime.weight.ConfirmDialog
import java.util.*

/**
 * 修改密码
 */
@Route(path = ARouterUrl.CHANGE_PWD)
class ChangePwdAct : BaseVmAct<ChangePwdVm>() {
    var timer: Timer? = null
    var title=""
    var timeNow = Const.timeBack
    val timeLive by lazy { MutableLiveData<String>() }
    val binding by lazy { ActChangePwdBinding.inflate(layoutInflater) }
    val dialog by lazy {
        ConfirmDialog(this) {
            finish()
        }
    }

    override fun setLayout() = binding.root
    override fun initData() {
        super.initData()
        isDarkBar = true
        intent.extras?.let {
            if(it.containsKey("title")){
                title=it.getString("title").toString()
            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        if(title.isEmpty()){
            binding.title.mTvTitle.text = getString(R.string.change_pwd)
        }else{
            binding.title.mTvTitle.text = title
        }

        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        binding.mConfirm.isSelected = false
        binding.mConfirm.isEnabled = false
        binding.mConfirm.setOnClickListener {
           var phone= binding.mAccount.text.toString().trim()
           var code= binding.mCode.text.toString().trim()
           var pwd= binding.mPsw.text.toString().trim()
            mViewModel.changePsw(phone,code,pwd)
        }
        binding.mTvCode.apply {
            isSelected = true
            setOnClickListener {
                val trim = binding.mAccount.text.toString().trim()
                if(RxRegTool.isMobileSimple(trim)){
                    mViewModel.sendCodeForget(trim)
                }else{
                    CommonToastUtils.showToast(R.string.please_input_ok_phone)
                }
            }
        }


        getTextWatch(binding.mAccount)
        getTextWatch(binding.mCode)
        getTextWatch(binding.mPsw)

    }

    override fun liveObserver() {
        mViewModel.codeLive.observe(this){
            timeStart()
            binding.mTvCode.apply {
                isEnabled = false
                isSelected = false
            }
        }
        mViewModel.changePwd.observe(this){
            dialog.show()
        }
        timeLive.observe(this) {
            --timeNow
            if (timeNow > 0) {
                binding.mTvCode.text = "${timeNow}秒"
            } else {
                timer!!.cancel()
                binding.mTvCode.isSelected = true
                binding.mTvCode.isEnabled = true
                timeNow =  Const.timeBack
                binding.mTvCode.text = getString(R.string.get_code)
            }
        }
    }


    fun timeStart() {
        timer = Timer()
        var task = object : TimerTask() {
            override fun run() {
                timeLive.postValue("")
            }
        }
        timer!!.schedule(task, 0, 1000)
    }

    fun changeStatus() {
        var phone = binding.mAccount.text.toString().trim()

        var code = binding.mCode.text.toString().trim()

        var pwd = binding.mPsw.text.toString().trim()

        if (phone.isNotEmpty() && code.isNotEmpty() && pwd.isNotEmpty()) {
            binding.mConfirm.isSelected = true
            binding.mConfirm.isEnabled = true
        } else {
            binding.mConfirm.isSelected = false
            binding.mConfirm.isEnabled = false
        }
    }


    fun getTextWatch(view: TextView) {
        var watcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                changeStatus()
            }
        }
        view.setTag(watcher)
        view.addTextChangedListener(watcher)
    }


    override fun onDestroy() {
        super.onDestroy()
        if (timer != null) {
            timer!!.cancel()
        }
    }


}