package com.base.locationsharewhite.ui.views

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.base.locationsharewhite.databinding.DialogAllowAllTimeLocationBinding

object LocationPermissionDialog {


    fun Context.showLocationAllowAllTimeDialog(
        setAction: (() -> Unit)? = null,
        dismissAction: (() -> Unit)? = null
    ): AlertDialog {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogAllowAllTimeLocationBinding.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.BOTTOM
        params?.y = 50
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        val stepText1 = "1. Go to Settings>Location Share>Permissions, then select the location"
        val spannableString1 = SpannableString(stepText1)
        val spanText1 = "Settings>Location Share>Permissions"
        spannableString1.setSpan(
            ForegroundColorSpan(Color.parseColor("#03B887")),
            stepText1.indexOf(spanText1),
            stepText1.indexOf(spanText1) + spanText1.length,
            SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.tvStep1.text = spannableString1

        val stepText2 = "2. Click 'Always Allow'"
        val spannableString2 = SpannableString(stepText2)
        val spanText2 = "'Always Allow'"
        spannableString2.setSpan(
            ForegroundColorSpan(Color.parseColor("#03B887")),
            stepText2.indexOf(spanText2),
            stepText2.indexOf(spanText2) + spanText2.length,
            SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        binding.tvStep2.text = spannableString2

        binding.tvSettings.setOnClickListener {
            dialog.dismiss()
            setAction?.invoke()
        }
        dialog.setOnDismissListener {
            dismissAction?.invoke()
        }

        return dialog
    }
}