import { Request, Response } from 'express';
import { GameService } from '../../../application/GameService';
import { GameType } from '../../../domain/enums/GameType';

interface AuthenticatedUser {
    getId(): string;
    getUsername(): string;
    getPassword(): string;
    username: string;
}

export class GameController {
    constructor(private gameService: GameService) {}

    async createGame(req: Request, res: Response): Promise<void> {
        try {
            const { gameType, initialCondition } = req.body;
            if (!gameType || !initialCondition) {
                res.status(400).json({ error: '缺少必要参数' });
                return;
            }

            const game = await this.gameService.createGame(gameType as GameType, initialCondition);
            res.status(201).json(game);
        } catch (error) {
            res.status(400).json({ error: (error as Error).message });
        }
    }

    async getPreparingGames(req: Request, res: Response): Promise<void> {
        try {
            const games = await this.gameService.getPreparingGames();
            res.json(games);
        } catch (error) {
            res.status(500).json({ error: (error as Error).message });
        }
    }

    async getNotFinishedGames(req: Request, res: Response): Promise<void> {
        try {
            const games = await this.gameService.getNotFinishedGames();
            res.json(games);
        } catch (error) {
            res.status(500).json({ error: (error as Error).message });
        }
    }

    async getFinishedGames(req: Request, res: Response): Promise<void> {
        try {
            const games = await this.gameService.getFinishedGames();
            res.json(games);    
        } catch (error) {
            res.status(500).json({ error: (error as Error).message });
        }
    }

    async joinGame(req: Request, res: Response): Promise<void> {
        try {
            const { gameId } = req.params;
            console.log(req.user)
            const user = req.user as AuthenticatedUser | undefined;
            if (!user) {
                res.status(401).json({ error: '未登录' });
                return;
            }

            const game = await this.gameService.joinGame(gameId, user.username);
            res.json(game);
        } catch (error) {
            res.status(400).json({ error: (error as Error).message });
        }
    }

    async startGame(req: Request, res: Response): Promise<void> {
        try {
            const { gameId } = req.params;
            const game = await this.gameService.startGame(gameId);
            res.json(game);
        } catch (error) {
            res.status(400).json({ error: (error as Error).message });
        }
    }

    async submitAnswer(req: Request, res: Response): Promise<void> {
        try {
            const { gameId } = req.params;
            const { answer } = req.body;
            
            const user = req.user as AuthenticatedUser | undefined;
            if (!user) {
                res.status(401).json({ error: '未登录' });
                return;
            }
            
            if (!answer) {
                res.status(400).json({ error: '缺少答案' });
                return;
            }

            const game = await this.gameService.getGame(gameId);
            const currentPlayer = game.getCurrentPlayer();
            
            if (currentPlayer.getName() !== user.username) {
                res.status(403).json({ error: '不是你的回合' });
                return;
            }

            const isValid = await this.gameService.submitAnswer(gameId, answer);
            if (isValid) {
                res.json({ isValid });
            } else {
                res.status(400).json({ error: '答案错误' });
            }
        } catch (error) {
            res.status(400).json({ error: (error as Error).message });
        }
    }

    async getGame(req: Request, res: Response): Promise<void> {
        try {
            const { gameId } = req.params;
            const game = await this.gameService.getGame(gameId);
            res.json(game);
        } catch (error) {
            res.status(404).json({ error: (error as Error).message });
        }
    }
} 