package com.app.brush.guitar.ink.icicle

import android.app.Dialog
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.app.brush.guitar.ink.BuildConfig
import com.app.brush.guitar.ink.R
import com.app.brush.guitar.ink.databinding.BoldDialogRateUserBinding
import com.app.brush.guitar.ink.databinding.SlimFragmentSettingsPageBinding

class ResilientSetting : Fragment() {

    private lateinit var vb: SlimFragmentSettingsPageBinding
    private var currentRating = 0

    companion object {
        @JvmStatic
        fun newInstance() =
            ResilientSetting()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        vb = SlimFragmentSettingsPageBinding.inflate(layoutInflater)
        return vb.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupVersion()
        setupRateUs()
    }

    private fun setupRateUs() {
        vb.layoutRateUs.setOnClickListener {
            showRateDialog()
        }
    }

    private fun showRateDialog() {
        val dialog = Dialog(requireContext())
        val dialogBinding = BoldDialogRateUserBinding.inflate(LayoutInflater.from(requireContext()))
        dialog.setContentView(dialogBinding.root)
        
        dialog.window?.apply {
            val displayMetrics = requireContext().resources.displayMetrics
            val width = (displayMetrics.widthPixels * 0.9).toInt() // 宽度为屏幕的90%
            
            setLayout(width, WindowManager.LayoutParams.WRAP_CONTENT)
            setGravity(Gravity.CENTER)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
        
        dialog.setCanceledOnTouchOutside(true)

        currentRating = 0
        val stars = listOf(
            dialogBinding.star1,
            dialogBinding.star2,
            dialogBinding.star3,
            dialogBinding.star4,
            dialogBinding.star5
        )

        fun updateStars(rating: Int) {
            currentRating = rating
            stars.forEachIndexed { index, star ->
                if (index < rating) {
                    star.setImageResource(R.drawable.empty_ic_filled_star)
                } else {
                    star.setImageResource(R.drawable.full_ic_empty_star)
                }
            }
            dialogBinding.tvSubmit.visibility = if (rating > 0) View.VISIBLE else View.GONE
        }

        stars.forEachIndexed { index, star ->
            star.setOnClickListener {
                updateStars(index + 1)
            }
        }

        dialogBinding.imClose.setOnClickListener {
            dialog.dismiss()
        }

        dialogBinding.tvSubmit.setOnClickListener {
            dialog.dismiss()
            // 显示评分完成的提示
            showRatingSubmittedToast()
        }

        dialog.show()
    }

    private fun showRatingSubmittedToast() {
        Toast.makeText(
            requireContext(),
            getString(R.string.rating_submitted),
            Toast.LENGTH_SHORT
        ).show()
    }

    private fun setupVersion() {
        try {
            val versionName = BuildConfig.VERSION_NAME
            vb.tvVersionValue.text = versionName
        } catch (e: Exception) {
            // Fallback to PackageManager if BuildConfig is not available
            try {
                val packageInfo = requireContext().packageManager.getPackageInfo(
                    requireContext().packageName,
                    0
                )
                vb.tvVersionValue.text = packageInfo.versionName ?: "Unknown"
            } catch (e2: PackageManager.NameNotFoundException) {
                vb.tvVersionValue.text = "Unknown"
            }
        }
    }

}

