package rallyedto.farewelledto

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.FilmedtoBinding
import rallyedto.talledto.blewedto.Pianoedto

object Hutedto {

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

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

            } else {
                return
            }
        }


        val binding = FilmedtoBinding.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.paralleledto)
        binding.momentedto.text = "Thank you for using $appName"

        listOf(
            binding.chinedto,
            binding.photographyedto,
            binding.riveredto,
            binding.gettingedto,
            binding.noddededto
        ).forEachIndexed { index, it ->
            it.setOnClickListener {
                setStar(binding, index)
            }
        }
        binding.incongruousedto.setOnClickListener {
            dialog.dismiss()
        }
        binding.alledto.setOnClickListener {
            if (star >= 5) {
                Pianoedto.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 {
            Pianoedto.getInstance().put("isRated", true)
        }
    }

    private var star = 5
    private fun setStar(binding: FilmedtoBinding, star: Int) {
        this.star = star + 1
        if (this.star < 3) {
            binding.speakeredto.setImageResource(R.mipmap.bracketedto)
        } else {
            binding.speakeredto.setImageResource(R.mipmap.mainedto)
        }
        listOf(
            binding.chinedto,
            binding.photographyedto,
            binding.riveredto,
            binding.gettingedto,
            binding.noddededto
        ).forEachIndexed { index, it ->
            it.setImageResource(
                if (index <= star) {
                    R.mipmap.chestedto
                } else {
                    R.mipmap.authoredto
                }
            )
        }
    }
}