/**
 * GameGlobal.js
 * Global constants, enums, and configuration for Tank War mini game.
 */

// ============================================================
// Screen & Canvas
// ============================================================
const systemInfo = wx.getSystemInfoSync();

const SCREEN_WIDTH = systemInfo.windowWidth;
const SCREEN_HEIGHT = systemInfo.windowHeight;
const DEVICE_PIXEL_RATIO = systemInfo.pixelRatio || 1;

// ============================================================
// Map Grid
// ============================================================
// TILE_SIZE is determined by screen height so the map fills vertically
const GRID_ROWS = 13;
const GRID_COLS = 21;
const TILE_SIZE = SCREEN_HEIGHT / GRID_ROWS;

const MAP_WIDTH = TILE_SIZE * GRID_COLS;
const MAP_HEIGHT = TILE_SIZE * GRID_ROWS;
// Center map on screen — controls (joystick & fire button) overlay on the map
const MAP_OFFSET_X = Math.floor((SCREEN_WIDTH - MAP_WIDTH) / 2);
const MAP_OFFSET_Y = Math.floor((SCREEN_HEIGHT - MAP_HEIGHT) / 2);

// ============================================================
// Terrain Types
// ============================================================
const TERRAIN = {
  EMPTY: 0,
  BRICK: 1,
  STEEL: 2,
  RIVER: 3,
  FOREST: 4,
  BASE: 5,
  BASE_WALL: 6, // brick wall around base
};

// ============================================================
// Direction
// ============================================================
const DIRECTION = {
  UP: 0,
  DOWN: 1,
  LEFT: 2,
  RIGHT: 3,
};

// Direction vectors (dx, dy)
const DIR_VECTORS = {
  [DIRECTION.UP]: { dx: 0, dy: -1 },
  [DIRECTION.DOWN]: { dx: 0, dy: 1 },
  [DIRECTION.LEFT]: { dx: -1, dy: 0 },
  [DIRECTION.RIGHT]: { dx: 1, dy: 0 },
};

// ============================================================
// Tank Types
// ============================================================
const TANK_TYPE = {
  PLAYER: 'player',
  ENEMY_NORMAL: 'enemy_normal',
  ENEMY_FAST: 'enemy_fast',
  ENEMY_ARMOR: 'enemy_armor',
  ENEMY_BOSS: 'enemy_boss',
};

// Tank configuration table
const TANK_CONFIG = {
  [TANK_TYPE.PLAYER]: {
    speed: 2,
    hp: 1,
    color: '#FFD700', // gold
    size: TILE_SIZE * 0.85,
  },
  [TANK_TYPE.ENEMY_NORMAL]: {
    speed: 1.5,
    hp: 1,
    color: '#AAAAAA', // gray
    size: TILE_SIZE * 0.85,
    score: 100,
  },
  [TANK_TYPE.ENEMY_FAST]: {
    speed: 3,
    hp: 1,
    color: '#FF6347', // tomato
    size: TILE_SIZE * 0.85,
    score: 200,
  },
  [TANK_TYPE.ENEMY_ARMOR]: {
    speed: 1,
    hp: 3,
    color: '#228B22', // forest green
    size: TILE_SIZE * 0.85,
    score: 300,
  },
  [TANK_TYPE.ENEMY_BOSS]: {
    speed: 1.2,
    hp: 6,
    color: '#8B0000', // dark red
    size: TILE_SIZE * 1.2,
    colliderSize: TILE_SIZE * 0.85,
    score: 500,
  },
};

// ============================================================
// Bullet
// ============================================================
const BULLET_SPEED = 5;
const BULLET_SIZE = 6;

// ============================================================
// Fire Level
// ============================================================
const FIRE_LEVEL = {
  LV1: 1, // single shot, 1 bullet on screen
  LV2: 2, // rapid fire, 2 bullets on screen
  LV3: 3, // rapid fire + steel break, 2 bullets on screen
};

const MAX_BULLETS_BY_LEVEL = {
  [FIRE_LEVEL.LV1]: 1,
  [FIRE_LEVEL.LV2]: 2,
  [FIRE_LEVEL.LV3]: 2,
};

