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.bcommon.bean.LifeAccountItemBean
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ViewLifeAccountTitleBinding
import com.yidian.shenghuoquan.bmanagercenter.manager.LifeAccountManager

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

  private val viewBinding = ViewLifeAccountTitleBinding.bind(
    View.inflate(
      getContext(),
      R.layout.view_life_account_title,
      this
    )
  )

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

  private var callback: OnLifeAccountTitleCallback? = null

  init {
    viewBinding.ivLifeAccountDetail.setOnClickListener(this)
    viewBinding.tvLifeAccountLabel.setOnClickListener(this)
  }

  /**
   * 设置生活号信息
   * @param data 生活号详情
   */
  fun setLifeAccountData(data: LifeAccountItemBean.Response) {
    curLifeAccount = data
    viewBinding.tvLifeAccountName.text = data.life_account_name
    viewBinding.tvLifeAccountLabel.apply {
      text = data.tag.tag_text
      when (LifeAccountManager.getLifeAccountTag(data.tag.tag_key)) {
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_COMMON_ENTERPRISE_AUTH, LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_INDIVIDUAL_BUSINESS_AUTH, LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_PERSONAL_AUTH -> {
          // 个体工商户、普通企业、个人
          setBackgroundResource(R.drawable.shape_life_account_status_auth)
          setTextColor(ContextCompat.getColor(context, R.color.color_FF1852F1))
        }
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_NOT_AUTH -> {
          // 未认证
          setBackgroundResource(R.drawable.shape_life_account_status_unauth)
          setTextColor(ContextCompat.getColor(context, R.color.color_333333))
        }
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_AUDIT_UNDER -> {
          // 审核中
          setBackgroundResource(R.drawable.shape_life_account_status_audit)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFF26E3D))
        }
        LifeAccountManager.LifeAccountTag.LIFE_ACCOUNT_TAG_AUTH_REJECT -> {
          // 审核驳回
          setBackgroundResource(R.drawable.shape_life_account_status_reject)
          setTextColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
        }
        else -> {
          setBackgroundResource(R.drawable.shape_life_account_status_auth)
          setTextColor(ContextCompat.getColor(context, R.color.color_FF1852F1))
        }
      }
    }
  }

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

  override fun onClick(v: View?) {
    if (v?.id == R.id.iv_life_account_detail) {
      // 跳转生活号基本信息
      callback?.onEnterLifeAccountInfo(curLifeAccount.life_account_id)
    } else if (v?.id == R.id.tv_life_account_label) {
      // 跳转生活号认证信息
      callback?.onEnterLifeAccountAuthInfo(curLifeAccount.life_account_id)
    }
  }

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

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

    /**
     * 进入生活号认证信息
     */
    fun onEnterLifeAccountAuthInfo(lifeAccountId: String)
  }


}
