package com.grkj.iscs.view.base

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.view.Window
import androidx.viewbinding.ViewBinding

/**
 * Dialog基类
 *
 * @param mGravity dialog显示位置
 * @param isCanCancel 是否允许点击外部取消
 */
abstract class BaseDialog<T : ViewBinding?>(
    context: Context,
    private var mWidth: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
    private var mHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
    private var mGravity: Int = Gravity.CENTER,
    private var isCanCancel: Boolean = true
) : Dialog(context) {

    protected var mBinding: T? = null
    abstract val viewBinding: T

    init {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        mBinding = viewBinding
        setContentView(mBinding!!.root)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window?.setBackgroundDrawableResource(android.R.color.transparent)
        val layoutParams = window?.attributes
        layoutParams?.width = mWidth
        layoutParams?.height = mHeight
        window?.setGravity(mGravity)
        window?.attributes = layoutParams

        setCanceledOnTouchOutside(isCanCancel)

        initView()
    }

    abstract fun initView()
}