package importantedto.maidedto.witnessedto.nationalismedto

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import com.base.datarecovery.R
import com.base.datarecovery.databinding.SuddenlyedtoBinding
import importantedto.gallonedto.Waitedto

object Yellowedto {

    @SuppressLint("SetTextI18n")
    fun Context.showRateStarPopDialog(mustShow: Boolean = false) {

        if (!mustShow) {
            val n = Waitedto.getInstance().getInt("RateNumbers", 0)
            Waitedto.getInstance().put("RateNumbers", n + 1)
            if (Waitedto.getInstance().getBoolean("isRated", false)) {
                return
            }
            if (n > 0 && n % 3 == 0) {

            } else {
                return
            }
        }


        val binding = SuddenlyedtoBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)
        dialog.show()
//        dialog.window?.setDimAmount(1f)
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        val appName = resources.getString(R.string.kettleedto)
        binding.assaultedto.text = "Thank you for using $appName"

        listOf(
            binding.passageedto,
            binding.fleetedto,
            binding.potteryedto,
            binding.copeedto,
            binding.favouredto
        ).forEachIndexed { index, it ->
            it.setOnClickListener {
                setStar(binding, index)
            }
        }
        binding.screamedto.setOnClickListener {
            dialog.dismiss()
        }
        binding.subjectedto.setOnClickListener {
            if (star >= 5) {
                Waitedto.getInstance().put("isRated", true)
                try {
                    val uri =
                        Uri.parse("https://play.google.com/store/apps/details?id=" + this.packageName)
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    this.startActivity(intent)
                } catch (_: Exception) {
                }
            }
            dialog.dismiss()
        }

        dialog.setOnDismissListener {
            Waitedto.getInstance().put("isRated", true)
        }
    }

    private var star = 5
    private fun setStar(binding: SuddenlyedtoBinding, star: Int) {
        this.star = star + 1
        if (this.star < 3) {
            binding.evaluateedto.setImageResource(R.mipmap.staredto)
        } else {
            binding.evaluateedto.setImageResource(R.mipmap.evidenceedto)
        }
        listOf(
            binding.passageedto,
            binding.fleetedto,
            binding.potteryedto,
            binding.copeedto,
            binding.favouredto
        ).forEachIndexed { index, it ->
            it.setImageResource(
                if (index <= star) {
                    R.mipmap.shootedto
                } else {
                    R.mipmap.preferenceedto
                }
            )
        }
    }
}