package com.pixelmintdrop.ui

import android.content.Context
import android.graphics.Color
import android.view.View
import com.pixelmintdrop.databinding.ActivityMainBinding
import com.pixelmintdrop.model.PlayerProgressionManager

/**
 * Handles UI updates and state management for the game interface
 * to reduce the responsibility of the MainActivity.
 */
class GameUIManager(
    private val context: Context,
    private val binding: ActivityMainBinding
) {
    // UI state
    private var currentScore = 0L
    private var currentLevel = 1
    private var currentLines = 0
    
    // Theme management
    private var currentTheme = PlayerProgressionManager.THEME_CLASSIC
    
    /**
     * Update the game state UI elements
     */
    fun updateGameStateUI(score: Long, level: Int, lines: Int) {
        // Update cached values
        currentScore = score
        currentLevel = level
        currentLines = lines
        
        // Update UI
        binding.scoreText.text = score.toString()
        binding.currentLevelText.text = level.toString()
        binding.linesText.text = lines.toString()
    }
    
    /**
     * Show the game over UI
     */
    fun showGameOver(finalScore: Long) {
        // Hide game UI elements
        binding.gameControlsContainer.visibility = View.GONE
        binding.holdPieceView.visibility = View.GONE
        binding.nextPieceView.visibility = View.GONE
        binding.pauseButton.visibility = View.GONE
        binding.leftControlsPanel?.visibility = View.GONE
        binding.rightControlsPanel?.visibility = View.GONE
        
        // Show game over container
        binding.gameOverContainer.visibility = View.VISIBLE
        binding.sessionScoreText.text = "Score: $finalScore"
    }
    
    /**
     * Hide the game over UI
     */
    fun hideGameOver() {
        binding.gameOverContainer.visibility = View.GONE
    }
    
    /**
     * Show the pause menu
     */
    fun showPauseMenu() {
        binding.pauseContainer.visibility = View.VISIBLE
    }
    
    /**
     * Hide the pause menu
     */
    fun hidePauseMenu() {
        binding.pauseContainer.visibility = View.GONE
    }
    
    /**
     * Update the music toggle UI based on current state
     */
    fun updateMusicToggleUI(isMusicEnabled: Boolean) {
        // This assumes there's a musicToggle view in the layout
        // Modify as needed based on the actual UI
    }
    
    /**
     * Update the level selector display
     */
    fun updateLevelSelector(selectedLevel: Int) {
        // Assuming pauseLevelText is part of the LevelBadge component
        // This may need to be adapted based on how the level badge works
    }
    
    /**
     * Show the game elements (views, controls)
     */
    fun showGameElements() {
        binding.gameView.visibility = View.VISIBLE
        binding.gameControlsContainer.visibility = View.VISIBLE
        binding.holdPieceView.visibility = View.VISIBLE
        binding.nextPieceView.visibility = View.VISIBLE
        binding.pauseButton.visibility = View.VISIBLE
        
        // These might not exist in the actual layout
        // binding.leftControlsPanel?.visibility = View.VISIBLE
        // binding.rightControlsPanel?.visibility = View.VISIBLE
    }
    
    /**
     * Hide the game elements
     */
    fun hideGameElements() {
        binding.gameControlsContainer.visibility = View.GONE
        binding.holdPieceView.visibility = View.GONE
        binding.nextPieceView.visibility = View.GONE
        binding.pauseButton.visibility = View.GONE
        
        // These might not exist in the actual layout
        // binding.leftControlsPanel?.visibility = View.GONE
        // binding.rightControlsPanel?.visibility = View.GONE
    }
    
    /**
     * Apply a theme to the game UI
     */
    fun applyTheme(themeId: String) {
        currentTheme = themeId
        
        // Set background color based on theme
        val backgroundColor = getThemeBackgroundColor(themeId)
        binding.root.setBackgroundColor(backgroundColor)
        
        // Update title screen theme if it has a setTheme method
        // binding.titleScreen.setTheme(themeId)
        
        // Set text colors based on theme
        val isDarkTheme = backgroundColor == Color.BLACK || 
                          Color.red(backgroundColor) < 50 && 
                          Color.green(backgroundColor) < 50 && 
                          Color.blue(backgroundColor) < 50
        
        val textColor = if (isDarkTheme) Color.WHITE else Color.BLACK
        updateTextColors(textColor)
    }
    
    /**
     * Update all text colors in the UI
     */
    private fun updateTextColors(color: Int) {
        binding.scoreText.setTextColor(color)
        binding.currentLevelText.setTextColor(color)
        binding.linesText.setTextColor(color)
        
        // Other text views might not exist or have different IDs
        // Adapt based on the actual layout
    }
    
    /**
     * Get background color based on theme ID
     */
    private fun getThemeBackgroundColor(themeId: String): Int {
        return when (themeId) {
            PlayerProgressionManager.THEME_CLASSIC -> Color.BLACK
            PlayerProgressionManager.THEME_NEON -> Color.parseColor("#0D0221")
            PlayerProgressionManager.THEME_MONOCHROME -> Color.parseColor("#1A1A1A")
            PlayerProgressionManager.THEME_RETRO -> Color.parseColor("#3F2832")
            PlayerProgressionManager.THEME_MINIMALIST -> Color.WHITE
            PlayerProgressionManager.THEME_GALAXY -> Color.parseColor("#0B0C10")
            else -> Color.BLACK
        }
    }
    
    /**
     * Get the current score
     */
    fun getCurrentScore(): Long = currentScore
    
    /**
     * Get the current level
     */
    fun getCurrentLevel(): Int = currentLevel
    
    /**
     * Get the current lines cleared
     */
    fun getCurrentLines(): Int = currentLines
} 