package com.yidian.shenghuoquan.bmanagercenter.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.constant.Constant
import com.yidian.shenghuoquan.bmanagercenter.databinding.ViewLifeAccountLabelBinding
import com.yidian.bcommon.bean.LifeAccountItemBean

/**
 * author: yinjiacheng
 * date: 6/8/21 9:39 PM
 * description: 管理中心-生活号标签
 */
class LifeAccountLabelView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : ConstraintLayout(context, attrs, defStyle), View.OnClickListener {

  private val viewBinding = ViewLifeAccountLabelBinding.bind(
    View.inflate(
      getContext(),
      R.layout.view_life_account_label,
      this
    )
  )

  /**
   * 当前显示的生活号
   */
  private lateinit var curLifeAccount: LifeAccountItemBean.Response

  private var callback: OnLifeAccountLabelCallback? = null

  init {
    setOnClickListener(this)
  }

  /**
   * 设置生活号信息
   * 先判断认证状态 再判断审核状态 即审核中和审核驳回状态可覆盖认证状态
   * @param data 生活号详情
   */
  fun setLifeAccountData(data: LifeAccountItemBean.Response) {
    curLifeAccount = data
    viewBinding.tvLifeAccountName.text = data.life_account_name
    viewBinding.tvLifeAccountStatus.apply {
      when (data.life_account_auth_status) {
        Constant.STATUS_AUTH_NOT -> {
          // 未认证
          setBackgroundResource(R.drawable.shape_life_account_status_unauth)
          setTextColor(ContextCompat.getColor(context, R.color.color_333333))
          text = resources.getString(R.string.not_auth)
        }
        Constant.STATUS_AUTH_FINISH -> {
          // 已认证
          setBackgroundResource(R.drawable.shape_life_account_status_auth)
          setTextColor(ContextCompat.getColor(context, R.color.color_FF1852F1))
          text = when (data.tag.tag_key) {
            Constant.LIFE_ACCOUNT_TAG_COMMON_ENTERPRISE_AUTH -> resources.getString(R.string.common_enterprise)
            Constant.LIFE_ACCOUNT_TAG_INDIVIDUAL_BUSINESS_AUTH -> resources.getString(R.string.individual_business)
            Constant.LIFE_ACCOUNT_TAG_PERSONAL_AUTH -> resources.getString(R.string.personal_auth)
            else -> ""
          }
        }
      }
      when (data.enterprise_audit_status) {
        Constant.STATUS_AUDIT_UNDER -> {
          // 审核中
          setBackgroundResource(R.drawable.shape_life_account_status_audit)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFF26E3D))
          text = resources.getString(R.string.under_audit)
        }
        Constant.STATUS_AUDIT_REJECT -> {
          // 审核驳回
          setBackgroundResource(R.drawable.shape_life_account_status_reject)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
          text = resources.getString(R.string.auth_reject)
        }
      }
    }
  }

  /**
   * 设置生活号名称
   * @param name 修改后的生活号名称
   */
  fun setLifeAccountName(name: String) {
    viewBinding.tvLifeAccountName.text = name
  }

  override fun onClick(v: View?) {
    // 跳转生活号基本信息
    callback?.onEnterLifeAccountInfo(curLifeAccount.life_account_id)
  }

  fun setOnLifeAccountLabelCallback(callback: OnLifeAccountLabelCallback) {
    this.callback = callback
  }

  interface OnLifeAccountLabelCallback {
    /**
     * 进入生活号基本信息
     */
    fun onEnterLifeAccountInfo(lifeAccountId: String)
  }


}
