/**
 * AI Interactive Fiction - Web Server
 * Serves the web UI and handles WebSocket communication
 */

import path from 'path';
import express from 'express';
import http from 'http';
import { Server as SocketIOServer } from 'socket.io';
import * as dotenv from 'dotenv';
import { GameRunner } from './cli/game-runner';
import { existsSync, mkdirSync, copyFileSync } from 'fs';
import {
  textToParagraphs,
  TurnResult,
} from './interfaces/turn-result';
import {
  clientGameConfig,
  ensureConfiguredAssetDirectories,
  loadGameConfig,
  projectPath,
} from './config/game-config';

// Load environment variables
dotenv.config();

// Create Express application
const app = express();
const server = http.createServer(app);
const io = new SocketIOServer(server);

// Get port from environment variables or use default
const DEFAULT_PORT = 3001;
const PORT = process.env.PORT ? parseInt(process.env.PORT) : DEFAULT_PORT;
const PORT_RANGE = 300; // Try enough ports to skip OS-excluded ranges.
const engineConfig = loadGameConfig(
  process.env.YAML_CONFIG_FILE || './config/engines/yaml.json',
  'yaml',
);

// Serve static files from the public directory. During local development the
// browser must not keep stale ES modules, otherwise UI fixes appear to do
// nothing until a hard cache clear.
app.use(express.static(path.join(__dirname, '../public'), {
  etag: false,
  lastModified: false,
  setHeaders: (res) => {
    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
    res.setHeader('Pragma', 'no-cache');
    res.setHeader('Expires', '0');
  }
}));

app.get('/api/game-config', (_req, res) => {
  res.json(clientGameConfig(engineConfig));
});

// Set up game sessions
const gameSessions = new Map<string, GameRunner>();
const nextTurnIds = new Map<string, number>();

function nextTurnId(socketId: string): number {
  const current = nextTurnIds.get(socketId) || 1;
  nextTurnIds.set(socketId, current + 1);
  return current;
}

function createTextTurn(
  socketId: string,
  text: string,
  gameState: TurnResult['gameState'] = {},
  suggestions?: string[],
): TurnResult {
  const paragraphs = textToParagraphs(text);
  return {
    turnId: nextTurnId(socketId),
    paragraphs,
    choices: [],
    inputMode: 'text',
    gameState,
    suggestions,
  };
}

function normalizeSaveSlot(slot: unknown): number {
  const value = Number(slot);
  return Number.isInteger(value) && value > 0 ? value : 1;
}

async function startDemoGameForSocket(socket: any): Promise<GameRunner> {
  nextTurnIds.set(socket.id, 1);
  const gameRunner = new GameRunner();
  const worldFile = projectPath(process.env.DEFAULT_WORLD_FILE || engineConfig.paths.mainGameFile);

  await gameRunner.initialize(worldFile);
  gameSessions.set(socket.id, gameRunner);

  const gameState = gameRunner.getGameState();
  const paragraphs = [
    ...textToParagraphs(gameState.world.introduction),
    ...textToParagraphs(gameRunner.getCurrentRoomDescription()),
  ];
  socket.emit('narrativeResponse', {
    turnId: nextTurnId(socket.id),
    paragraphs,
    choices: [],
    inputMode: 'text',
    gameState: {
      currentRoomId: gameState.currentRoomId,
    },
  });

  return gameRunner;
}

async function handleGameApi(socket: any, method: string, args: unknown[] = []) {
  const saveGames: Map<number, any> = socket.data.saveGames || new Map<number, any>();
  socket.data.saveGames = saveGames;

  switch (method) {
    case 'newGame':
    case 'newGame()':
      await startDemoGameForSocket(socket);
      return { success: true, result: true, running: true, canLoad: saveGames.size > 0 };

    case 'loadGame':
    case 'loadGame()': {
      const slot = normalizeSaveSlot(args[0]);
      if (!saveGames.has(slot)) {
        return { success: false, error: 'missing_save', result: false };
      }
      await startDemoGameForSocket(socket);
      socket.emit('gameLoaded', { slot });
      return { success: true, result: true, running: true, slot };
    }

    case 'saveGame':
    case 'saveGame()': {
      const gameRunner = gameSessions.get(socket.id);
      if (!gameRunner) {
        return { success: false, error: 'game_not_running', result: false };
      }
      const slot = normalizeSaveSlot(args[0]);
      saveGames.set(slot, gameRunner.getGameState());
      socket.emit('gameSaved', { slot });
      return { success: true, result: true, slot };
    }

    case 'hasSaveGame':
    case 'hasSaveGame()': {
      const slot = normalizeSaveSlot(args[0]);
      return { success: true, result: saveGames.has(slot), slot };
    }

    case 'getSaveGames':
    case 'getSaveGames()':
      return { success: true, result: Array.from(saveGames.keys()).sort((a, b) => a - b) };

    case 'isGameRunning':
    case 'isGameRunning()':
      return { success: true, result: gameSessions.has(socket.id) };

    default:
      return { success: false, error: `unknown_method:${method}` };
  }
}

