package com.base.appzxhy.ui.dialog

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import androidx.appcompat.app.AlertDialog
import com.base.appzxhy.R
import com.base.appzxhy.SpConstObject.scoreRate
import androidx.core.net.toUri
import com.base.appzxhy.SpConstObject.isScoreRated
import com.base.appzxhy.databinding.DialogRate1Binding
import nl.dionsegijn.konfetti.core.Angle
import nl.dionsegijn.konfetti.core.Party
import nl.dionsegijn.konfetti.core.Position
import nl.dionsegijn.konfetti.core.emitter.Emitter
import java.util.concurrent.TimeUnit

class RateDialog(
    val activity: Activity
) {
    val dialog = AlertDialog.Builder(activity).create()
    val binding = DialogRate1Binding.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.dp_300)
//        params?.width = ConstraintLayout.LayoutParams.MATCH_PARENT
//        params?.horizontalMargin = activity.resources.getDimension(R.dimen.dp_16)
//        params?.height = ConstraintLayout.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.app_name)
        val thankTip = activity.getString(R.string.thank_you_for_using_app, appName)
        binding.tvThank.text = thankTip
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }

        var score = 0
        val starList = listOf(
            binding.star1,
            binding.star2,
            binding.star3,
            binding.star4,
            binding.star5,
        )

        starList.forEachIndexed { index, view ->
            view.setOnClickListener {
                starList.forEachIndexed { index2, view2 ->
                    view2.setImageResource(if (index2 <= index) R.drawable.icon_star_pressed else R.drawable.icon_star_normal)
                }
                score = index + 1
            }
        }
        binding.tvSubmit.setOnClickListener {
            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.star5.callOnClick()

        val party = Party(
            angle = Angle.TOP,
            speed = 0f,
            maxSpeed = 30f,
            damping = 0.9f,
            spread = 360,
            colors = listOf(0xfce18a, 0xff726d, 0xf4306d, 0xb48def),
            emitter = Emitter(duration = 100, TimeUnit.MILLISECONDS).max(100),
            position = Position.Relative(0.5, 0.3)
        )
        binding.konfettiView.start(party)
    }
}