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.event.MyInfoEvent
import com.xty.common.util.CommonToastUtils
import com.zj365.mime.R
import com.zj365.mime.databinding.ActChangePhoneBinding
import com.zj365.mime.vm.ChangePhoneVm
import org.greenrobot.eventbus.EventBus
import java.util.*

/**
 * 修改手机号
 */
@Route(path = ARouterUrl.CHANGE_PHONE)
class ChangePhoneAct: BaseVmAct<ChangePhoneVm>() {
    var timer: Timer? = null

    var timeNow = Const.timeBack
    val timeLive by lazy { MutableLiveData<String>() }
    val binding by lazy{ActChangePhoneBinding.inflate(layoutInflater)}
    var phone=""

    override fun setLayout()=binding.root

    override fun initData() {
        super.initData()
        isDarkBar=true
          phone=intent.extras!!.getString("phone") .toString()
        binding.mNowPhone.text="当前绑定的手机号：$phone"
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = getString(R.string.change_phone)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        binding.mConfirm.setOnClickListener {
            var newPhone= binding.mAccount.text.toString().trim()
            var code= binding.mCode.text.toString().trim()
            mViewModel.changeBindPhone(newPhone,code)
        }
        binding.mTvCode.apply {
            isSelected = true
            setOnClickListener {
                val newPhone = binding.mAccount.text.toString().trim()
                if(newPhone==phone){
                    CommonToastUtils.showToast("已绑定当前手机号")
                    return@setOnClickListener
                }
                if(RxRegTool.isMobileSimple(newPhone)){
                    mViewModel.sendCodeForget(newPhone)
                }else{
                    CommonToastUtils.showToast(R.string.please_input_ok_phone)
                }
            }
        }
        getTextWatch(binding.mAccount)
        getTextWatch(binding.mCode)
    }

    override fun liveObserver() {
        mViewModel.codeLive.observe(this){
            timeStart()
            binding.mTvCode.apply {
                isEnabled = false
                isSelected = false
            }
        }
        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)
            }
        }
        mViewModel.changePhone.observe(this){
            CommonToastUtils.showToast(R.string.phone_change_success)
            EventBus.getDefault().post(MyInfoEvent())
            finish()
        }
    }



    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()


        if (phone.isNotEmpty() && code.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()
        }
    }


}