package com.yidian.shenghuoquan.newscontent.widget

import android.content.Context
import android.graphics.Color
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 androidx.core.view.isVisible
import com.yidian.nightmode.widget.YdConstraintLayout
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ViewLifeAccountAuthBusinessInfoEditBinding

/**
 * author: yinjiacheng
 * date: 5/28/21 9:04 PM
 * description: 生活号认证 营业执照信息录入
 */
class LifeAccountAuthBusinessInfoEditView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : YdConstraintLayout(context, attrs, defStyle), View.OnClickListener, TextWatcher, View.OnFocusChangeListener {

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

  private var callback: OnLifeAccountAuthBusinessInfoEditViewCallback? = null

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

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

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

  /**
   * 隐藏错误提示
   */
  private fun dismissErrorTips() {
    viewBinding.tvErrorTips.isVisible = false
    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.isVisible = false
    viewBinding.etContent.keyListener = null
  }

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

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

  /**
   * 设置输入过滤
   */
  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.isVisible = !TextUtils.isEmpty(s)
    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 setOnLifeAccountAuthBusinessInfoEditViewCallback(callback: OnLifeAccountAuthBusinessInfoEditViewCallback) {
    this.callback = callback
  }

  interface OnLifeAccountAuthBusinessInfoEditViewCallback {
    /**
     * 清除内容
     */
    fun onTextClear(from: LifeAccountAuthBusinessInfoEditView)

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