package com.base.pdfreaderallpdfreader.ui.view

import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.provider.Settings
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.ads.admob.AdmobNativeUtils
import com.base.pdfreaderallpdfreader.databinding.DialogAppExitBinding
import com.base.pdfreaderallpdfreader.databinding.DialogNotificationEnableBinding
import com.base.pdfreaderallpdfreader.databinding.DialogStoragePermissionBinding
import com.base.pdfreaderallpdfreader.utils.ActivityLauncher
import com.base.pdfreaderallpdfreader.utils.PermissionUtils.requestStoragePermission
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

object MainDialog {


    fun Context.showStoragePermission(
        launcher: ActivityLauncher,
        launcherAction: ((flag: Boolean) -> Unit)? = null,
        jumpAction: (() -> Unit)? = null,
        dismissAction: (() -> Unit)? = null,
    ): BottomSheetDialog {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogStoragePermissionBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        binding.tvAllow.setOnClickListener {
            dialog.dismiss()
            jumpAction?.invoke()
            requestStoragePermission(launcher) {
                launcherAction?.invoke(it)
            }
        }
        dialog.setOnDismissListener {
            dismissAction?.invoke()
        }
        val text = "To read and edit more documents on your device, please allow PDF Reader to access all your document files"
        val redText = "PDF Reader"
        val spannableString = SpannableString(text)
        val startIndex = text.indexOf(redText)
        val endIndex = startIndex + redText.length

        spannableString.setSpan(
            ForegroundColorSpan(Color.parseColor("#6DA9FD")),
            startIndex,
            endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.tvTip.text = spannableString

        return dialog

    }


    fun Activity.showAppExitDialog() {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogAppExitBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        AdmobNativeUtils.showNativeAd(this, binding.flAd, R.layout.layout_admob_app_exit)

        binding.cardNo.setOnClickListener {
            finish()
        }
        binding.cardYes.setOnClickListener {
            dialog.dismiss()
        }
    }

    fun Activity.showNotificationDialog(launcher: ActivityLauncher) {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogNotificationEnableBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val params = dialog.window?.attributes
        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_345)
        params?.height = LinearLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.CENTER
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        binding.tvOk.setOnClickListener {
            dialog.dismiss()
            val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
                putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
            }
            launcher.launch(intent)
        }
        binding.tvNo.setOnClickListener {
            dialog.dismiss()
        }
        binding.flClose.setOnClickListener {
            dialog.dismiss()
        }

    }
}