package com.yidian.bcommon.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewTreeObserver
import android.view.WindowManager
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.yidian.bcommon.R
import com.yidian.bcommon.databinding.ViewCommonTopBarBinding
import com.yidian.utils.DensityUtil
import com.yidian.xpage.XPageManager

/**
 * author: yinjiacheng
 * date: 6/14/21 12:58 AM
 * description: 顶部栏
 */
class CommonTopBarView @JvmOverloads constructor(
  context: Context,
  attrs: AttributeSet? = null,
  defStyle: Int = 0
) : ConstraintLayout(context, attrs, defStyle), View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {

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

  /**
   * 返回按钮点击回调
   */
  private var backCallback: OnCommonTopBarBackCallback? = null

  /**
   * 操作按钮点击回调
   */
  private var actionCallback: OnCommonTopBarActionCallback? = null

  /**
   * StatusBar颜色
   */
  private var statusBarColor: Int = 0

  init {
    val typeArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTopBarView)
    // 设置标题文字
    viewBinding.tvTitle.text = typeArray.getText(R.styleable.CommonTopBarView_CommonTopBarView_title_text) ?: ""
    // 设置标题文字颜色
    viewBinding.tvTitle.setTextColor(
      typeArray.getColor(
        R.styleable.CommonTopBarView_CommonTopBarView_title_text_color,
        ContextCompat.getColor(context, R.color.color_333333)
      )
    )
    // 设置操作文字
    val action = typeArray.getText(R.styleable.CommonTopBarView_CommonTopBarView_action_text) ?: ""
    viewBinding.tvAction.isVisible = action.isNotEmpty()
    viewBinding.tvAction.text = action
    // 设置背景色
    setBackgroundColor(
      typeArray.getColor(
        R.styleable.CommonTopBarView_CommonTopBarView_bg_color,
        ContextCompat.getColor(context, R.color.transparent)
      )
    )
    // 设置返回按钮图标
    viewBinding.ivBack.setImageResource(
      typeArray.getResourceId(
        R.styleable.CommonTopBarView_CommonTopBarView_back_img,
        R.drawable.icon_back
      )
    )
    // 设置横向内边距
    val horizontalPadding =
      typeArray.getDimensionPixelOffset(R.styleable.CommonTopBarView_CommonTopBarView_horizontal_padding, DensityUtil.dp2px(20F))
    setPadding(horizontalPadding, 0, horizontalPadding, 0)
    // 暂存StatusBar背景色
    statusBarColor = typeArray.getColor(
      R.styleable.CommonTopBarView_CommonTopBarView_statusBar_color,
      ContextCompat.getColor(context, R.color.transparent)
    )
    typeArray.recycle()

    viewBinding.ivBack.setOnClickListener(this)
    viewBinding.tvAction.setOnClickListener(this)
    viewTreeObserver.addOnGlobalLayoutListener(this)
  }

  /**
   * 设置标题
   */
  fun setTitle(title: String) {
    viewBinding.tvTitle.text = title
  }

  /**
   * 设置标题文字颜色
   */
  fun setTitleTextColor(@ColorInt color: Int) {
    viewBinding.tvTitle.setTextColor(color)
  }

  /**
   * 设置背景色
   */
  fun setRootBackgroundColor(@ColorInt color: Int) {
    setBackgroundColor(color)
  }

  /**
   * 设置状态栏颜色
   */
  fun setStatusBarColor(@ColorInt color: Int) {
    updateStatusBarColor(color)
  }

  /**
   * 设置返回按钮图标
   */
  fun setBackImageResource(@DrawableRes resource: Int) {
    viewBinding.ivBack.setImageResource(resource)
  }

  /**
   * 设置横向内边距
   * @param padding 单位: px
   */
  fun setRootHorizontalPadding(padding: Int) {
    setPadding(padding, 0, padding, 0)
  }

  /**
   * 设置返回按钮是否可见
   */
  fun setBackVisibility(isVisible: Boolean) {
    viewBinding.ivBack.isVisible = isVisible
  }

  /**
   * 设置操作按钮是否可见
   */
  fun setActionVisibility(isVisible: Boolean) {
    viewBinding.tvAction.isVisible = isVisible
  }

  /**
   * 设置操作按钮是否可用
   */
  fun setActionEnable(isEnable: Boolean) {
    viewBinding.tvAction.isEnabled = isEnable
    viewBinding.tvAction.alpha = if (isEnable) 1f else 0.4f
  }

  override fun onClick(v: View?) {
    if (v?.id == R.id.iv_back) {
      // 若没有设置返回点击回调则默认pop
      backCallback?.onDoBack() ?: XPageManager.pop(null)
    } else if (v?.id == R.id.tv_action) {
      actionCallback?.onDoAction()
    }
  }

  /**
   * 设置回调
   * @param backCallback 返回按钮点击回调
   * @param actionCallback 操作按钮点击回调
   */
  fun setOnCommonTopBarCallback(backCallback: OnCommonTopBarBackCallback?, actionCallback: OnCommonTopBarActionCallback?) {
    this.backCallback = backCallback
    this.actionCallback = actionCallback
  }

  interface OnCommonTopBarActionCallback {
    /**
     * 操作按钮回调
     */
    fun onDoAction()
  }

  interface OnCommonTopBarBackCallback {
    /**
     * 返回按钮回调
     */
    fun onDoBack()
  }

  override fun onGlobalLayout() {
    // 设置StatusBar背景色
    if (statusBarColor != 0) {
      updateStatusBarColor(statusBarColor)
    }
    viewTreeObserver.removeOnGlobalLayoutListener(this)
  }

  private fun updateStatusBarColor(@ColorInt color: Int) {
    val context = context as Activity
    context.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    context.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    context.window.statusBarColor = color
  }
}
