package com.rugvedsarika.easymath.ui.dual

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.rugvedsarika.easymath.databinding.FragmentDualModeBinding

class DualModeFragment : Fragment() {

    private var _binding: FragmentDualModeBinding? = null
    private val binding get() = _binding!!
    
    private val viewModel: DualModeViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentDualModeBinding.inflate(inflater, container, false)
        return binding.root
    }

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

        viewModel.currentQuestion.observe(viewLifecycleOwner) { q ->
            binding.textQuestionP1.text = q.text
            binding.textQuestionP2.text = q.text
        }

        viewModel.options.observe(viewLifecycleOwner) { options ->
            setOptions(options)
        }

        viewModel.scoreP1.observe(viewLifecycleOwner) { score ->
            binding.textScoreP1.text = "P1: $score"
        }

        viewModel.scoreP2.observe(viewLifecycleOwner) { score ->
            binding.textScoreP2.text = "P2: $score"
        }
        
        viewModel.winner.observe(viewLifecycleOwner) { winner ->
            if (winner != null) {
                showWinnerDialog(winner)
            }
        }

        setupClickListeners()
    }

    private fun setOptions(options: List<Int>) {
        if (options.size >= 3) {
            binding.btnOption1P1.text = options[0].toString()
            binding.btnOption2P1.text = options[1].toString()
            binding.btnOption3P1.text = options[2].toString()
            
            binding.btnOption1P2.text = options[0].toString()
            binding.btnOption2P2.text = options[1].toString()
            binding.btnOption3P2.text = options[2].toString()
        }
    }
    
    private fun setupClickListeners() {
        val p1Buttons = listOf(binding.btnOption1P1, binding.btnOption2P1, binding.btnOption3P1)
        p1Buttons.forEach { btn ->
            btn.setOnClickListener {
                viewModel.checkAnswerP1(btn.text.toString().toIntOrNull() ?: 0)
            }
        }
        
        val p2Buttons = listOf(binding.btnOption1P2, binding.btnOption2P2, binding.btnOption3P2)
        p2Buttons.forEach { btn ->
            btn.setOnClickListener {
                viewModel.checkAnswerP2(btn.text.toString().toIntOrNull() ?: 0)
            }
        }
    }

    private fun showWinnerDialog(winnerName: String) {
        AlertDialog.Builder(requireContext())
            .setTitle("Game Over")
            .setMessage("$winnerName")
            .setPositiveButton("Play Again") { _, _ ->
                viewModel.resetGame()
            }
            .setNegativeButton("Exit") { _, _ ->
                findNavController().navigateUp()
            }
            .setCancelable(false)
            .show()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
