package com.dong.maxhap.demos.game

import androidx.compose.ui.geometry.Offset
import kotlin.random.Random

data class SnakeGameState(
    val snake: List<Offset>,
    val food: Offset,
    val direction: Offset,
    val score: Int,
    val gameOver: Boolean,
    val gamePaused: Boolean
)

class SnakeGameLogic {
    
    companion object {
        const val GRID_WIDTH = 20
        const val GRID_HEIGHT = 20
        const val CELL_SIZE = 20f
        val INITIAL_DIRECTION = Offset(1f, 0f)
    }
    
    fun initializeGame(): SnakeGameState {
        val initialSnake = listOf(
            Offset(10f, 10f),
            Offset(9f, 10f),
            Offset(8f, 10f)
        )
        return SnakeGameState(
            snake = initialSnake,
            food = generateFood(initialSnake),
            direction = INITIAL_DIRECTION,
            score = 0,
            gameOver = false,
            gamePaused = false
        )
    }
    
    fun moveSnake(state: SnakeGameState): SnakeGameState {
        if (state.gameOver || state.gamePaused) return state
        
        val head = state.snake.first()
        val newHead = Offset(
            (head.x + state.direction.x).coerceIn(0f, (GRID_WIDTH - 1).toFloat()),
            (head.y + state.direction.y).coerceIn(0f, (GRID_HEIGHT - 1).toFloat())
        )
        
        // 检查是否撞到自己
        if (newHead in state.snake) {
            return state.copy(gameOver = true)
        }
        
        val newSnake = mutableListOf(newHead)
        newSnake.addAll(state.snake.dropLast(1))
        
        // 检查是否吃到食物
        if (newHead == state.food) {
            // 蛇身增长
            newSnake.addAll(state.snake.takeLast(1))
            return state.copy(
                snake = newSnake,
                food = generateFood(newSnake),
                score = state.score + 10
            )
        }
        
        return state.copy(snake = newSnake)
    }
    
    fun changeDirection(state: SnakeGameState, newDirection: Offset): SnakeGameState {
        // 防止反向移动
        if (newDirection.x == -state.direction.x && newDirection.y == -state.direction.y) {
            return state
        }
        return state.copy(direction = newDirection)
    }
    
    fun togglePause(state: SnakeGameState): SnakeGameState {
        return state.copy(gamePaused = !state.gamePaused)
    }
    
    fun restartGame(): SnakeGameState {
        return initializeGame()
    }
    
    private fun generateFood(snake: List<Offset>): Offset {
        var food: Offset
        do {
            food = Offset(
                Random.nextInt(GRID_WIDTH).toFloat(),
                Random.nextInt(GRID_HEIGHT).toFloat()
            )
        } while (food in snake)
        return food
    }
    
    fun isGameOver(state: SnakeGameState): Boolean {
        val head = state.snake.first()
        // 检查边界碰撞
        if (head.x < 0 || head.x >= GRID_WIDTH || head.y < 0 || head.y >= GRID_HEIGHT) {
            return true
        }
        // 检查自我碰撞
        return head in state.snake.drop(1)
    }
}