package com.rugvedsarika.easymath.ui.dual

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.rugvedsarika.easymath.model.Question
import com.rugvedsarika.easymath.utils.MathGenerator
import kotlin.random.Random

class DualModeViewModel : ViewModel() {

    private val _currentQuestion = MutableLiveData<Question>()
    val currentQuestion: LiveData<Question> = _currentQuestion

    private val _options = MutableLiveData<List<Int>>()
    val options: LiveData<List<Int>> = _options

    private val _scoreP1 = MutableLiveData(0)
    val scoreP1: LiveData<Int> = _scoreP1

    private val _scoreP2 = MutableLiveData(0)
    val scoreP2: LiveData<Int> = _scoreP2
    
    // Game over logic (e.g. first to 10)
    private val _winner = MutableLiveData<String?>(null)
    val winner: LiveData<String?> = _winner

    init {
        generateNewQuestion()
    }

    fun generateNewQuestion() {
        // Generate random basic question
        val categories = listOf("Addition", "Subtraction", "Multiplication")
        val q = MathGenerator.generateQuestion(categories.random(), "Easy")
        
        // Generate options
        val correctAnswer = q.correctAnswer
        val opts = mutableSetOf<Int>()
        opts.add(correctAnswer)
        while (opts.size < 3) {
            val offset = Random.nextInt(-5, 6) // -5 to +5
            if (offset != 0) {
                opts.add(correctAnswer + offset)
            }
        }
        
        _currentQuestion.value = q
        _options.value = opts.toList().shuffled()
    }

    fun checkAnswerP1(selectedAnswer: Int) {
        if (_winner.value != null) return
        
        val correct = _currentQuestion.value?.correctAnswer ?: return
        if (selectedAnswer == correct) {
            _scoreP1.value = (_scoreP1.value ?: 0) + 1
            checkWinner()
            generateNewQuestion()
        } else {
             // Penalty? Or just ignore? Let's subtract 1 for wrong guess to discourage spamming
             // val current = _scoreP1.value ?: 0
             // if (current > 0) _scoreP1.value = current - 1
             // For simplicity, no penalty for now, or maybe a short delay (handled in UI)
        }
    }

    fun checkAnswerP2(selectedAnswer: Int) {
        if (_winner.value != null) return

        val correct = _currentQuestion.value?.correctAnswer ?: return
        if (selectedAnswer == correct) {
            _scoreP2.value = (_scoreP2.value ?: 0) + 1
            checkWinner()
            generateNewQuestion()
        }
    }
    
    private fun checkWinner() {
        val s1 = _scoreP1.value ?: 0
        val s2 = _scoreP2.value ?: 0
        if (s1 >= 10) {
            _winner.value = "Player 1 Wins!"
        } else if (s2 >= 10) {
            _winner.value = "Player 2 Wins!"
        }
    }
    
    fun resetGame() {
        _scoreP1.value = 0
        _scoreP2.value = 0
        _winner.value = null
        generateNewQuestion()
    }
}
