package com.base.filerecoveryrecyclebin.view

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.base.filerecoveryrecyclebin.activity.MainActivity
import com.base.filerecoveryrecyclebin.databinding.DialogPermissionBottomsheetBinding
import com.base.filerecoveryrecyclebin.help.ActivityLauncher
import com.base.filerecoveryrecyclebin.help.PermissionHelp.requestStorePermission
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

object PermissionDialog {
    private val TAG = "PermissionDialog"
    fun Context.showPermissionBottomSheet(launcher: ActivityLauncher, dismissAction: () -> Unit): BottomSheetDialog {
        val dialog = BottomSheetDialog(this)
        val binding = DialogPermissionBottomsheetBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
//        dialog.setCanceledOnTouchOutside(false)
//        dialog.setCancelable(false)
        dialog.show()

        val parentView = binding.root.parent as View
        BottomSheetBehavior.from(parentView).state = BottomSheetBehavior.STATE_EXPANDED

        binding.tvOpen.setOnClickListener {
            requestStorePermission(launcher, result = {
                if (it) {
                    dialog.dismiss()
                }
            })
        }
        dialog.setOnDismissListener {
            LogEx.logDebug(TAG, "dismissAction 2")
            dismissAction.invoke()
        }
        return dialog

    }

}