package com.abbidot.tracker.deprecated.ui.fragment.login

import android.content.Context
import android.text.TextUtils
import android.view.View
import androidx.fragment.app.viewModels
import com.abbidot.tracker.R
import com.abbidot.tracker.base.BaseFragment
import com.abbidot.tracker.constant.GetResultCallback
import com.abbidot.tracker.databinding.FragmentBindPhoneCnBinding
import com.abbidot.tracker.deprecated.ui.activity.LoginRegisterCNActivity
import com.abbidot.tracker.deprecated.ui.fragment.vm.LoginCNViewModel
import com.abbidot.tracker.deprecated.ui.fragment.vm.RegisterCNViewModel
import com.abbidot.tracker.vm.CountDownTimerViewModel


/**
 * 绑定手机号
 */
class BindPhoneCNFragment :
    BaseFragment<FragmentBindPhoneCnBinding>(FragmentBindPhoneCnBinding::inflate) {

    private val mRegisterCNViewModel: RegisterCNViewModel by viewModels()
    private val mCountDownTimerViewModel: CountDownTimerViewModel by viewModels()
    private val mLoginCNViewModel: LoginCNViewModel by viewModels()

    companion object {
        @JvmStatic
        fun newInstance(context: Context) = BindPhoneCNFragment().apply {
            mContext = context
        }
    }

    override fun initData() {
        super.initData()
        val activity = activity as LoginRegisterCNActivity
        activity.setTopTitle(getString(R.string.login_txt_login_first))
        activity.setTopContent(getString(R.string.login_txt_login_bind_phone))

        mViewBinding.apply {
            setOnClickListenerViews(llBindPhoneGetPhoneCode.btnGetCode, btnBindBindPhoneCn)
        }
    }

    override fun liveDataObserve() {
        mRegisterCNViewModel.mSendCodeResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    mCountDownTimerViewModel.startCountDown(60)
                    showToast(R.string.txt_code_success)
                }
            })
        }

        //倒计时
        mCountDownTimerViewModel.mCountDowningLiveData.observe(viewLifecycleOwner) {
            mViewBinding.llBindPhoneGetPhoneCode.btnGetCode.apply {
                isEnabled = false
                text = "${it}s"
            }
        }
        mCountDownTimerViewModel.mCountDownEndLiveData.observe(viewLifecycleOwner) {
            mViewBinding.llBindPhoneGetPhoneCode.btnGetCode.apply {
                isEnabled = true
                text = getString(R.string.register_txt_get_code)
            }
        }

        mLoginCNViewModel.mWXBindResultLiveData.observe(viewLifecycleOwner) {
            dealRequestResult(it, object : GetResultCallback {
                override fun onResult(any: Any) {
                    val data = it.getOrNull()
                    data?.apply {
                        val activity = activity as LoginRegisterCNActivity
                        mLoginCNViewModel.loginSuccess(activity, this, R.string.txt_bind_success)
                    }
                }
            })
        }
    }

    /**
     * 获取验证码
     */
    private fun getPhoneCode() {
        val phone = mViewBinding.llBindPhoneGetPhoneCode.etGetCodePhone.text.toString()
        if (TextUtils.isEmpty(phone)) {
            showToast(R.string.txt_register_phone)
            return
        }
        mRegisterCNViewModel.sendCode(this, phone, 2)
    }

    /**
     *微信绑定手机号
     */
    private fun wxBindPhone() {
        val phone = mViewBinding.llBindPhoneGetPhoneCode.etGetCodePhone.text.toString()
        val code = mViewBinding.llBindPhoneGetPhoneCode.etPhoneCode.text.toString()
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
            showToast(R.string.txt_not_null)
            return
        }
        val activity = activity as LoginRegisterCNActivity
        mLoginCNViewModel.wxBind(this, phone, code, activity.mWXId)
    }

    override fun onClick(v: View?) {
        when (v!!) {
            mViewBinding.llBindPhoneGetPhoneCode.btnGetCode -> getPhoneCode()
            mViewBinding.btnBindBindPhoneCn -> wxBindPhone()
        }
    }
}