/**
 * TilePropertyHelper.js
 *
 * Static lookup table for all tile properties, transcribed from platforms.tsx.
 * Phaser cannot load external TSX files, so properties are defined here.
 *
 * IMPORTANT: Phaser's tile.index is a 1-based GID.
 *   - Use getPropsByGID(tile.index) when working with Phaser tile objects.
 *   - Use getProps(tileId) with 0-based IDs from the TSX.
 *   - All setCollision() / putTileAt() calls use 1-based GIDs.
 */

// 0-based tile ID -> property object (source: assets/tilesets/platforms.tsx)
const TILE_PROPS = {
    0:  { floor: true },
    2:  { floor: true },
    3:  { floor: true },
    4:  { floor: true },
    5:  { floor: true },
    6:  { floor: true },
    7:  { ladder: true, top: true },
    8:  { button: true, color: 'red',    pressed: false, solid: true  },
    9:  { button: true, color: 'blue',   pressed: false, solid: true  },
    10: { floor: true,  solid: true },
    12: { floor: true },
    13: { floor: true },
    14: { floor: true },
    15: { floor: true },
    16: { floor: true },
    17: { ladder: true },
    18: { button: true, color: 'red',    pressed: true,  solid: false },
    19: { button: true, color: 'blue',   pressed: true,  solid: false },
    20: { barrier: true, color: 'red',    solid: true  },
    21: { barrier: true, color: 'red',    solid: false },
    22: { floor: true },
    23: { floor: true },
    24: { floor: true },
    25: { floor: true },
    26: { floor: true },
    28: { button: true, color: 'yellow', pressed: false, solid: true  },
    29: { button: true, color: 'green',  pressed: false, solid: true  },
    30: { barrier: true, color: 'blue',   solid: true  },
    31: { barrier: true, color: 'blue',   solid: false },
    38: { button: true, color: 'yellow', pressed: true,  solid: false },
    39: { button: true, color: 'green',  pressed: true,  solid: false },
    40: { barrier: true, color: 'yellow', solid: true  },
    41: { barrier: true, color: 'yellow', solid: false },
    50: { barrier: true, color: 'green',  solid: true  },
    51: { barrier: true, color: 'green',  solid: false },
};

/**
 * GIDs (1-based, matching Phaser tile.index) for each color's button/barrier tiles.
 *   btn_off      = unpressed button GID
 *   btn_on       = pressed button GID
 *   barrier_solid = solid barrier GID
 *   barrier_open  = passable barrier GID
 */
export const COLOR_TILES = {
    red:    { btn_off: 9,  btn_on: 19, barrier_solid: 21, barrier_open: 22 },
    blue:   { btn_off: 10, btn_on: 20, barrier_solid: 31, barrier_open: 32 },
    yellow: { btn_off: 29, btn_on: 39, barrier_solid: 41, barrier_open: 42 },
    green:  { btn_off: 30, btn_on: 40, barrier_solid: 51, barrier_open: 52 },
};

// GIDs of floor tiles for one-way collision (does not include SOLID_FLOOR_GID)
export const FLOOR_GIDS = [
    1,          // tile_id 0
    3, 4, 5, 6, 7,          // tile_id 2–6
    13, 14, 15, 16, 17,     // tile_id 12–16
    23, 24, 25, 26, 27,     // tile_id 22–26
];

// GID of the tile that is both floor AND solid (impassable from all sides)
export const SOLID_FLOOR_GID = 11; // tile_id 10

// Ladder GIDs
export const LADDER_GID     = 18; // tile_id 17 — standard rung
export const LADDER_TOP_GID = 8;  // tile_id 7  — top rung (allows jumping)

// All GIDs that start as solid in the barriers layer (unpressed buttons + solid barriers)
export const SOLID_BARRIER_GIDS = [
    9, 10, 29, 30,   // unpressed buttons: red, blue, yellow, green
    21, 31, 41, 51,  // solid barriers:    red, blue, yellow, green
];

export default class TilePropertyHelper {
    /** Returns property object for a 0-based tile ID (TSX convention). */
    static getProps(tileId) {
        return TILE_PROPS[tileId] || {};
    }

    /** Returns property object for a 1-based GID (Phaser tile.index convention). */
    static getPropsByGID(gid) {
        return TILE_PROPS[gid - 1] || {};
    }

    static isFloor(gid)     { return !!(this.getPropsByGID(gid).floor);    }
    static isSolid(gid)     { return !!(this.getPropsByGID(gid).solid);    }
    static isLadder(gid)    { return !!(this.getPropsByGID(gid).ladder);   }
    static isLadderTop(gid) { const p = this.getPropsByGID(gid); return !!(p.ladder && p.top); }
    static isButton(gid)    { return !!(this.getPropsByGID(gid).button);   }
    static isBarrier(gid)   { return !!(this.getPropsByGID(gid).barrier);  }
    static getColor(gid)    { return this.getPropsByGID(gid).color || null; }
}