// ============================================================
// Power-Up Types
// ============================================================
const POWERUP_TYPE = {
  STAR: 'star',
  CLOCK: 'clock',
  BOMB: 'bomb',
  HELMET: 'helmet',
  SHOVEL: 'shovel',
  TANK: 'tank',
};

const POWERUP_DURATION = 15000; // ms, how long a power-up stays on map

// ============================================================
// Game Settings
// ============================================================
const DEFAULT_LIVES = 3;
const ENEMIES_PER_LEVEL = 20;
const MAX_ENEMIES_ON_SCREEN = 4;
const ENEMY_SPAWN_INTERVAL = 3000; // ms
const FREEZE_DURATION = 10000; // ms
const SHIELD_DURATION = 15000; // ms
const SHOVEL_DURATION = 20000; // ms
const INVINCIBLE_BLINK_INTERVAL = 100; // ms

// ============================================================
// Scene Names
// ============================================================
const SCENE = {
  LOADING: 'loading',
  MENU: 'menu',
  GAME: 'game',
  RESULT: 'result',
  RANKING: 'ranking',
  SETTINGS: 'settings',
  SHOP: 'shop',
  SKIN: 'skin',
  BUFF_SELECT: 'buff_select',
  PVP_ROOM: 'pvp_room',
  PVP_GAME: 'pvp_game',
  PVP_RESULT: 'pvp_result',
  TEAM_ROOM: 'team_room',
  TEAM_GAME: 'team_game',
  TEAM_RESULT: 'team_result',
  TEAM_2V2_ROOM: 'team_2v2_room',
  CHAT_ROOM: 'chat_room',
};

// ============================================================
// Game Modes
// ============================================================
const GAME_MODE = {
  CLASSIC: 'classic',
  ENDLESS: 'endless',
  PVP: 'pvp',
  TEAM_2V2: 'team_2v2',
  TEAM_3V3: 'team_3v3',
};

// ============================================================
// PVP Settings
// ============================================================
const PVP_ROUND_TIME = 180; // seconds per round (legacy, unused in base-destruction mode)
const PVP_RESPAWN_DELAY = 3000; // ms before respawn
const PVP_MAX_LIVES = 5; // legacy, unused in base-destruction mode
const PVP_WIN_KILLS = 5; // legacy, unused in base-destruction mode
const PVP_BASE_HP = 5; // base hit points for 1v1 PVP mode

// ============================================================
// Server Configuration
// ============================================================
// const SERVER_URL = 'ws://192.168.1.103:3000'; // local testing server URL, replace with actual server URL in production
const SERVER_URL = 'wss://game.igeek.site/tankwar/ws';


// ============================================================
// 3v3 Team Settings
// ============================================================
const TEAM_SIZE = 3;
const TEAM_RESPAWN_DELAY = 3000; // ms before respawn
const TEAM_BASE_HP = 10; // base hit points for 3v3 mode
const TEAM_RECONNECT_TIMEOUT = 60000; // ms, 60s to reconnect
const TEAM_MATCH_TIMEOUT = 60000; // ms, 60s matchmaking timeout

// ============================================================
// Battle Configuration (X vs X configurable)
// ============================================================
const BATTLE_CONFIG = {
  '1v1': {
    teamSize: 1,
    baseHp: PVP_BASE_HP,
    respawnDelay: PVP_RESPAWN_DELAY,
    fillWithBots: false,
    mapPool: 'pvp',
  },
  '2v2': {
    teamSize: 2,
    baseHp: 8,
    respawnDelay: TEAM_RESPAWN_DELAY,
    fillWithBots: true,
    mapPool: 'team',
  },
  '3v3': {
    teamSize: 3,
    baseHp: TEAM_BASE_HP,
    respawnDelay: TEAM_RESPAWN_DELAY,
    fillWithBots: true,
    mapPool: 'team',
  },
};

