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 androidx.core.view.isVisible
import com.yidian.shenghuoquan.bmanagercenter.R
import com.yidian.shenghuoquan.bmanagercenter.databinding.ViewMerchantInfoBinding

/**
 * author: yinjiacheng
 * date: 6/11/21 1:15 PM
 * description:
 */
class MerchantInfoView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : ConstraintLayout(context, attrs, defStyle), View.OnClickListener {

  private val viewBinding by lazy {
    ViewMerchantInfoBinding.bind(View.inflate(getContext(), R.layout.view_merchant_info, this))
  }

  private var listener: OnMerchantInfoViewListener? = null

  init {
    val typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.MerchantInfoView)
    viewBinding.tvTitle.text =
      typeArray.getString(R.styleable.MerchantInfoView_MerchantInfoView_name_text)
    typeArray.recycle()
    viewBinding.ivNext.setOnClickListener(this)
    viewBinding.tvAuth.setOnClickListener(this)
  }

  /**
   * 填充信息
   */
  fun fillContent(content: String?) {
    viewBinding.tvContent.text = content
  }

  /**
   * 显示详情入口
   * 修改生活号名称、查看认证信息
   */
  fun showDetailEntry() {
    viewBinding.ivNext.isVisible = true
  }

  /**
   * 显示认证入口
   * 未认证、升级认证、修改认证
   */
  fun showAuthEntry(status: LifeAccountAuthStatus) {
    viewBinding.tvAuth.isVisible = true
    when (status) {
      LifeAccountAuthStatus.NOT_AUTH -> {
        viewBinding.tvAuth.text = resources.getText(R.string.immediate_auth)
        viewBinding.tvAuth.setTextColor(ContextCompat.getColor(context, R.color.color_FFFFFFFF))
        viewBinding.tvAuth.setBackgroundResource(R.drawable.shape_bg_common_btn)
      }
      LifeAccountAuthStatus.UPGRADE_AUTH -> {
        viewBinding.tvAuth.text = resources.getText(R.string.upgrade_enterprise_merchant)
        viewBinding.tvAuth.setTextColor(ContextCompat.getColor(context, R.color.color_FFFFFFFF))
        viewBinding.tvAuth.setBackgroundResource(R.drawable.shape_bg_common_btn)
      }
      LifeAccountAuthStatus.MODIFY_AUTH -> {
        viewBinding.tvAuth.text = resources.getText(R.string.modify_auth)
        viewBinding.tvAuth.setTextColor(ContextCompat.getColor(context, R.color.color_FFFF3A3A))
        viewBinding.tvAuth.setBackgroundResource(R.drawable.shape_bg_modify_auth_btn)
      }
    }
  }

  override fun onClick(v: View?) {
    when (v?.id) {
      R.id.iv_next -> {
        // 进入生活号名称修改页或认证详情页
        listener?.onEnterDetail(this)
      }
      R.id.tv_auth -> {
        // 进入认证流程
        listener?.onEnterAuth(this)
      }
    }
  }

  fun setOnMerchantInfoViewListener(listener: OnMerchantInfoViewListener) {
    this.listener = listener
  }

  interface OnMerchantInfoViewListener {
    /**
     * 进入详情
     */
    fun onEnterDetail(from: MerchantInfoView)

    /**
     * 进入认证
     */
    fun onEnterAuth(from: MerchantInfoView)
  }

  /**
   * 生活号认证状态
   */
  enum class LifeAccountAuthStatus {
    // 未认证
    NOT_AUTH,

    // 升级认证
    UPGRADE_AUTH,

    // 修改认证
    MODIFY_AUTH
  }


}
