package com.convenient.android.common.base.view

import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.convenient.android.common.extension.showAllowingStateLoss

/**
 * @classname:BaseDialogFragment
 * @author: LiuXiaoLong
 * @date: 2022/7/27
 * description:
 */
open class BaseDialogFragment : DialogFragment() {

    /**
     * 点击回退是否可以关闭弹窗
     */
    open var mCanKeyBack = true


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setOnKeyListener { dialog, keyCode, event ->
            if ((keyCode == KeyEvent.KEYCODE_BACK) or (keyCode == KeyEvent.KEYCODE_ESCAPE) and mCanKeyBack){
                dismiss()
                return@setOnKeyListener true
            }
            return@setOnKeyListener false
        }
    }

    fun setDimAmount(dimAmount : Float){
        dialog?.window?.attributes?.dimAmount = dimAmount
    }

    /**
     * @param ：[manager, tag]
     * @return : void
     * @methodName ：onShow created by luozhipeng on 12/12/17 11:07.
     * @description ：自定义重写了父类的方法show
     */
    fun onShow(manager : FragmentManager, tag : String){
        showAllowingStateLoss(manager, tag)
    }

}