package com.yidian.shenghuoquan.newscontent.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import com.yidian.bcommon.extensions.hide
import com.yidian.bcommon.extensions.show
import com.yidian.nightmode.widget.YdConstraintLayout
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.databinding.ViewLifeAccountFaceAuthBinding

/**
 * author: yinjiacheng
 * date: 6/6/21 3:57 PM
 * description: 生活号认证 人脸认证
 */
class LifeAccountFaceAuthView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : YdConstraintLayout(context, attrs, defStyle), View.OnClickListener {

  private val viewBinding = ViewLifeAccountFaceAuthBinding.bind(
    View.inflate(
      getContext(), R.layout.view_life_account_face_auth, this
    )
  )

  private var callback: OnLifeAccountFaceAuthCallback? = null

  init {
    viewBinding.tvFaceAuthStart.setOnClickListener(this)
  }

  /**
   * 人脸验证结果UI
   * @param result true为验证通过 false为验证未通过
   */
  fun showFaceAuthResult(result: Boolean) {
    if (result) {
      viewBinding.tvFaceAuthResult.show()
      viewBinding.tvFaceAuthStart.hide()
      viewBinding.tvFaceAuthErrorTips.hide()
    } else {
      viewBinding.tvFaceAuthResult.hide()
      viewBinding.tvFaceAuthStart.text = resources.getText(R.string.face_auth_fail)
      viewBinding.tvFaceAuthStart.setTextColor(Color.parseColor("#FFFF3A3A"))
      viewBinding.tvFaceAuthErrorTips.show()
      viewBinding.tvFaceAuthErrorTips.text = resources.getText(R.string.face_auth_error_tips)
    }
  }

  override fun onClick(v: View?) {
    // 去验证
    if (v?.id == R.id.tv_face_auth_start) {
      callback?.onStartFaceAuth()
    }
  }

  fun setOnLifeAccountFaceAuthCallback(callback: OnLifeAccountFaceAuthCallback) {
    this.callback = callback
  }

  interface OnLifeAccountFaceAuthCallback {
    /**
     * 进行人脸验证
     */
    fun onStartFaceAuth()
  }

}
