package com.base.pdfreaderallpdfreader.ui.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.databinding.DialogRateStarBinding
import com.base.pdfreaderallpdfreader.utils.AppPreferences
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

object RateDialog {

    fun Context.showRateDialog(mustShow: Boolean = false) {
        if (!mustShow) {
            if (AppPreferences.getInstance().getBoolean("isRated", false)) {
                return
            }
        }

        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val binding = DialogRateStarBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED


        listOf(
            binding.iv1,
            binding.iv2,
            binding.iv3,
            binding.iv4,
            binding.iv5
        ).forEachIndexed { index, it ->
            it.setOnClickListener {
                setStar(binding, index)
            }
        }
        binding.tvSubmit.setOnClickListener {
            if (star >= 5) {
                AppPreferences.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 {
            AppPreferences.getInstance().put("isRated", true)
        }
    }

    private var star = 5

    @SuppressLint("SetTextI18n")
    private fun setStar(binding: DialogRateStarBinding, star: Int) {
        val context = binding.root.context
        this.star = star + 1
//        if (this.star < 3) {
//            binding.ivFace.setImageResource(R.mipmap.pingfeniconk)
//        } else {
//            binding.ivFace.setImageResource(R.mipmap.pingfenicon)
//        }
        listOf(
            binding.iv1,
            binding.iv2,
            binding.iv3,
            binding.iv4,
            binding.iv5
        ).forEachIndexed { index, it ->
            it.setImageResource(
                if (index <= star) {
                    R.mipmap.pdf_xing_s
                } else {
                    R.mipmap.pdf_xing_n
                }
            )
        }
        when (this.star) {
            in 1..2 -> {
                binding.tv1.text = context.getString(R.string.oh_no)
                binding.tv2.text = context.getString(R.string.leave_us_your_feedback)
                binding.ivFace.setImageResource(R.mipmap.pdf_expression2)
            }

            in 3..4 -> {
                binding.tv1.text = context.getString(R.string.oh_we_are_sorry)
                binding.tv2.text = context.getString(R.string.leave_us_your_feedback)
                binding.ivFace.setImageResource(R.mipmap.pdf_expression1)
            }

            5 -> {
                binding.tv1.text = context.getString(R.string.much_appreciated)
                binding.tv2.text = context.getString(R.string.your_support_is_our_motivation)
                binding.ivFace.setImageResource(R.mipmap.pdf_expression3)
            }
        }
    }
}