package rememberic

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import com.easy.clean.R
import tooic.fatalityic.Runawayic.scoreRate
import com.easy.clean.databinding.MythologicalicBinding
import androidx.core.net.toUri
import tooic.fatalityic.Runawayic.isScoreRated

class Subsequentic(
    val activity: Activity
) {
    val dialog = AlertDialog.Builder(activity).create()
    val binding = MythologicalicBinding.inflate(LayoutInflater.from(activity))

    var action: (() -> Unit)? = null

    fun showDialog(isMust: Boolean) {

        if (!isMust) {
            if (isScoreRated && scoreRate == 5)
                return
        }
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        val params = dialog.window?.attributes
//        params?.width = activity.resources.getDimensionPixelSize(R.dimen.suffereric)
//        params?.width = FrameLayout.LayoutParams.WRAP_CONTENT
//        params?.height = FrameLayout.LayoutParams.WRAP_CONTENT
//        params?.gravity = Gravity.CENTER
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        dialog.show()

        val appName = activity.getString(R.string.overrodeic)
        val thankTip = activity.getString(R.string.townic, appName)
        binding.variedic.text = thankTip
        binding.traditionalistic.setOnClickListener {
            dialog.dismiss()
        }

        var score = 0
        val starList = listOf(
            binding.unconstitutionalic,
            binding.knockeric,
            binding.revileic,
            binding.modernityic,
            binding.shooic,
        )

        starList.forEachIndexed { index, view ->
            view.setOnClickListener {
                starList.forEachIndexed { index2, view2 ->
                    view2.setImageResource(if (index2 <= index) R.drawable.liquidateic else R.drawable.canoeic)

                }
                score = index + 1
            }
        }
        binding.gyroscopeic.setOnClickListener {
            if (score == 5) {
                scoreRate = score
            }
            try {
                val url = "https://play.google.com/store/apps/details?id=${activity.packageName}"
                activity.startActivity(Intent(Intent.ACTION_VIEW, url.toUri()))
                isScoreRated = true
            } catch (_: Exception) {
            }
            dialog.dismiss()
        }
        binding.shooic.callOnClick()

    }
}