package com.yidian.shenghuoquan.bmanagercenter.widget

import android.content.Context
import android.text.Editable
import android.text.InputFilter
import android.text.TextUtils
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.yidian.nightmode.widget.YdConstraintLayout
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ViewLifeAccountAuthIdentityInfoEditBinding

/**
 * author: yinjiacheng
 * date: 5/27/21 2:12 PM
 * description: 生活号认证 身份证信息录入
 */
class LifeAccountAuthIdentityInfoEditView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : YdConstraintLayout(context, attrs, defStyle), View.OnClickListener, TextWatcher, View.OnFocusChangeListener {

  private val viewBinding: ViewLifeAccountAuthIdentityInfoEditBinding =
    ViewLifeAccountAuthIdentityInfoEditBinding.bind(
      View.inflate(
        getContext(),
        R.layout.view_life_account_auth_identity_info_edit,
        this
      )
    )

  private var callback: OnLifeAccountAuthIdentityInfoEditViewCallback? = null

  /**
   * 是否输入错误标识位
   */
  private var isInputError = false

  init {
    val typedArray =
      context.obtainStyledAttributes(attrs, R.styleable.LifeAccountAuthIdentityInfoEditView)
    viewBinding.tvContent.text =
      typedArray.getText(R.styleable.LifeAccountAuthIdentityInfoEditView_LifeAccountAuthIdentityInfoEditView_content_text)
    viewBinding.etContent.hint =
      typedArray.getText(R.styleable.LifeAccountAuthIdentityInfoEditView_LifeAccountAuthIdentityInfoEditView_hint_text)
    typedArray.recycle()
    viewBinding.ivClear.setOnClickListener(this)
    viewBinding.etContent.addTextChangedListener(this)
    viewBinding.etContent.onFocusChangeListener = this
  }

  /**
   * 设置标题
   */
  fun setTitleContent(content: String) {
    viewBinding.tvContent.text = content
  }

  /**
   * 展示错误提示
   */
  fun showErrorTips(tips: String) {
    viewBinding.tvErrorTips.visibility = View.VISIBLE
    viewBinding.tvErrorTips.text = tips
    viewBinding.viewDivider.setBackgroundColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
    highLightEditContent()
  }

  /**
   * 隐藏错误提示
   */
  private fun dismissErrorTips() {
    viewBinding.tvErrorTips.visibility = View.GONE
    viewBinding.tvErrorTips.text = ""
    viewBinding.viewDivider.setBackgroundColor(ContextCompat.getColor(context, R.color.color_333333))
    cancelHighLightEditContent()
  }

  /**
   * 清除输入的文字
   */
  fun clearEditContent() {
    viewBinding.etContent.setText("")
    if (isInputError) {
      dismissErrorTips()
    }
    callback?.onTextClear(this)
  }

  /**
   * 填充传入的文字
   */
  fun fillEditContent(content: String?) {
    if (!TextUtils.isEmpty(content)) {
      viewBinding.etContent.setText(content)
      viewBinding.ivClear.visibility = View.VISIBLE
    }
  }

  /**
   * 禁止修改
   */
  fun disableModify() {
    viewBinding.ivClear.visibility = View.GONE
    viewBinding.etContent.keyListener = null
  }

  /**
   * 高亮显示输入文本
   */
  fun highLightEditContent() {
    isInputError = true
    viewBinding.etContent.setTextColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
  }

  /**
   * 取消高亮显示输入文本
   */
  fun cancelHighLightEditContent() {
    isInputError = false
    viewBinding.etContent.setTextColor(ContextCompat.getColor(context, R.color.color_333333))
  }

  /**
   * 输入框获取焦点
   */
  fun requestEditTextFocus() {
    viewBinding.etContent.isFocusable = true
    viewBinding.etContent.isFocusableInTouchMode = true
    viewBinding.etContent.requestFocus()
  }

  /**
   * 获取当前输入的内容
   */
  fun getEditContent(): String {
    return viewBinding.etContent.text.toString()
  }

  /**
   * 设置输入过滤
   */
  fun setInputFilters(vararg filter: InputFilter) {
    viewBinding.etContent.filters = filter
  }

  override fun onClick(v: View?) {
    if (v?.id == R.id.iv_clear) {
      // 清除输入框中的内容
      clearEditContent()
    }
  }

  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?) {
    // 处理一键清除按钮
    viewBinding.ivClear.visibility = if (TextUtils.isEmpty(s)) View.GONE else View.VISIBLE
    callback?.onTextChange(this, s.toString())
    if (isInputError) {
      dismissErrorTips()
    }
  }

  override fun onFocusChange(v: View?, hasFocus: Boolean) {
    if (hasFocus) {
      // 获得焦点
      viewBinding.viewDivider.setBackgroundColor(ContextCompat.getColor(context, R.color.color_333333))
    } else {
      // 失去焦点
      viewBinding.viewDivider.setBackgroundColor(ContextCompat.getColor(context, R.color.color_FFF2F2F2))
    }
  }

  fun setOnLifeAccountAuthIdentityInfoEditViewCallback(callback: OnLifeAccountAuthIdentityInfoEditViewCallback) {
    this.callback = callback
  }

  interface OnLifeAccountAuthIdentityInfoEditViewCallback {
    /**
     * 清除内容
     */
    fun onTextClear(from: LifeAccountAuthIdentityInfoEditView)

    /**
     * 内容修改
     */
    fun onTextChange(from: LifeAccountAuthIdentityInfoEditView, text: String)
  }
}
