package com.yidian.shenghuoquan.newscontent.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.yidian.common.base.BaseDialog
import com.yidian.shenghuoquan.newscontent.R
import com.yidian.shenghuoquan.newscontent.bean.HintOptionBean
import com.yidian.shenghuoquan.newscontent.databinding.DialogHintBinding
import com.yidian.utils.DensityUtil


/**
 * author: yinjiacheng
 * date: 6/8/21 12:34 AM
 * description: 提示dialog
 */
class HintDialog(
  context: Context,
  val content: String,
  val options: ArrayList<HintOptionBean>,
  val callback: OnHintOptionCallback?
) : BaseDialog<DialogHintBinding>(context), View.OnClickListener {

  private var leftBtnColor: Int = R.color.color_333333
  private var rightBtnColor: Int = R.color.color_333333

  override fun createViewBinding(): DialogHintBinding {
    return DialogHintBinding.inflate(layoutInflater)
  }

  @SuppressLint("ResourceAsColor")
  override fun initView() {
    viewBinding.tvContent.text = content
    when (options.size) {
      1 -> {
        viewBinding.btnRight.text = options[0].option
        viewBinding.btnLeft.isVisible = false
        viewBinding.viewVerticalDivider.isVisible = false
        viewBinding.btnRight.setTextColor(rightBtnColor)
      }
      2 -> {
        viewBinding.btnLeft.text = options[0].option
        viewBinding.btnRight.text = options[1].option
        viewBinding.btnLeft.setTextColor(leftBtnColor)
        viewBinding.btnRight.setTextColor(rightBtnColor)
      }
    }
  }

  override fun initListener() {
    viewBinding.btnLeft.setOnClickListener(this)
    viewBinding.btnRight.setOnClickListener(this)
  }

  override fun init(savedInstanceState: Bundle?) {
    super.init(savedInstanceState)
    setCanceledOnTouchOutside(false)
  }

  override fun show() {
    super.show()
    val attrs = window?.attributes
    attrs?.width = DensityUtil.dp2px(269f)
    attrs?.height = WindowManager.LayoutParams.WRAP_CONTENT
    window?.attributes = attrs
  }

  override fun onClick(v: View?) {
    if (v?.id == R.id.btn_left) {
      callback?.onLeftOption()
    } else if (v?.id == R.id.btn_right) {
      callback?.onRightOption()
    }
    // 点击左右均要关闭弹框
    dismiss()
  }

  interface OnHintOptionCallback {
    /**
     * 左操作项
     */
    fun onLeftOption()

    /**
     * 右操作项
     */
    fun onRightOption()
  }

  /***
   * 设置左侧按钮颜色
   */
  fun setLeftBtnColor(@ColorInt color: Int): HintDialog {
    leftBtnColor = color
    return this
  }

  /***
   * 设置右侧按钮颜色
   */
  fun setRightBtnColor(@ColorInt color: Int): HintDialog {
    rightBtnColor = color
    return this
  }

}
