package com.xty.base.dialog

import android.content.DialogInterface
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import com.xty.base.vm.BaseVm
import com.xty.common.ReflexUtils

abstract  class BaseDialogFragment :DialogFragment(){


    protected lateinit var mView: View
    private var dismissListener: OnDismissListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getStyle()?.let {
            setStyle(STYLE_NORMAL, it)
        }
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mView = onCreateView()

        initView()
        initData()
        initListener()
        return mView
    }

    override fun onStart() {
        super.onStart()
        //设置 dialog 的宽高
        dialog?.window?.let {
            it.setBackgroundDrawable(null)
            val lp: WindowManager.LayoutParams = it.attributes
            lp.gravity = getGravity()
            lp.width = getWidth()
            lp.height = getHeight()
            if (isBGTransparent()) {
                lp.dimAmount = 0.0f
            } else {
                lp.dimAmount = 0.5f
            }

            it.attributes = lp
        }

    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        dismissListener?.onDismiss()
    }
    fun setOnDismissListener(dismissListener: OnDismissListener) {
        this.dismissListener = dismissListener
    }

    protected abstract fun onCreateView(): View
    protected open fun getStyle(): Int? = null   //弹框样式 动画
    protected open fun getGravity(): Int = Gravity.BOTTOM   //弹框位置
    protected open fun isBGTransparent() = false  //是否全透明背景

    protected open fun getHeight(): Int = WindowManager.LayoutParams.WRAP_CONTENT   //弹框最大高度
    protected open fun getWidth(): Int = WindowManager.LayoutParams.MATCH_PARENT   //弹框最大高度


    protected abstract fun initView()

    protected abstract fun initData()

    protected abstract fun initListener()
    interface OnDismissListener {
        fun onDismiss()
    }
}