package com.bc_chat.account.fragment

import android.text.TextUtils
import android.view.View
import com.bc_chat.account.R
import com.bc_chat.account.contract.VerifySmsContract
import com.bc_chat.account.databinding.FragmentStep1ForgetpwdBinding
import com.bc_chat.account.presenter.VerifySmsCodePresenter
import com.bc_chat.bc_base.utils.Const
import com.zhaohaoting.framework.abs.AbsV4Fragment
import com.zhaohaoting.framework.utils.ToastUtils
import com.zhaohaoting.framework.utils.Utils
import java.lang.Exception

/**
 * author：Created by zhaohaoting on 2019/2/21
 * email：526309416@qq.com
 * desc：忘记密码第一步
 */
class Step1ForgetPwdFragment : AbsV4Fragment<VerifySmsCodePresenter<VerifySmsContract.View>>(), View.OnClickListener, VerifySmsContract.View {
    override fun loadWebPageSuccess(content: String) {
    }

    private val binding by lazy {
        rootDataBinding as FragmentStep1ForgetpwdBinding
    }

    fun setNextListener(listener: (phone: String, verCode: String) -> Unit) {
        nextListener = object : OnNextListener {
            override fun next(phone: String, verCode: String) {
                listener(phone, verCode)
            }
        }
    }

    override fun initialize() {
        binding.onClickEvent = this
    }

    override fun initPresenter(): VerifySmsCodePresenter<VerifySmsContract.View>? {
        return VerifySmsCodePresenter(this)
    }

    override fun onClick(v: View?) {
        val phone = binding.etAccount.text.toString().trim()
        when (v?.id) {
            R.id.get_ver_code -> {
                if (!Utils.isMobilePhone(phone)) {
                    ToastUtils.show("请输入正确的电话号码")
                    return
                }
                presenter.senVerCode(phone, Const.VerfiyCodeType.FORGET_TYPE)
                binding.getVerCode.isEnabled = false
            }
            R.id.btn_next -> {
                val verCode = binding.etVerCode.text.toString().trim()
                when {
                    !Utils.isMobilePhone(phone) -> {
                        ToastUtils.show("请输入正确的电话号码")
                        return
                    }
                    TextUtils.isEmpty(verCode) || verCode.length < 6 -> {
                        ToastUtils.show("输入的验证码不能少于六位")
                        return
                    }
                }
                presenter.verifySmsCode(phone, verCode, Const.VerfiyCodeType.FORGET_TYPE)
                binding.btnNext.isEnabled = false
            }
        }
    }

    override fun vercodeSendSuccess(s: String?) {
        ToastUtils.show(s)
        binding.getVerCode.btnVerCodeNoFos()
    }

    override fun vercodeSendFailure(exception: Exception?) {
        binding.getVerCode.isEnabled = true
    }

    override fun verifySuccess() {
        nextListener?.next(binding.etAccount.text.toString(), binding.etVerCode.text.toString())
        binding.btnNext.isEnabled = true
    }

    override fun verifyFailure(exception: Exception?) {
        binding.btnNext.isEnabled = true
    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_step1_forgetpwd
    }

    private var nextListener: OnNextListener? = null

    interface OnNextListener {
        fun next(phone: String, verCode: String)
    }

    override fun onDestroy() {
        binding.getVerCode.destroy()
        super.onDestroy()
    }
}
