package com.test.basd.fastcleanerjunk.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.view.Gravity
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.view.updatePadding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.SPUtils
import com.test.basd.fastcleanerjunk.R
import com.test.basd.fastcleanerjunk.databinding.PopLayoutStarBinding

class RateStarPop(private val activity: Activity) : PopupWindow() {
    companion object {
        fun show(activity: Activity) {
            val n = SPUtils.getInstance().getInt("RateNums", 0)
            SPUtils.getInstance().put("RateNums", n + 1)
            if (SPUtils.getInstance().getBoolean("isRated", false)) {
                return
            }
            if (n > 0 && n % 8 == 0) {
                RateStarPop(activity).show()
            } else {
                return
            }

        }
    }

    init {
        width = ViewGroup.LayoutParams.MATCH_PARENT
        height = ViewGroup.LayoutParams.MATCH_PARENT
        isOutsideTouchable = false
        isFocusable = true
        isClippingEnabled = false
    }

    private val binding by lazy {
        PopLayoutStarBinding.inflate(activity.layoutInflater)
    }

    @SuppressLint("ClickableViewAccessibility")
    fun show() {
        contentView = binding.root
        activity.window.decorView.post {
            binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
            BarUtils.setStatusBarLightMode(activity, false)
            showAtLocation(activity.window.decorView, Gravity.CENTER, 0, 0)
        }

        listOf(
            binding.idStar1,
            binding.idStar2,
            binding.idStar3,
            binding.idStar4,
            binding.idStar5
        ).forEachIndexed { index, it ->
            it.setOnClickListener {
                setStar(index)
            }
        }

        binding.idClose.setOnClickListener {
            dismiss()
        }

        binding.idSubmit.setOnClickListener {
            if (star > 0) {
                if (star >= 5) {
                    SPUtils.getInstance().put("isRated", true)
                    try {
                        val uri =
                            Uri.parse("https://play.google.com/store/apps/details?id=" + activity.packageName)
                        val intent = Intent(Intent.ACTION_VIEW, uri)
                        activity.startActivity(intent)
                    } catch (_: Exception) {
                    }
                }
                dismiss()
            }

        }
    }

    private var star = 5

    private fun setStar(star: Int) {
        this.star = star
        listOf(
            binding.idStar1,
            binding.idStar2,
            binding.idStar3,
            binding.idStar4,
            binding.idStar5
        ).forEachIndexed { index, it ->
            it.setImageResource(
                if (index <= star) {
                    R.mipmap.xing_s2
                } else {
                    R.mipmap.xing_n2
                }
            )

        }
    }

    override fun dismiss() {
        super.dismiss()
        BarUtils.setStatusBarLightMode(activity, true)
    }
}