package com.divyagkquiz.app.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.divyagkquiz.app.R
import com.divyagkquiz.app.viewmodel.QuizViewModel
import nl.dionsegijn.konfetti.core.Party
import nl.dionsegijn.konfetti.core.Position
import nl.dionsegijn.konfetti.core.emitter.Emitter
import nl.dionsegijn.konfetti.xml.KonfettiView
import java.util.concurrent.TimeUnit

class ResultFragment : Fragment() {

    private val viewModel: QuizViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_result, container, false)
    }

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

        val score = arguments?.getInt("score") ?: 0
        val total = arguments?.getInt("total") ?: 0
        val category = arguments?.getString("category") ?: "All"
        val isNewRecord = arguments?.getBoolean("isNewRecord") ?: false

        val scoreText = view.findViewById<TextView>(R.id.score_text)
        val resultMessage = view.findViewById<TextView>(R.id.result_message)
        val highScoreText = view.findViewById<TextView>(R.id.high_score_text)
        val achievementBadge = view.findViewById<TextView>(R.id.achievement_badge)
        val homeButton = view.findViewById<Button>(R.id.home_button)
        val konfettiView = view.findViewById<KonfettiView>(R.id.konfettiView)

        scoreText.text = "$score / $total"
        
        val percentage = if (total > 0) (score.toFloat() / total) * 100 else 0f
        
        // Show High Score
        val bestScore = viewModel.getHighScore(category)
        highScoreText.text = "Personal Best: $bestScore / $total"

        when {
            percentage >= 90 -> {
                resultMessage.text = if (isNewRecord) "New Record! Outstanding!" else "Outstanding!"
                achievementBadge.text = "🏆 Gold Badge Unlocked!"
                achievementBadge.visibility = View.VISIBLE
                startCelebration(konfettiView)
            }
            percentage >= 80 -> {
                resultMessage.text = if (isNewRecord) "New Record! Excellent!" else "Excellent!"
                achievementBadge.text = "🥈 Silver Badge Unlocked!"
                achievementBadge.visibility = View.VISIBLE
                startCelebration(konfettiView)
            }
            percentage >= 50 -> {
                resultMessage.text = "Good Job!"
                achievementBadge.text = "🥉 Bronze Badge Unlocked!"
                achievementBadge.visibility = View.VISIBLE
            }
            else -> {
                resultMessage.text = "Keep Practicing!"
                achievementBadge.visibility = View.GONE
            }
        }

        homeButton.setOnClickListener {
            findNavController().navigate(R.id.action_resultFragment_to_homeFragment)
        }
    }

    private fun startCelebration(konfettiView: KonfettiView) {
        val party = Party(
            speed = 0f,
            maxSpeed = 30f,
            damping = 0.9f,
            spread = 360,
            colors = listOf(0xfce18a, 0xff726d, 0xf4306d, 0xb48def),
            position = Position.Relative(0.5, 0.3),
            emitter = Emitter(duration = 100, TimeUnit.MILLISECONDS).max(100)
        )
        konfettiView.start(party)
    }
}
