package com.audio.record.screen.test.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.audio.record.screen.test.databinding.DialogPermissionBinding
import com.audio.record.screen.test.tool.Permission
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class DialogPermission(private var mClickType: (type: Int) -> Unit) : BottomSheetDialogFragment() {
    private lateinit var vb: DialogPermissionBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = DialogPermissionBinding.inflate(layoutInflater)
        init()
        return vb!!.root
    }

    override fun onStart() {
        super.onStart()
        val dialog = dialog
        if (dialog != null) {
            dialog.setCanceledOnTouchOutside(false)
            val window = dialog.window
            if (window != null) {
                window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                window.setLayout(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            }
        }
    }

    private fun init() {
        vb.run {

            enableTvBall.setOnClickListener {
                mClickType.invoke(type_ball)
            }
            enableTvNotification.setOnClickListener {
                mClickType.invoke(type_notification)
            }
        }

    }

    override fun onResume() {
        super.onResume()
        refreshUI()
    }

    private fun refreshUI() {
        var ovalay = false
        var notification = false
        Permission.checkOvalApp(requireContext()) {
            vb.layoutBall.isVisible = !it
            ovalay = it
            if(ovalay&&notification){
                dismiss()
            }
        }
        Permission.checkNotification(requireContext()) {
            vb.layoutNotification.isVisible = !it
            notification = it
            if(ovalay&&notification){
                dismiss()
            }
        }

    }

    companion object {
        const val type_ball = 0
        const val type_notification = 1
    }
}