package com.yidian.shenghuoquan.newscontent.personnel.ui

import android.os.Bundle
import androidx.core.content.ContextCompat
import com.yidian.bcommon.XRouterPathConstants
import com.yidian.bcommon.XRouterPathConstants.Companion.PERSONAL_ADD_STAFF
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.bcommon.widget.EditTextInputCallback
import com.yidian.bcommon.widget.PhoneNumberTextWatcher
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ActivityAddStaffBinding
import com.yidian.shenghuoquan.newscontent.personnel.http.PersonalRemoteDataSource
import com.yidian.shenghuoquan.newscontent.personnel.http.callback.IPersonalAddUserCallBack
import com.yidian.shenghuoquan.newscontent.personnel.http.callback.IPersonalGetSmsCodeCallBack
import com.yidian.shenghuoquan.newscontent.utils.CountDownTimerUtils
import com.yidian.shenghuoquan.newscontent.utils.TextWatcherAdapter
import com.yidian.utils.ToastUtil
import com.yidian.xpage.XPageManager

/**
 * 人员管理 —— 添加员工
 */
class AddStaffActivity : BaseActivity<ActivityAddStaffBinding>() {


  private var newCodeFinish: Boolean = false
  private var newMobileFinish: Boolean = false
  private lateinit var mCountDownTimerUtils: CountDownTimerUtils

  override fun createViewBinding(): ActivityAddStaffBinding {
    return ActivityAddStaffBinding.inflate(layoutInflater)
  }

  override fun getXPageName(): String {
    return PERSONAL_ADD_STAFF
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    initTitle()
    initClick()
    // 初始化倒计时
    mCountDownTimerUtils = CountDownTimerUtils(viewBind.tvGetCode, 60000, 1000)
  }

  private fun initClick() {
    updateConfirmButton()
    // 手机号输入监听
    viewBind.etMobile.addTextChangedListener(PhoneNumberTextWatcher(viewBind.etMobile, object : EditTextInputCallback {
      override fun inputCallback(s: String) {
        if (s.length == 13) {
          newMobileFinish = true
          updateConfirmButton()
        } else if (newMobileFinish) {
          newMobileFinish = false
          updateConfirmButton()
        }
      }
    }))

    // 验证码输入监听
    viewBind.etSmsCode.addTextChangedListener(object : TextWatcherAdapter() {
      override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        if (s?.length == 6) {
          newCodeFinish = true
          updateConfirmButton()
        } else if (newCodeFinish) {
          newCodeFinish = false
          updateConfirmButton()
        }
      }
    })

    // 发送验证码
    viewBind.tvGetCode.clickAntiShake {
      viewBind.etSmsCode.setText("")
      val mobile = viewBind.etMobile.text.toString().replace(" ", "")
      if (mobile.length == 11) {
        requestSmsCode(mobile)
      } else {
        ToastUtil.showToast(this, "请输入11位手机号")
      }
    }
  }

  private fun initTitle() {
    viewBind.include.tvTitle.text = "添加员工"
    viewBind.include.ivBack.setOnClickListener {
      XPageManager.pop(null)
    }
    viewBind.include.tvMenu.text = "确认添加"
    viewBind.include.tvMenu.setTextColor(ContextCompat.getColor(this, R.color.color_661852F1))
    viewBind.include.tvMenu.setOnClickListener {
      requestAddUser()
    }
  }

  private fun requestAddUser() {
    val hashMap = HashMap<String, String?>()
    hashMap["mobile"] = viewBind.etMobile.text.toString().replace(" ", "")
    hashMap["code"] = viewBind.etSmsCode.text.toString()
    PersonalRemoteDataSource.postAddUser(postAddUserCallBack, hashMap)
  }

  private fun updateConfirmButton() {
    if (newMobileFinish && newCodeFinish) {
      setChangeButtonStatus(true)
    } else {
      setChangeButtonStatus(false)
    }
  }

  private fun setChangeButtonStatus(flag: Boolean) {
    if (flag) {
      viewBind.include.tvMenu.setTextColor(ContextCompat.getColor(this, R.color.color_1852F1))
      viewBind.include.tvMenu.isEnabled = true
    } else {
      viewBind.include.tvMenu.setTextColor(ContextCompat.getColor(this, R.color.color_661852F1))
      viewBind.include.tvMenu.isEnabled = false
    }
  }

  /**
   * 获取验证码
   */
  private fun requestSmsCode(mobile: String) {
    val paramsMap = HashMap<String, String?>()
    paramsMap["mobile"] = mobile
    paramsMap["scene"] = "add_user"
    PersonalRemoteDataSource.getSmsCode(getSmsCodeCallBack, paramsMap)
  }

  private val getSmsCodeCallBack = object : IPersonalGetSmsCodeCallBack {

    override fun getSmsCodeSuccess() {
      // 开始倒计时
      mCountDownTimerUtils.start()
    }
  }

  private val postAddUserCallBack = object : IPersonalAddUserCallBack {
    override fun addUserSuccess() {
      // 返回员工列表并刷新
      XPageManager.push(XRouterPathConstants.PERSONAL_MY_STAFF, null)
    }
  }

  override fun onDestroy() {
    super.onDestroy()
    mCountDownTimerUtils.cancel()
  }
}