// ============================================================
// Network Message Types
// ============================================================
const NET_MSG = {
  // Room
  CREATE_ROOM: 'create_room',
  JOIN_ROOM: 'join_room',
  ROOM_CREATED: 'room_created',
  ROOM_JOINED: 'room_joined',
  ROOM_ERROR: 'room_error',
  OPPONENT_JOINED: 'opponent_joined',
  OPPONENT_LEFT: 'opponent_left',
  GAME_START: 'game_start',
  // Gameplay
  PLAYER_INPUT: 'player_input',
  PLAYER_STATE: 'player_state',
  BULLET_FIRE: 'bullet_fire',
  BULLET_HIT: 'bullet_hit',
  PLAYER_HIT: 'player_hit',
  PLAYER_KILLED: 'player_killed',
  GAME_OVER: 'game_over',
  // Sync
  PING: 'ping',
  PONG: 'pong',
  SYNC_STATE: 'sync_state',
  // Team (3v3)
  CREATE_TEAM: 'create_team',
  JOIN_TEAM: 'join_team',
  LEAVE_TEAM: 'leave_team',
  TEAM_READY: 'team_ready',
  TEAM_KICK: 'team_kick',
  TEAM_DISBAND: 'team_disband',
  TEAM_STATE: 'team_state',
  MATCH_START: 'match_start',
  MATCH_CANCEL: 'match_cancel',
  MATCH_FOUND: 'match_found',
  MATCH_TIMEOUT: 'match_timeout',
  BASE_HIT: 'base_hit',
  BASE_DESTROYED: 'base_destroyed',
  PLAYER_RESPAWN: 'player_respawn',
  TEAM_GAME_START: 'team_game_start',
  TEAM_GAME_OVER: 'team_game_over',
  TERRAIN_CHANGE: 'terrain_change',
  BOT_STATE: 'bot_state',
  RECONNECT: 'reconnect',
  RECONNECT_OK: 'reconnect_ok',
  PLAYER_DISCONNECT: 'player_disconnect',
  BOT_TAKEOVER: 'bot_takeover',
  SOLO_MATCH: 'solo_match',
  REMATCH: 'rematch',
  REMATCH_READY: 'rematch_ready',
};

// ============================================================
// Colors
// ============================================================
const COLORS = {
  BG: '#000000',
  BRICK: '#B5651D',
  STEEL: '#808080',
  RIVER: '#4169E1',
  FOREST: '#006400',
  BASE: '#FFD700',
  BASE_WALL: '#B5651D',
  HUD_TEXT: '#FFFFFF',
  MENU_BG: '#1a1a2e',
  MENU_TITLE: '#FFD700',
  MENU_BTN: '#16213e',
  MENU_BTN_TEXT: '#FFFFFF',
  MENU_BTN_BORDER: '#0f3460',
};

// ============================================================
// Export
// ============================================================
module.exports = {
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  DEVICE_PIXEL_RATIO,
  GRID_COLS,
  GRID_ROWS,
  TILE_SIZE,
  MAP_WIDTH,
  MAP_HEIGHT,
  MAP_OFFSET_X,
  MAP_OFFSET_Y,
  TERRAIN,
  DIRECTION,
  DIR_VECTORS,
  TANK_TYPE,
  TANK_CONFIG,
  BULLET_SPEED,
  BULLET_SIZE,
  FIRE_LEVEL,
  MAX_BULLETS_BY_LEVEL,
  POWERUP_TYPE,
  POWERUP_DURATION,
  DEFAULT_LIVES,
  ENEMIES_PER_LEVEL,
  MAX_ENEMIES_ON_SCREEN,
  ENEMY_SPAWN_INTERVAL,
  FREEZE_DURATION,
  SHIELD_DURATION,
  SHOVEL_DURATION,
  INVINCIBLE_BLINK_INTERVAL,
  SCENE,
  GAME_MODE,
  PVP_ROUND_TIME,
  PVP_RESPAWN_DELAY,
  PVP_MAX_LIVES,
  PVP_WIN_KILLS,
  PVP_BASE_HP,
  TEAM_SIZE,
  TEAM_RESPAWN_DELAY,
  TEAM_BASE_HP,
  TEAM_RECONNECT_TIMEOUT,
  TEAM_MATCH_TIMEOUT,
  BATTLE_CONFIG,
  NET_MSG,
  COLORS,
  SERVER_URL,
};
