import { Player, system, world } from '@minecraft/server';
import { ActionFormData } from '@minecraft/server-ui';
function getRandomInt(min, max) {
    const minCeiled = Math.ceil(min);
    const maxFloored = Math.floor(max);
    return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
}
world.afterEvents.entitySpawn.subscribe((data) => {
    const entity = data.entity;
    if (!(entity.typeId.includes("npc")))
        return;
    const seller_npc = ["Taylor (Receptionist)", "Noah (Waitstaff)", "Oliver (Barista)", "Charlie (Vendor)", "Joseph (Server)", "William (Salesperson)", "Isabella (Receptionist)", "Emma (Waitstaff)", "Carroll (Barista)", "Olivia (Vendor)", "Morgan (Server)", "Mary (Salesperson)"];
    const regular_npc = [
        "Abby",
        "Adrian",
        "Alex",
        "Ali",
        "Andie",
        "Angel",
        "Ariel",
        "Billie",
        "Blake",
        "Blue",
        "Brent",
        "Brooke",
        "Carey",
        "Carroll",
        "Casey",
        "Charlie",
        "Chris",
        "Ellis",
        "Erin",
        "Frankie",
        "Gabriel",
        "Grey",
        "Harley",
        "Jade",
        "Jesse",
        "Kim",
        "Leah",
        "Lou",
        "Marley",
        "Morgan",
        "Noel",
        "Quinn",
        "Ray",
        "Remy",
        "Riley",
        "Robin",
        "Sam",
        "Shiloh",
        "Sunny",
        "Taylor",
        "Terry",
        "Venice",
        "Wren"
    ];
    const old_male_npc = [
        "John",
        "William",
        "James",
        "Robert",
        "Charles",
        "George",
        "Richard",
        "Joseph",
        "Edward",
        "Thomas",
        "Frank",
        "Henry",
        "Walter",
        "Harold",
        "Arthur",
        "Albert",
        "Paul",
        "Raymond",
        "Donald",
        "Jack"
    ];
    const old_famale_npc = [
        "Mary",
        "Margaret",
        "Helen",
        "Dorothy",
        "Ruth",
        "Betty",
        "Barbara",
        "Shirley",
        "Patricia",
        "Virginia",
        "Joan",
        "Frances",
        "Alice",
        "Elizabeth",
        "Evelyn",
        "Jean",
        "Doris",
        "Rose",
        "Martha",
        "Gloria"
    ];
    if (entity.typeId == "ph_furn:seller_npc") {
        if (entity.hasTag("ph_furn:seller_npc_1")) {
            entity.nameTag = seller_npc[0];
        }
        if (entity.hasTag("ph_furn:seller_npc_2")) {
            entity.nameTag = seller_npc[1];
        }
        if (entity.hasTag("ph_furn:seller_npc_3")) {
            entity.nameTag = seller_npc[2];
        }
        if (entity.hasTag("ph_furn:seller_npc_4")) {
            entity.nameTag = seller_npc[3];
        }
        if (entity.hasTag("ph_furn:seller_npc_5")) {
            entity.nameTag = seller_npc[4];
        }
		if (entity.hasTag("ph_furn:seller_npc_6")) {
            entity.nameTag = seller_npc[5];
        }
		if (entity.hasTag("ph_furn:seller_npc_7")) {
            entity.nameTag = seller_npc[6];
        }
		if (entity.hasTag("ph_furn:seller_npc_8")) {
            entity.nameTag = seller_npc[7];
        }
		if (entity.hasTag("ph_furn:seller_npc_9")) {
            entity.nameTag = seller_npc[8];
        }
		if (entity.hasTag("ph_furn:seller_npc_10")) {
            entity.nameTag = seller_npc[9];
        }
		if (entity.hasTag("ph_furn:seller_npc_11")) {
            entity.nameTag = seller_npc[10];
        }
		if (entity.hasTag("ph_furn:seller_npc_12")) {
            entity.nameTag = seller_npc[11];
        }
    }
    if (entity.typeId == "ph_furn:npc.male_npc") {
        if (entity.hasTag("ph_furn:old_male_npc")) {
            entity.nameTag = old_male_npc[getRandomInt(0, 19)];
        }
        if (entity.hasTag("ph_furn:male_npc")) {
            entity.nameTag = regular_npc[getRandomInt(0, 42)];
        }
    }
    if (entity.typeId == "ph_furn:npc.female_npc") {
        if (entity.hasTag("ph_furn:old_famale_npc")) {
            entity.nameTag = old_famale_npc[getRandomInt(0, 19)];
        }
        if (entity.hasTag("ph_furn:female_npc")) {
            entity.nameTag = regular_npc[getRandomInt(0, 42)];
        }
    }
});
system.afterEvents.scriptEventReceive.subscribe((xm) => {
    const player = xm.sourceEntity;
    const form = new ActionFormData()
        .title({ translate: 'ph_furn.furniture.npc.guide' })
        .body({ "rawtext": [{ translate: `ph_furn.furniture.npc.body` }] })
        .button({ translate: 'ph_furn.furniture.npc.stay' }, "textures/pixelhead/furniture/ui/ph_furn/stay")
        .button({ translate: 'ph_furn.furniture.npc.wander' }, "textures/pixelhead/furniture/ui/ph_furn/wander")
        .button({ translate: 'ph_furn.furniture.npc.follow' }, "textures/pixelhead/furniture/ui/ph_furn/follow")
        .button({ translate: 'ph_furn.furniture.npc.sitting' }, "textures/pixelhead/furniture/ui/ph_furn/sit");
    if (xm.id == "ph_furn:player") {
        if (player instanceof Player) {
            player.playSound("random.click");
            form.show(player).then((response) => {
                if (response.canceled)
                    return;
                const overworld = world.getDimension("overworld");
                const nether = world.getDimension("nether");
                const the_end = world.getDimension("the_end");
                const entities = [...overworld.getEntities(), ...nether.getEntities(), ...the_end.getEntities()];
                entities.forEach((entity) => {
                    if (entity.hasTag("ph_furn:npc")) {
                        entity.addTag(`${player.id}`);
                        entity.removeTag("ph_furn:npc");
                    }
                    if (entity.hasTag(`${player.id}`)) {
                        if (response.selection === 0) {
                            entity.triggerEvent('stay');
                            entity.runCommand("/ride @s stop_riding");
                        }
                        if (response.selection === 1) {
                            entity.triggerEvent('wander');
                            entity.runCommand("/ride @s stop_riding");
                        }
                        if (response.selection === 2) {
                            entity.triggerEvent('follow');
                            entity.runCommand("/ride @s stop_riding");
                        }
                        if (response.selection === 3) {
                            entity.runCommand("/event entity @s sitting");
                            entity.runCommand("/ride @s summon_ride ph_furn:ride no_ride_change look_at_player");
                        }
                        entity.removeTag(`${player.id}`);
                    }
                });
            });
        }
    }
});
