import { ActionFormData, ModalFormData } from '@minecraft/server-ui';
/**
 * UI handler for area management
 */
export class AreaUI {
    areaData;
    constructor(areaData) {
        this.areaData = areaData;
    }
    /**
     * Show the zone completion form (deprecated - zone creation disabled)
     */
    async showCompleteZoneForm(player) {
        player.sendMessage("§cArea creation has been disabled. All areas are now predefined.");
    }
    /**
     * Show the zone name input form
     */
    async showZoneNameForm(player, tempData) {
        const form = new ModalFormData()
            .title("Enter Zone Name")
            .textField("Zone Name:", "Enter a name for this zone...");
        try {
            const response = await form.show(player);
            if (response.canceled || !response.formValues || !response.formValues[0]) {
                player.sendMessage("§eZone naming cancelled.");
                return;
            }
            const zoneName = response.formValues[0];
            if (zoneName.trim().length === 0) {
                player.sendMessage("§cZone name cannot be empty!");
                return;
            }
            // Complete the zone creation
            player.sendMessage("§cZone creation is disabled.");
        }
        catch (error) {
            player.sendMessage("§cError showing zone name form!");
        }
    }
    /**
     * Show zone creation confirmation
     */
    async showZoneCreatedConfirmation(player, zoneName) {
        const form = new ActionFormData()
            .title("Zone Created Successfully!")
            .body(`§aZone "${zoneName}" has been created successfully!\n\n§7You can now use this area for your parkour map.`)
            .button("OK", "textures/ui/confirm");
        try {
            await form.show(player);
        }
        catch (error) {
            console.error(`Error showing confirmation: ${error}`);
        }
    }
    /**
     * Build the zone information text for the form
     */
    buildZoneInfoText(tempData) {
        const startPos = tempData.startPos;
        const endPos = tempData.endPos;
        const spawnPoint = tempData.spawnPoint;
        const checkpoint = tempData.checkpoint;
        const customSpawnPoint = tempData.customSpawnPoint;
        const finishLine = tempData.finishLine;
        let text = "§6=== Zone Configuration ===\n\n";
        text += "§bArea Coordinates:\n";
        text += `§7Start: §f${Math.floor(startPos.x)}, ${Math.floor(startPos.y)}, ${Math.floor(startPos.z)}\n`;
        text += `§7End: §f${Math.floor(endPos.x)}, ${Math.floor(endPos.y)}, ${Math.floor(endPos.z)}\n`;
        text += `§7Size: §f25x25x25 blocks\n\n`;
        if (customSpawnPoint) {
            text += "§bCustom Spawn Point (Starting Area):\n";
            text += `§7Location: §f${Math.floor(customSpawnPoint.x)}, ${Math.floor(customSpawnPoint.y)}, ${Math.floor(customSpawnPoint.z)}\n`;
            text += `§7Rotation: §f${customSpawnPoint.yaw.toFixed(1)}°, ${customSpawnPoint.pitch.toFixed(1)}°\n\n`;
        }
        else {
            text += "§bDefault Spawn Point:\n";
            text += `§7Location: §f${Math.floor(spawnPoint.x)}, ${Math.floor(spawnPoint.y)}, ${Math.floor(spawnPoint.z)}\n`;
            text += `§7Rotation: §f${spawnPoint.yaw.toFixed(1)}°, ${spawnPoint.pitch.toFixed(1)}°\n\n`;
        }
        if (checkpoint) {
            text += "§bCheckpoint:\n";
            text += `§7Location: §f${Math.floor(checkpoint.x)}, ${Math.floor(checkpoint.y)}, ${Math.floor(checkpoint.z)}\n`;
            text += `§7Rotation: §f${checkpoint.yaw.toFixed(1)}°, ${checkpoint.pitch.toFixed(1)}°\n\n`;
        }
        if (finishLine) {
            text += "§bFinish Line (Finishing Area):\n";
            text += `§7Location: §f${Math.floor(finishLine.x)}, ${Math.floor(finishLine.y)}, ${Math.floor(finishLine.z)}\n`;
            text += `§7Rotation: §f${finishLine.yaw.toFixed(1)}°, ${finishLine.pitch.toFixed(1)}°\n\n`;
        }
        text += "§aReady to create zone! Click 'Enter Zone Name' to continue.";
        return text;
    }
    /**
     * Show area list (for admin purposes)
     */
    async showAreaList(player) {
        const areas = this.areaData.getAllAreasWithData();
        if (areas.length === 0) {
            const form = new ActionFormData()
                .title("Area List")
                .body("§7No areas have been created yet.")
                .button("OK", "textures/ui/confirm");
            try {
                await form.show(player);
            }
            catch (error) {
                console.error(`Error showing empty area list: ${error}`);
            }
            return;
        }
        const form = new ActionFormData()
            .title("Area List")
            .body(`§6Found ${areas.length} area(s):`);
        // Add button for each area with name and coordinates
        for (const area of areas) {
            if (area && area.data) {
                const startPos = area.data.coordinates.startPos;
                const endPos = area.data.coordinates.endPos;
                const centerX = Math.floor((startPos.x + endPos.x) / 2);
                const centerY = Math.floor((startPos.y + endPos.y) / 2);
                const centerZ = Math.floor((startPos.z + endPos.z) / 2);
                form.button(`${area.data.name} (${centerX}, ${centerY}, ${centerZ})\n§cCreated by: ${area.data.createdBy}`, "textures/blocks/grass_carried");
            }
        }
        form.button("Close", "textures/ui/cancel");
        try {
            const response = await form.show(player);
            if (response.canceled || response.selection === areas.length) {
                return;
            }
            // Show details for selected area
            const selectedArea = areas[response.selection];
            await this.showAreaDetails(player, selectedArea.areaId);
        }
        catch (error) {
            player.sendMessage("§cError showing area list!");
        }
    }
    /**
     * Show details for a specific area
     */
    async showAreaDetails(player, areaId) {
        const area = this.areaData.get(areaId);
        if (!area) {
            player.sendMessage("§cArea not found!");
            return;
        }
        const data = area.data;
        let bodyText = `§6=== ${data.name} ===\n\n`;
        bodyText += `§bArea ID: §f${areaId}\n`;
        bodyText += `§bCreated by: §f${data.createdBy}\n`;
        bodyText += `§bCreated: §f${new Date(data.createdAt).toLocaleString()}\n`;
        bodyText += `§bStatus: §f${data.isActive ? '§aActive' : '§cInactive'}\n\n`;
        bodyText += "§bCoordinates:\n";
        bodyText += `§7Start: §f${Math.floor(data.coordinates.startPos.x)}, ${Math.floor(data.coordinates.startPos.y)}, ${Math.floor(data.coordinates.startPos.z)}\n`;
        bodyText += `§7End: §f${Math.floor(data.coordinates.endPos.x)}, ${Math.floor(data.coordinates.endPos.y)}, ${Math.floor(data.coordinates.endPos.z)}\n\n`;
        bodyText += `§bSpawn Point: §f${Math.floor(data.spawnPoint.x)}, ${Math.floor(data.spawnPoint.y)}, ${Math.floor(data.spawnPoint.z)}\n`;
        if (data.checkpoint) {
            bodyText += `§bCheckpoint: §f${Math.floor(data.checkpoint.x)}, ${Math.floor(data.checkpoint.y)}, ${Math.floor(data.checkpoint.z)}\n`;
        }
        else {
            bodyText += `§bCheckpoint: §7None\n`;
        }
        const form = new ActionFormData()
            .title("Area Details")
            .body(bodyText)
            .button("Back to List", "textures/ui/arrow_left")
            .button("Delete Area", "textures/ui/trash")
            .button("Close", "textures/ui/cancel");
        try {
            const response = await form.show(player);
            if (response.canceled) {
                return;
            }
            switch (response.selection) {
                case 0: // Back to List
                    await this.showAreaList(player);
                    break;
                case 1: // Delete Area
                    await this.confirmDeleteArea(player, areaId, data.name);
                    break;
                case 2: // Close
                    break;
            }
        }
        catch (error) {
            player.sendMessage("§cError showing area details!");
        }
    }
    /**
     * Confirm area deletion
     */
    async confirmDeleteArea(player, areaId, areaName) {
        const form = new ActionFormData()
            .title("Confirm Delete")
            .body(`§cAre you sure you want to delete area "${areaName}"?\n\n§7This action cannot be undone!`)
            .button("§cDelete", "textures/ui/trash")
            .button("Cancel", "textures/ui/cancel");
        try {
            const response = await form.show(player);
            if (response.canceled || response.selection === 1) {
                player.sendMessage("§eDeletion cancelled.");
                return;
            }
            if (response.selection === 0) {
                const success = this.areaData.delete(areaId);
                if (success) {
                    player.sendMessage(`§aArea "${areaName}" deleted successfully!`);
                }
                else {
                    player.sendMessage("§cFailed to delete area!");
                }
            }
        }
        catch (error) {
            console.error(`Error confirming delete: ${error}`);
            player.sendMessage("§cError confirming deletion!");
        }
    }
    /**
     * Show delete confirmation for a specific area
     */
    async showDeleteConfirmation(player, areaData) {
        try {
            const form = new ActionFormData()
                .title("§cDelete Area")
                .body(`§eAre you sure you want to delete this area?\n\n§fArea Name: §b${areaData.data.name}\n§fCreated by: §a${areaData.data.createdBy}\n§fCreated: §7${new Date(areaData.data.createdAt).toLocaleString()}\n\n§c§lThis action cannot be undone!`)
                .button("§6§lConfirm Delete", "textures/ui/redX1")
                .button("§cCancel", "textures/ui/cancel");
            const response = await form.show(player);
            if (response.canceled || response.selection === undefined) {
                player.sendMessage("§7Area deletion cancelled.");
                return;
            }
            if (response.selection === 0) {
                // Area deletion is no longer supported - areas are hardcoded
                player.sendMessage("§cArea deletion is not supported.");
                player.sendMessage("§eAreas are now hardcoded in AreaData.ts");
            }
            else {
                // Cancel
                player.sendMessage("§7Area deletion cancelled.");
            }
        }
        catch (error) {
            player.sendMessage("§cError showing delete confirmation!");
        }
    }
}
