package com.develop.food.base.ui

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.KeyEvent
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.fragment.app.FragmentManager

abstract class FullScreenTransparentDialog : DialogFragment() {
    private var isShow = false
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog?.window?.decorView?.setPadding(0, 0, 0, 0)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        dialog?.window?.apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setLayout(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT
            )
        }
        dialog?.setOnKeyListener { dialogInterface, i, keyEvent ->
            i == KeyEvent.KEYCODE_BACK
        }

    }

    class CloseDialogEvent {}

    fun removeSelf() {
        if (isShow && isAdded) {
            isShow = false
            dismiss()
        }
        isShow = false;
    }


    fun showDialog(fragmentManager: FragmentManager, tag: String) {
        if (!isShow && !fragmentManager.isDestroyed) {
            isShow = true
            show(fragmentManager, tag)
        }
    }
}