// Handle socket connections
io.on('connection', (socket) => {
  console.log(`New client connected: ${socket.id}`);
  socket.emit('gameConfig', clientGameConfig(engineConfig));

  socket.data.saveGames = new Map<number, any>();

  socket.on('gameApi', async (request, respond) => {
    try {
      const response = await handleGameApi(socket, String(request?.method || ''), Array.isArray(request?.args) ? request.args : []);
      if (typeof respond === 'function') {
        respond(response);
      }
    } catch (error) {
      console.error('Game API error:', error);
      if (typeof respond === 'function') {
        respond({ success: false, error: error instanceof Error ? error.message : String(error) });
      }
    }
  });
  
  // Process player command
  socket.on('playerCommand', async (data) => {
    try {
      const gameRunner = gameSessions.get(socket.id);
      
      if (!gameRunner) {
        socket.emit('error', { message: 'Game session not found. Please start a new game.' });
        return;
      }
      
      const command = String(data?.command || '').trim();
      
      // During typography and animation work, mirror the command back through
      // the real socket path so the UI pipeline can be tested end to end.
      socket.emit('narrativeResponse', createTextTurn(socket.id, command, {
        currentRoomId: gameRunner.getGameState().currentRoomId
      }, gameRunner.getSuggestions()));
      
    } catch (error) {
      console.error('Error processing command:', error);
      socket.emit('error', { message: 'Failed to process command. Please try again.' });
    }
  });
  
  // Handle disconnection
  socket.on('disconnect', () => {
    console.log(`Client disconnected: ${socket.id}`);
    
    // Clean up game session
    if (gameSessions.has(socket.id)) {
      gameSessions.delete(socket.id);
    }
    nextTurnIds.delete(socket.id);
  });
});

// Ensure required asset folders exist
function ensureDirectories() {
  const dirs = [
    path.join(__dirname, '../public'),
    path.join(__dirname, '../public/js'),
    path.join(__dirname, '../public/css'),
    path.join(__dirname, '../public/images'),
    path.join(__dirname, '../public/music'),
    path.join(__dirname, '../public/sounds'),
    path.join(__dirname, '../public/fonts')
  ];
  
  for (const dir of dirs) {
    if (!existsSync(dir)) {
      mkdirSync(dir, { recursive: true });
    }
  }
  ensureConfiguredAssetDirectories(engineConfig);
}

// Copy kokoro-js library from node_modules if not already present
function ensureKokoroJs() {
  const source = path.join(__dirname, '../node_modules/kokoro-js/dist/index.js');
  const destination = path.join(__dirname, '../public/js/kokoro-js.js');
  
  if (existsSync(source) && !existsSync(destination)) {
    copyFileSync(source, destination);
    console.log(`Copied kokoro-js from ${source} to ${destination}`);
  }
}

// Start the server with port fallback
export async function startServer(initialPort: number, range: number): Promise<void> {
  let currentPort = initialPort;
  const maxPort = initialPort + range;
  
  // Try ports in the specified range
  while (currentPort < maxPort) {
    try {
      // Ensure directories exist
      ensureDirectories();
      
      // Ensure kokoro-js is copied
      try {
        ensureKokoroJs();
      } catch (error) {
        console.error('Error copying kokoro-js:', error);
      }
      
      // Try to start the server on the current port
      await new Promise<void>((resolve, reject) => {
        server.removeAllListeners('error');
        server.removeAllListeners('listening');
        server.once('listening', () => {
          console.log(`AI Interactive Fiction web server running on http://localhost:${currentPort}`);
          resolve();
        });
        server.once('error', (error: NodeJS.ErrnoException) => {
          // If port is in use, try next port
          if (error.code === 'EADDRINUSE' || error.code === 'EACCES') {
            console.log(`Port ${currentPort} is unavailable (${error.code}), trying next port...`);
            server.close();
            currentPort++;
            reject();
          } else {
            // For other errors, log and reject
            console.error('Server error:', error);
            reject(error);
          }
        });
        server.listen(currentPort);
      });
      
      // If we reach here, server started successfully
      return;
      
    } catch (error) {
      // If we reach the max port and still fail, throw an error
      if (currentPort >= maxPort - 1) {
        throw new Error(`Failed to start server on ports ${initialPort} to ${maxPort - 1}`);
      }
      
      // Otherwise try the next port
      // The loop continues as the rejection above increments currentPort
    }
  }
}

// Start the server when this module is run directly
if (require.main === module) {
  startServer(PORT, PORT_RANGE).catch(error => {
    console.error('Failed to start server:', error);
    process.exit(1);
  });
}

export { app, server, io };
