package com.tiedup.remake.cells;

/**
 * Enum defining the types of markers used in the cell system.
 *
 * Phase: Kidnapper Revamp - Cell System
 *
 * Markers are invisible points placed by structure builders to define
 * functional areas within kidnapper hideouts.
 */
public enum MarkerType {
    // ==================== CELL MARKERS (V1 legacy — kept for retrocompat) ====================

    /** @deprecated V1 cell marker. Kept for save retrocompat and V1→V2 migration. Use Cell Core flood-fill instead. */
    @Deprecated
    WALL("wall", true),

    /** @deprecated V1 cell marker. Kept for save retrocompat and V1→V2 migration. Use Cell Core flood-fill instead. */
    @Deprecated
    ANCHOR("anchor", true),

    /** @deprecated V1 cell marker. Kept for save retrocompat and V1→V2 migration. Use Cell Core flood-fill instead. */
    @Deprecated
    BED("bed", true),

    /** @deprecated V1 cell marker. Kept for save retrocompat and V1→V2 migration. Use Cell Core flood-fill instead. */
    @Deprecated
    DOOR("door", true),

    /** @deprecated V1 cell marker. Kept for save retrocompat and V1→V2 migration. Use Cell Core flood-fill instead. */
    @Deprecated
    DELIVERY("delivery", true),

    // ==================== STRUCTURE MARKERS (Admin Wand) ====================

    /**
     * Entrance marker - Main entry point to the structure.
     * Used for AI pathfinding and player release point.
     */
    ENTRANCE("entrance", false),

    /**
     * Patrol marker - Waypoint for kidnapper AI patrol routes.
     * Guards will walk between these points.
     */
    PATROL("patrol", false),

    /**
     * Loot marker - Position for loot chests in structures.
     * Used for confiscated inventory storage.
     */
    LOOT("loot", false),

    /**
     * Spawner marker - Position for kidnapper spawns.
     * Kidnappers respawn at these points.
     */
    SPAWNER("spawner", false),

    /**
     * Trader spawn marker - Position for SlaveTrader spawn.
     * Only spawns once when structure generates.
     */
    TRADER_SPAWN("trader_spawn", false),

    /**
     * Maid spawn marker - Position for Maid spawn.
     * Spawns linked to the nearest trader.
     */
    MAID_SPAWN("maid_spawn", false),

    /**
     * Merchant spawn marker - Position for Merchant spawn.
     * Spawns a merchant NPC that can trade/buy items.
     */
    MERCHANT_SPAWN("merchant_spawn", false);

    private final String serializedName;
    private final boolean cellMarker;

    MarkerType(String serializedName, boolean cellMarker) {
        this.serializedName = serializedName;
        this.cellMarker = cellMarker;
    }

    /**
     * Get the serialized name for NBT/network storage.
     */
    public String getSerializedName() {
        return serializedName;
    }

    /**
     * Check if this is a cell-level marker (vs structure-level).
     */
    public boolean isCellMarker() {
        return cellMarker;
    }

    /**
     * Check if this is a structure-level marker.
     */
    public boolean isStructureMarker() {
        return !cellMarker;
    }

    /**
     * Check if this marker type should be linked to a cell's positions.
     * This includes WALL, ANCHOR, BED, DOOR - positions that define cell structure.
     */
    public boolean isLinkedPosition() {
        return cellMarker;
    }

    /**
     * Parse a MarkerType from its serialized name.
     *
     * @param name The serialized name
     * @return The MarkerType, or WALL as default
     */
    public static MarkerType fromString(String name) {
        for (MarkerType type : values()) {
            if (type.serializedName.equalsIgnoreCase(name)) {
                return type;
            }
        }
        return ENTRANCE;
    }

    /**
     * Get the next STRUCTURE marker type (for Admin Wand).
     * Cycles: ENTRANCE -> PATROL -> LOOT -> SPAWNER -> TRADER_SPAWN -> MAID_SPAWN -> MERCHANT_SPAWN -> ENTRANCE
     */
    public MarkerType nextStructureType() {
        return switch (this) {
            case ENTRANCE -> PATROL;
            case PATROL -> LOOT;
            case LOOT -> SPAWNER;
            case SPAWNER -> TRADER_SPAWN;
            case TRADER_SPAWN -> MAID_SPAWN;
            case MAID_SPAWN -> MERCHANT_SPAWN;
            case MERCHANT_SPAWN -> ENTRANCE;
            default -> ENTRANCE;
        };
    }

    /**
     * Get all structure marker types.
     */
    public static MarkerType[] structureTypes() {
        return new MarkerType[] {
            ENTRANCE,
            PATROL,
            LOOT,
            SPAWNER,
            TRADER_SPAWN,
            MAID_SPAWN,
            MERCHANT_SPAWN,
        };
    }
}
