package com.yidian.shenghuoquan.bmanagercenter.personnel.ui

import android.os.Bundle
import androidx.core.content.ContextCompat
import com.yidian.bcommon.constant.XRouterPathConstants
import com.yidian.bcommon.constant.XRouterPathConstants.Companion.PERSONAL_ADD_STAFF
import com.yidian.bcommon.base.BaseActivity
import com.yidian.bcommon.extensions.clickAntiShake
import com.yidian.bcommon.services.ZapServiceActionConstants
import com.yidian.bcommon.services.ZapServiceNameConstants
import com.yidian.bcommon.widget.EditTextInputCallback
import com.yidian.bcommon.widget.PhoneNumberTextWatcher
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ActivityAddStaffBinding
import com.yidian.shenghuoquan.bmanagercenter.personnel.http.PersonalRemoteDataSource
import com.yidian.shenghuoquan.bmanagercenter.personnel.http.callback.IPersonalAddUserCallBack
import com.yidian.bcommon.utils.CountDownTimerUtils
import com.yidian.bcommon.utils.TextWatcherAdapter
import com.yidian.utils.ToastUtil
import com.yidian.xpage.XPageManager
import com.yidian.yac.core.zap.ZapTicket
import org.json.JSONObject

/**
 * 人员管理 —— 添加员工
 */
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 jsonObject = JSONObject()
    jsonObject.put("mobile", mobile)
    jsonObject.put("scene", "add_user")
    ZapTicket(ZapServiceNameConstants.GeneralAbilityService)
      .withAction(ZapServiceActionConstants.ActionRequestSmsCode)
      .withServiceParams(jsonObject)
      .onResult { result ->
        // 请求成功开始倒计时
        if (result.result as Boolean) {
          mCountDownTimerUtils.start()
        }
      }
      .ship()
  }

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

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