package com.abbidot.tracker.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.WindowManager
import androidx.annotation.StyleRes
import androidx.appcompat.app.AppCompatDialog
import androidx.viewbinding.ViewBinding
import com.abbidot.baselibrary.util.AppUtils
import com.abbidot.tracker.R
import com.qmuiteam.qmui.alpha.QMUIAlphaButton
import com.qmuiteam.qmui.alpha.QMUIAlphaImageButton


/**
 *Created  by .yzq on 2022/1/11/011.-
 * @link
 * @dialogHeightDp 默认高度350dp
 * @description:BaseDialog
 */
abstract class BaseDialog<T : ViewBinding>
    (
    val inflater: (inflater: LayoutInflater) -> T,
    context: Context,
    dialogHeightDp: Int = 0,
    @StyleRes animationResId: Int = R.style.QMUI_Animation_Scale,
    @StyleRes themeResId: Int = R.style.my_Dialog_shadow_style
) : AppCompatDialog(context, themeResId), OnClickListener {

    protected lateinit var mViewBinding: T

    val mContext = context

    private val mAnimationResId = animationResId
    private val mDialogHeightDp = dialogHeightDp

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = inflater(layoutInflater)
        setContentView(mViewBinding.root)
        //设置Dialog动画
        window!!.setWindowAnimations(mAnimationResId)
        //设置弹窗的高度
        val layoutParams: WindowManager.LayoutParams = window!!.attributes
        if (mDialogHeightDp == 0) {
            //采用自适应高度
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
        } else {
            layoutParams.height = AppUtils.dpToPx(mDialogHeightDp)
        }
//        layoutParams.width =  WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = layoutParams

        initView()
    }

    abstract fun initView()

    /**
     * 设置多个view的监听事件
     */
    fun setOnClickListenerViews(vararg views: View?) {
        for (v in views) {
            v?.let {
                if (v is QMUIAlphaButton) {
                    //设置按下点击透明效果
                    v.setChangeAlphaWhenPress(true)
                } else if (v is QMUIAlphaImageButton) {
                    //设置按下点击透明效果
                    v.setChangeAlphaWhenPress(true)
                }
                v.setOnClickListener(this)
            }
        }
    }

    override fun onClick(v: View) {
        // 默认不实现，让子类实现
    }

    interface OnDialogOkListener {
        fun onOkClick(dialog: BaseDialog<*>)
    }

    interface OnDialogSelectListener {
        fun onSelectClick(dialog: BaseDialog<*>, any: Any)
    }

    interface OnDialogCancelListener {
        fun onCancelClick(dialog: BaseDialog<*>)
    }
}

