/**
 * SpecialMoves.js - Handles special chess moves
 * Castling, En Passant, and Pawn Promotion
 */

import { Queen } from '../pieces/Queen.js';
import { Rook } from '../pieces/Rook.js';
import { Bishop } from '../pieces/Bishop.js';
import { Knight } from '../pieces/Knight.js';

export class SpecialMoves {
    /**
     * Execute castling move
     * @param {Board} board - Game board
     * @param {King} king - King piece
     * @param {number} targetCol - Target column (2 or 6)
     * @returns {Object} Move details
     */
    static executeCastle(board, king, targetCol) {
        const row = king.position.row;
        const kingCol = king.position.col;
        const isKingside = targetCol === 6;

        // Determine rook position
        const rookCol = isKingside ? 7 : 0;
        const rookTargetCol = isKingside ? 5 : 3;

        const rook = board.getPiece(row, rookCol);

        // Move king
        board.movePiece(row, kingCol, row, targetCol);

        // Move rook
        board.movePiece(row, rookCol, row, rookTargetCol);

        return {
            type: isKingside ? 'castle-kingside' : 'castle-queenside',
            king: { from: { row, col: kingCol }, to: { row, col: targetCol } },
            rook: { from: { row, col: rookCol }, to: { row, col: rookTargetCol } }
        };
    }

    /**
     * Check if castling is possible
     * @param {Board} board - Game board
     * @param {King} king - King piece
     * @param {number} targetCol - Target column (2 or 6)
     * @returns {boolean} True if can castle
     */
    static canCastle(board, king, targetCol) {
        // King must not have moved
        if (king.hasMoved) {
            return false;
        }

        const row = king.position.row;
        const isKingside = targetCol === 6;
        const rookCol = isKingside ? 7 : 0;

        // Get rook
        const rook = board.getPiece(row, rookCol);
        if (!rook || rook.type !== 'rook' || rook.hasMoved) {
            return false;
        }

        // Check if squares between are empty
        const minCol = Math.min(king.position.col, targetCol);
        const maxCol = Math.max(king.position.col, targetCol);

        for (let col = minCol + 1; col < maxCol; col++) {
            if (board.getPiece(row, col)) {
                return false;
            }
        }

        // Also check rook path for queenside
        if (!isKingside) {
            for (let col = 1; col < king.position.col; col++) {
                if (board.getPiece(row, col)) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Execute en passant capture
     * @param {Board} board - Game board
     * @param {Pawn} pawn - Attacking pawn
     * @param {number} targetRow - Target row
     * @param {number} targetCol - Target column
     * @returns {Piece} Captured pawn
     */
    static executeEnPassant(board, pawn, targetRow, targetCol) {
        const captureRow = pawn.position.row;
        const fromRow = pawn.position.row;
        const fromCol = pawn.position.col;

        // Capture the pawn on the same row
        const capturedPawn = board.getPiece(captureRow, targetCol);
        board.setPiece(captureRow, targetCol, null);

        // Move attacking pawn
        board.movePiece(fromRow, fromCol, targetRow, targetCol);

        return capturedPawn;
    }

    /**
     * Check if en passant is possible
     * @param {Board} board - Game board
     * @param {Pawn} pawn - Attacking pawn
     * @param {number} targetCol - Target column
     * @param {GameState} gameState - Game state
     * @returns {boolean} True if en passant is legal
     */
    static canEnPassant(board, pawn, targetCol, gameState) {
        const enPassantRank = pawn.color === 'white' ? 3 : 4;

        // Pawn must be on correct rank
        if (pawn.position.row !== enPassantRank) {
            return false;
        }

        // Adjacent square must have opponent pawn
        const adjacentPawn = board.getPiece(pawn.position.row, targetCol);
        if (!adjacentPawn ||
            adjacentPawn.type !== 'pawn' ||
            adjacentPawn.color === pawn.color) {
            return false;
        }

        // That pawn must have just moved two squares
        const lastMove = gameState.getLastMove();
        if (!lastMove || lastMove.piece !== adjacentPawn) {
            return false;
        }

        const moveDistance = Math.abs(lastMove.to.row - lastMove.from.row);
        return moveDistance === 2;
    }

    /**
     * Promote pawn to another piece
     * @param {Board} board - Game board
     * @param {Pawn} pawn - Pawn to promote
     * @param {string} pieceType - 'queen', 'rook', 'bishop', or 'knight'
     * @returns {Piece} New promoted piece
     */
    static promote(board, pawn, pieceType = 'queen') {
        const { row, col } = pawn.position;
        const color = pawn.color;

        let newPiece;
        switch (pieceType) {
        case 'queen':
            newPiece = new Queen(color, { row, col });
            break;
        case 'rook':
            newPiece = new Rook(color, { row, col });
            break;
        case 'bishop':
            newPiece = new Bishop(color, { row, col });
            break;
        case 'knight':
            newPiece = new Knight(color, { row, col });
            break;
        default:
            newPiece = new Queen(color, { row, col });
        }

        board.setPiece(row, col, newPiece);
        newPiece.hasMoved = true;

        return newPiece;
    }

    /**
     * Check if pawn can be promoted
     * @param {Pawn} pawn - Pawn to check
     * @returns {boolean} True if at promotion rank
     */
    static canPromote(pawn) {
        if (pawn.type !== 'pawn') {
            return false;
        }

        const promotionRank = pawn.color === 'white' ? 0 : 7;
        return pawn.position.row === promotionRank;
    }

    /**
     * Detect if a move is a special move
     * @param {Board} board - Game board
     * @param {Piece} piece - Piece being moved
     * @param {number} fromRow - Source row
     * @param {number} fromCol - Source column
     * @param {number} toRow - Target row
     * @param {number} toCol - Target column
     * @param {GameState} gameState - Game state
     * @returns {string|null} Special move type or null
     */
    static detectSpecialMove(board, piece, fromRow, fromCol, toRow, toCol, gameState) {
        // Castling
        if (piece.type === 'king' && Math.abs(toCol - fromCol) === 2) {
            return toCol === 6 ? 'castle-kingside' : 'castle-queenside';
        }

        // En passant
        if (piece.type === 'pawn' &&
            Math.abs(toCol - fromCol) === 1 &&
            !board.getPiece(toRow, toCol)) {
            return 'en-passant';
        }

        // Promotion
        if (piece.type === 'pawn' && this.canPromote(piece)) {
            return 'promotion';
        }

        return null;
    }
}
