package com.gh.gamecenter.common.dialog

import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentActivity
import com.therouter.TheRouter
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.databinding.DialogAlertDefaultBinding
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.core.provider.IAppProvider

@RequiresApi(Build.VERSION_CODES.R)
class ManageExternalStoragePermissionDialogFragment : BaseDialogFragment() {
    private val mBinding by lazy { DialogAlertDefaultBinding.inflate(layoutInflater) }
    private var mCallBack: ((Boolean) -> Unit)? = null

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.run {
            titleTv.text = "请求权限"
            titleTv.gravity = Gravity.CENTER
            contentTv.text = "需要所有文件访问权限，请打开权限设置页面"

            confirmTv.setOnClickListener {
                val appProvider = TheRouter.get(IAppProvider::class.java)
                appProvider?.setDisableSplashAdTemporarily(true)
                val intent = Intent().apply {
                    action = Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
                    data = Uri.fromParts("package", requireContext().packageName, null)
                }
                requireActivity().startActivityForResult(intent, REQUEST_CODE)
            }
            cancelTv.setOnClickListener {
                mCallBack?.invoke(false)
                dismissAllowingStateLoss()
            }
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        AppExecutor.uiExecutor.executeWithDelay({
            val appProvider = TheRouter.get(IAppProvider::class.java)
            appProvider?.setDisableSplashAdTemporarily(false)
        }, 100)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && Environment.isExternalStorageManager()) {
            mCallBack?.invoke(true)
            dismissAllowingStateLoss()
        }
    }

    companion object {
        const val REQUEST_CODE = 1000

        @JvmStatic
        fun show(activity: FragmentActivity, callback: (Boolean) -> Unit) {
            ManageExternalStoragePermissionDialogFragment().apply {
                mCallBack = callback
            }.show(
                activity.supportFragmentManager,
                ManageExternalStoragePermissionDialogFragment::class.java.name
            )
        }
    }
}