package com.pixelmintdrop.accessibility

import android.content.Context
import android.os.Build
import android.view.View
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityManager
import android.widget.ImageButton
import android.widget.TextView
import androidx.core.view.ViewCompat
import com.pixelmintdrop.R
import com.pixelmintdrop.game.GameView
import com.pixelmintdrop.model.TetrominoType

/**
 * Helper class to improve the game's accessibility for users with visual impairments
 * or other accessibility needs.
 */
class GameAccessibilityHelper(private val context: Context) {

    private val accessibilityManager = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
    
    /**
     * Sets up accessibility content descriptions for game controls
     */
    fun setupAccessibilityDescriptions(
        leftButton: ImageButton?,
        rightButton: ImageButton?,
        rotateButton: ImageButton?,
        dropButton: ImageButton?,
        holdButton: ImageButton?,
        pauseButton: ImageButton?,
        gameView: GameView?,
        holdPieceView: View?,
        nextPieceView: View?
    ) {
        // Set content descriptions for all control buttons
        leftButton?.contentDescription = context.getString(R.string.accessibility_move_left)
        rightButton?.contentDescription = context.getString(R.string.accessibility_move_right)
        rotateButton?.contentDescription = context.getString(R.string.accessibility_rotate_piece)
        dropButton?.contentDescription = context.getString(R.string.accessibility_drop_piece)
        holdButton?.contentDescription = context.getString(R.string.accessibility_hold_piece)
        pauseButton?.contentDescription = context.getString(R.string.accessibility_pause_game)
        
        // Set content descriptions for game views
        gameView?.contentDescription = context.getString(R.string.accessibility_game_board)
        holdPieceView?.contentDescription = context.getString(R.string.accessibility_held_piece)
        nextPieceView?.contentDescription = context.getString(R.string.accessibility_next_piece)
        
        // Add accessibility delegate to the game view for custom events
        gameView?.let {
            ViewCompat.setAccessibilityDelegate(it, object : androidx.core.view.AccessibilityDelegateCompat() {
                override fun onPopulateAccessibilityEvent(host: View, event: AccessibilityEvent) {
                    super.onPopulateAccessibilityEvent(host, event)
                    
                    // Add custom content to accessibility events if needed
                    if (event.eventType == AccessibilityEvent.TYPE_VIEW_SELECTED) {
                        // Example: announce the current piece type
                        event.text.add("Current piece: ${getCurrentPieceDescription(it)}")
                    }
                }
            })
        }
    }
    
    /**
     * Announces important game events via accessibility services
     */
    fun announceGameEvent(view: View, message: String) {
        if (accessibilityManager.isEnabled) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                view.announceForAccessibility(message)
            } else {
                // For older Android versions
                val event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT)
                event.text.add(message)
                view.sendAccessibilityEvent(AccessibilityEvent.TYPE_ANNOUNCEMENT)
            }
        }
    }
    
    /**
     * Updates score and level information to be more accessible
     */
    fun updateGameStatusAccessibility(
        scoreTextView: TextView?,
        levelTextView: TextView?,
        linesTextView: TextView?,
        score: Long,
        level: Int,
        lines: Int
    ) {
        // Make sure score is accessible to screen readers with proper formatting
        scoreTextView?.let {
            it.contentDescription = "Score: $score points"
        }
        
        levelTextView?.let {
            it.contentDescription = "Level: $level"
        }
        
        linesTextView?.let {
            it.contentDescription = "Lines cleared: $lines"
        }
    }
    
    /**
     * Get a description of the current Tetromino piece for accessibility announcements
     */
    private fun getCurrentPieceDescription(gameView: GameView): String {
        val pieceType = gameView.getCurrentPieceType() ?: return "No piece"
        
        return when (pieceType) {
            TetrominoType.I -> "I piece, long bar"
            TetrominoType.J -> "J piece, hook shape pointing left"
            TetrominoType.L -> "L piece, hook shape pointing right"
            TetrominoType.O -> "O piece, square shape"
            TetrominoType.S -> "S piece, zigzag shape"
            TetrominoType.T -> "T piece, T shape"
            TetrominoType.Z -> "Z piece, reverse zigzag shape"
        }
    }
    
    /**
     * Announce the game over event with final score
     */
    fun announceGameOver(view: View, score: Long) {
        announceGameEvent(view, "Game over. Final score: $score points")
    }
    
    /**
     * Announce level up
     */
    fun announceLevelUp(view: View, newLevel: Int) {
        announceGameEvent(view, "Level up! Now at level $newLevel")
    }
} 