package rowedto.jockeyedto.sheetedto.balsamedto.formaledto.orientationedto

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.SwopedtoBinding
import rowedto.jockeyedto.sheetedto.raceedto.pavementedto.Stewardedto

object Prematureedto {

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

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

            } else {
                return
            }
        }


        val binding = SwopedtoBinding.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.overchargeedto)
        binding.indirectedto.text = "Thank you for using $appName"

        listOf(
            binding.enthusiasmedto,
            binding.suiteedto,
            binding.incongruousedto,
            binding.consciousedto,
            binding.failededto
        ).forEachIndexed { index, it ->
            it.setOnClickListener {
                setStar(binding, index)
            }
        }
        binding.checkedto.setOnClickListener {
            dialog.dismiss()
        }
        binding.retreatedto.setOnClickListener {
            if (star >= 5) {
                Stewardedto.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 {
            Stewardedto.getInstance().put("isRated", true)
        }
    }

    private var star = 5
    private fun setStar(binding: SwopedtoBinding, star: Int) {
        this.star = star + 1
        if (this.star < 3) {
            binding.retireeedto.setImageResource(R.mipmap.fluedto)
        } else {
            binding.retireeedto.setImageResource(R.mipmap.embarkedto)
        }
        listOf(
            binding.enthusiasmedto,
            binding.suiteedto,
            binding.incongruousedto,
            binding.consciousedto,
            binding.failededto
        ).forEachIndexed { index, it ->
            it.setImageResource(
                if (index <= star) {
                    R.mipmap.risingedto
                } else {
                    R.mipmap.consideredto
                }
            )
        }
    }
}