package com.pixelmintdrop

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import android.content.Context

class MainActivityViewModel(application: Application) : AndroidViewModel(application) {

    // Private MutableLiveData for internal updates
    private val _currentScore = MutableLiveData<Long>(0L)
    private val _currentLevel = MutableLiveData<Int>(1)

    // Public LiveData for observation by the Activity
    val currentScore: LiveData<Long> = _currentScore
    val currentLevel: LiveData<Int> = _currentLevel

    // --- Sound & Music State ---
    private val _isSoundEnabled = MutableLiveData<Boolean>(true) // Default to true
    val isSoundEnabled: LiveData<Boolean> = _isSoundEnabled

    private val _isMusicEnabled = MutableLiveData<Boolean>(true) // Default to true
    val isMusicEnabled: LiveData<Boolean> = _isMusicEnabled
    // ---------------------------

    // --- Game Settings State ---
    private val _selectedLevel = MutableLiveData<Int>(1) // Default start level
    val selectedLevel: LiveData<Int> = _selectedLevel

    private val _isRandomModeEnabled = MutableLiveData<Boolean>(false)
    val isRandomModeEnabled: LiveData<Boolean> = _isRandomModeEnabled
    // ---------------------------

    init { // Load initial random mode setting
        loadSettings()
    }

    // Example function to update the score (logic would be moved here)
    fun incrementScore(points: Long) {
        _currentScore.value = (_currentScore.value ?: 0L) + points
        // Potentially add logic here to check for level up based on score
    }

    // Function to set the score directly
    fun setScore(score: Long) {
        _currentScore.value = score
    }

    // Example function to update the level
    fun setLevel(level: Int) {
        _currentLevel.value = level
    }

    fun resetGame() {
        _currentScore.value = 0L
        _currentLevel.value = 1
        // Reset other game state within the ViewModel as needed
    }

    // --- Sound & Music Logic ---
    fun toggleSound() {
        _isSoundEnabled.value = !(_isSoundEnabled.value ?: true)
    }

    fun setMusicEnabled(enabled: Boolean) {
        _isMusicEnabled.value = enabled
    }

    fun toggleMusic() {
        setMusicEnabled(!(_isMusicEnabled.value ?: true))
    }
    // --------------------------

    // --- Game Settings Logic ---
    fun setSelectedLevel(level: Int) {
        _selectedLevel.value = level
    }

    fun toggleRandomMode() {
        val newValue = !(_isRandomModeEnabled.value ?: false)
        _isRandomModeEnabled.value = newValue
        saveRandomModeSetting(newValue)
    }

    private fun loadSettings() {
        val prefs = getApplication<Application>().getSharedPreferences(
            "com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE
        )
        _isRandomModeEnabled.value = prefs.getBoolean("random_mode_enabled", false)
        // Load other settings if needed
    }

    private fun saveRandomModeSetting(enabled: Boolean) {
        val prefs = getApplication<Application>().getSharedPreferences(
            "com.com.pixelmintgames.pixelmintdrop.preferences", Context.MODE_PRIVATE
        )
        prefs.edit().putBoolean("random_mode_enabled", enabled).apply()
    }
    // ---------------------------

    // Add other state variables and logic related to game state here
} 