import { system } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { getDimension, getHand } from "./utils";
import { Painter } from "../modules/paintMachine";
import { CustomColors, PlaneEntity } from "../modules/planes";
export function changeColor(player, entity) {
    const planeColors = Object.values(CustomColors);
    const form = new ModalFormData();
    form.title("Change Color");
    form.dropdown("Change Color", planeColors);
    form.show(player).then((data) => {
        if (data.canceled || data.formValues == undefined)
            return;
        const selectedColor = planeColors[data.formValues[0]]; //@ts-ignore
        PlaneEntity.setColor(entity, selectedColor);
    });
}
export function painterMachineMain(player, entity) {
    let currentColor = Painter.getColor(entity) ?? "No Color Selected";
    currentColor = (currentColor.charAt(0).toUpperCase() + currentColor.slice(1))
        .replace("_skin", "")
        .replace("_", " ");
    const form = new ActionFormData();
    form.title("Painter Machine");
    form.body(`§7Welcome to the Painter Machine!\n\n§8If you want to know how to use this machine, please press the §bInformation Button.§8\n\n§7Selected Color:\n§8${currentColor}\n`);
    form.button("§9Add Color\n§8Click Here");
    form.button("§9Reset Color\n§8Click Here");
    form.button("§9Information\n§8Click Here");
    form.button("§9Paint Vehicle\n§8Click Here");
    form.button("§l§cExit Menu§r\n§8Click Here");
    form.show(player).then((data) => {
        if (data.canceled || data.selection == undefined)
            return;
        switch (data.selection) {
            case 0:
                const hand = getHand(player);
                if (!hand || !Painter.isDyeItem(hand)) {
                    return player.sendMessage("§8[§6!§8] §cYou need to hold a Spray Can in your hand to add color!");
                }
                const dyeColor = Painter.getDyeColor(hand);
                if (!dyeColor) {
                    return player.sendMessage("§8[§6!§8] §cInvalid Spray Can color!");
                }
                Painter.takeDye(player, hand);
                Painter.setColor(entity, dyeColor);
                break;
            case 1:
                Painter.emptyColor(entity);
                break;
            case 2:
                paintInformation(player, entity);
                break;
            case 3:
                if (!Painter.getColor(entity)) {
                    return player.sendMessage("§8[§6!§8] §cNo color selected for the vehicle.\n§8Please add a color first before attempting to paint a vehicle.§r");
                }
                selectVehicle(player, entity);
                break;
            default:
                return;
                break;
        }
    });
}
function paintInformation(player, entity) {
    const form = new ActionFormData();
    form.title("Paint Information");
    form.body(`§7Welcome to the Vehicle Painter Machine!\n\n§8Please hold a spray can of choice and then select the §bAdd Color Button§8.\n\n§8Once a color has been added onto the machine\n\nYou are then able to paint the vehicle.\n\n§8You can paint any §bCar§8 or §bPlane§8.\n\n§8Each Vehicle must be on the designated zone.\n\n§8Cars are on the §bYellow Zone§8 and planes on the §bOrange Zone§8.\n\n§8When you are ready to paint the vehicle, please press the §bpaint vehicle button§8.\n\n§8You can purchase Spray Cans from the Painter NPC next to the painting Machine.`);
    form.button("§l§cBack§r\n§8Click Here");
    form.show(player).then((data) => {
        if (data.canceled || data.selection == undefined)
            return;
        painterMachineMain(player, entity);
    });
}
function selectVehicle(player, painter) {
    const form = new ModalFormData();
    form.title("Select Vehicle");
    form.dropdown("Select Vehicle", ["Plane", "Car"]);
    form.show(player).then((data) => {
        if (data.canceled || data.formValues == undefined)
            return;
        const selectedVehicle = data.formValues[0];
        const vehicleType = selectedVehicle === 0 ? "plane" : "car";
        const location = selectedVehicle === 0 ? { x: 386, y: 72, z: -15 } : { x: 379, y: 72, z: -24 };
        const validTypes = selectedVehicle === 0 ? ["model:plane_1", "model:plane_2"] : ["model:fastcar", "model:suv"];
        const zoneName = selectedVehicle === 0 ? "plane" : "car";
        const vehicles = getDimension().getEntities({
            location,
            maxDistance: 5,
        });
        for (const entity of vehicles) {
            if (!validTypes.includes(entity.typeId)) {
                return player.sendMessage(`§8[§6!§8] §cInvalid vehicle found at the §b${zoneName}§c painting zone!\n§8Please drive the §b${zoneName}§8 to the painting zone.§r`);
            }
        }
        if (vehicles.length == 0) {
            return player.sendMessage(`§8[§6!§8] §cNo ${zoneName} found at the §b${zoneName}§c painting zone.\n§8Please drive the §b${zoneName}§8 to the painting zone.§r`);
        }
        if (vehicles.length > 1) {
            return player.sendMessage(`§8[§6!§8] §cMultiple §b${zoneName}s §cfound at the §b${zoneName}§c painting zone.\n§8Please drive only one §b${zoneName}§8 to the painting zone.`);
        }
        paintConfirm(player, vehicles[0], painter, vehicleType);
    });
}
function paintConfirm(player, vehicle, painter, type) {
    const form = new ActionFormData();
    const color = Painter.getColor(painter);
    if (!color) {
        player.sendMessage("§8[§6!§8] §cNo color selected for the vehicle.\n§8Please add a color first before attempting to paint a vehicle.");
        return;
    }
    form.title("Paint Vehicle");
    form.body("Are you sure you want to paint the vehicle?");
    form.button("Yes");
    form.button("No");
    form.show(player).then((data) => {
        if (data.canceled || data.selection == undefined)
            return;
        if (data.selection == 0) {
            const location = vehicle.location;
            const particleType = vehicle.typeId.startsWith("model:plane") ? "wildphire:painted_large" : "wildphire:painted_small";
            getDimension().runCommandAsync(`particle "${particleType}" ${location.x} ${location.y} ${location.z}`);
            getDimension().runCommandAsync(`playanimation @e[type=model:painter,x=374,y=73,z=-17,c=1] "animation.painter.new"`);
            system.runTimeout(() => {
                const painterEntity = getDimension().getEntities({ type: "model:painter" })[0];
                if (!painterEntity)
                    return;
                player.sendMessage(`§8[§b!§8] §eYou have just painted the §b${type}§e.`);
                PlaneEntity.setColor(vehicle, color);
                Painter.emptyColor(painterEntity);
            }, 90);
        }
    });
}
