import * as mc from "@minecraft/server";
import * as utils from "../../utils/index";
import { Behavior, NPC, PopupMessage, PopupSystem } from "./npc_system";
import { showHeartSorterHint } from "./hints";
import { HEART_DATA } from "../../shared/heart_data";
const dialogue_scenes = [
    'craftee.dialogue.heartventory',
    'craftee.dialogue.heart_1',
    'craftee.dialogue.heart_2',
    'craftee.dialogue.heart_3',
    'craftee.dialogue.heart_4',
    'craftee.dialogue.heart_5',
    'craftee.dialogue.heart_6',
    'craftee.dialogue.heart_7',
    'craftee.dialogue.heart_8',
    'craftee.dialogue.heart_9',
    'craftee.dialogue.heart_10',
    'craftee.dialogue.heart_11',
    'craftee.dialogue.heart_12',
    'craftee.dialogue.heart_13',
    'craftee.dialogue.heart_14',
    'craftee.dialogue.heart_15',
    'craftee.dialogue.heart_16',
    'craftee.dialogue.heart_17',
    'craftee.dialogue.heart_18',
    'craftee.dialogue.heart_19',
    'craftee.dialogue.heart_20',
    'craftee.dialogue.heart_21',
    'craftee.dialogue.heart_22',
    'craftee.dialogue.heart_23',
    'craftee.dialogue.heart_24',
    'craftee.dialogue.heart_25',
    'craftee.dialogue.heart_26',
    'craftee.dialogue.heart_27',
    'craftee.dialogue.heart_28',
    'craftee.dialogue.heart_29',
    'craftee.dialogue.reward',
];
const popupMessages = [
    { tag: 'bee', message: new PopupMessage([{ message: 'craftee_new_heart.bee', sound: 'craftee.popup.heart_1', duration: 8.94 }]) },
    { tag: 'chicken', message: new PopupMessage([{ message: 'craftee_new_heart.chicken', sound: 'craftee.popup.heart_2', duration: 6.32 }]) },
    { tag: 'creeper', message: new PopupMessage([{ message: 'craftee_new_heart.creeper', sound: 'craftee.popup.heart_3', duration: 8.79 }]) },
    { tag: 'dolphin', message: new PopupMessage([{ message: 'craftee_new_heart.dolphin', sound: 'craftee.popup.heart_4', duration: 7.97 }]) },
    { tag: 'ender_dragon', message: new PopupMessage([{ message: 'craftee_new_heart.ender_dragon', sound: 'craftee.popup.heart_5', duration: 10.35 }]) },
    { tag: 'enderman', message: new PopupMessage([{ message: 'craftee_new_heart.enderman', sound: 'craftee.popup.heart_6', duration: 9.50 }]) },
    { tag: 'pig', message: new PopupMessage([{ message: 'craftee_new_heart.pig', sound: 'craftee.popup.heart_7', duration: 11.01 }]) },
    { tag: 'slime', message: new PopupMessage([{ message: 'craftee_new_heart.slime', sound: 'craftee.popup.heart_8', duration: 9.81 }]) },
    { tag: 'skeleton', message: new PopupMessage([{ message: 'craftee_new_heart.skeleton', sound: 'craftee.popup.heart_9', duration: 13.10 }]) },
    { tag: 'wither', message: new PopupMessage([{ message: 'craftee_new_heart.wither', sound: 'craftee.popup.heart_10', duration: 8.25 }]) },
    { tag: 'anvil', message: new PopupMessage([{ message: 'craftee_new_heart.anvil', sound: 'craftee.popup.heart_11', duration: 9.06 }]) },
    { tag: 'crafting_table', message: new PopupMessage([{ message: 'craftee_new_heart.crafting_table', sound: 'craftee.popup.heart_12', duration: 9.10 }]) },
    { tag: 'diamond', message: new PopupMessage([{ message: 'craftee_new_heart.diamond', sound: 'craftee.popup.heart_13', duration: 10.03 }]) },
    { tag: 'dirt', message: new PopupMessage([{ message: 'craftee_new_heart.dirt', sound: 'craftee.popup.heart_14', duration: 8.78 }]) },
    { tag: 'fire', message: new PopupMessage([{ message: 'craftee_new_heart.fire', sound: 'craftee.popup.heart_15', duration: 10.75 }]) },
    { tag: 'furnace', message: new PopupMessage([{ message: 'craftee_new_heart.furnace', sound: 'craftee.popup.heart_16', duration: 10.50 }]) },
    { tag: 'glowstone', message: new PopupMessage([{ message: 'craftee_new_heart.glowstone', sound: 'craftee.popup.heart_17', duration: 10.26 }]) },
    { tag: 'piston', message: new PopupMessage([{ message: 'craftee_new_heart.piston', sound: 'craftee.popup.heart_18', duration: 12.66 }]) },
    { tag: 'sapling', message: new PopupMessage([{ message: 'craftee_new_heart.sapling', sound: 'craftee.popup.heart_19', duration: 11.12 }]) },
    { tag: 'tnt', message: new PopupMessage([{ message: 'craftee_new_heart.tnt', sound: 'craftee.popup.heart_20', duration: 11.14 }]) },
    { tag: 'ancient_city', message: new PopupMessage([{ message: 'craftee_new_heart.ancient_city', sound: 'craftee.popup.heart_21', duration: 8.01 }]) },
    { tag: 'bastion', message: new PopupMessage([{ message: 'craftee_new_heart.bastion', sound: 'craftee.popup.heart_22', duration: 7.94 }]) },
    { tag: 'dungeon', message: new PopupMessage([{ message: 'craftee_new_heart.dungeon', sound: 'craftee.popup.heart_23', duration: 8.61 }]) },
    { tag: 'fortress', message: new PopupMessage([{ message: 'craftee_new_heart.fortress', sound: 'craftee.popup.heart_24', duration: 8.14 }]) },
    { tag: 'mansion', message: new PopupMessage([{ message: 'craftee_new_heart.mansion', sound: 'craftee.popup.heart_25', duration: 9.15 }]) },
    { tag: 'mineshaft', message: new PopupMessage([{ message: 'craftee_new_heart.mineshaft', sound: 'craftee.popup.heart_26', duration: 10.48 }]) },
    { tag: 'monument', message: new PopupMessage([{ message: 'craftee_new_heart.monument', sound: 'craftee.popup.heart_27', duration: 12.06 }]) },
    { tag: 'stronghold', message: new PopupMessage([{ message: 'craftee_new_heart.stronghold', sound: 'craftee.popup.heart_28', duration: 11.24 }]) },
    { tag: 'swamp_hut', message: new PopupMessage([{ message: 'craftee_new_heart.swamp_hut', sound: 'craftee.popup.heart_29', duration: 13.10 }]) },
    { tag: 'village', message: new PopupMessage([{ message: 'craftee_new_heart.village', sound: 'craftee.popup.heart_30', duration: 8.46 }]) },
    { tag: 'craftee_hearts_remaining.0', message: new PopupMessage([{ message: 'craftee_hearts_remaining.0', sound: 'craftee.popup.popup_30', duration: 7.38 }]) },
    { tag: 'craftee_hearts_remaining.1', message: new PopupMessage([{ message: 'craftee_hearts_remaining.1', sound: 'craftee.popup.popup_29', duration: 3.17 }]) },
    { tag: 'craftee_hearts_remaining.2', message: new PopupMessage([{ message: 'craftee_hearts_remaining.2', sound: 'craftee.popup.popup_28', duration: 4.04 }]) },
    { tag: 'craftee_hearts_remaining.3', message: new PopupMessage([{ message: 'craftee_hearts_remaining.3', sound: 'craftee.popup.popup_27', duration: 2.39 }]) },
    { tag: 'craftee_hearts_remaining.4', message: new PopupMessage([{ message: 'craftee_hearts_remaining.4', sound: 'craftee.popup.popup_26', duration: 3.23 }]) },
    { tag: 'craftee_hearts_remaining.5', message: new PopupMessage([{ message: 'craftee_hearts_remaining.5', sound: 'craftee.popup.popup_25', duration: 2.37 }]) },
    { tag: 'craftee_hearts_remaining.6', message: new PopupMessage([{ message: 'craftee_hearts_remaining.6', sound: 'craftee.popup.popup_24', duration: 3.16 }]) },
    { tag: 'craftee_hearts_remaining.7', message: new PopupMessage([{ message: 'craftee_hearts_remaining.7', sound: 'craftee.popup.popup_23', duration: 2.56 }]) },
    { tag: 'craftee_hearts_remaining.8', message: new PopupMessage([{ message: 'craftee_hearts_remaining.8', sound: 'craftee.popup.popup_22', duration: 1.91 }]) },
    { tag: 'craftee_hearts_remaining.9', message: new PopupMessage([{ message: 'craftee_hearts_remaining.9', sound: 'craftee.popup.popup_21', duration: 3.05 }]) },
    { tag: 'craftee_hearts_remaining.10', message: new PopupMessage([{ message: 'craftee_hearts_remaining.10', sound: 'craftee.popup.popup_20', duration: 4.50 }]) },
    { tag: 'craftee_hearts_remaining.11', message: new PopupMessage([{ message: 'craftee_hearts_remaining.11', sound: 'craftee.popup.popup_19', duration: 2.78 }]) },
    { tag: 'craftee_hearts_remaining.12', message: new PopupMessage([{ message: 'craftee_hearts_remaining.12', sound: 'craftee.popup.popup_18', duration: 3.13 }]) },
    { tag: 'craftee_hearts_remaining.13', message: new PopupMessage([{ message: 'craftee_hearts_remaining.13', sound: 'craftee.popup.popup_17', duration: 2.23 }]) },
    { tag: 'craftee_hearts_remaining.14', message: new PopupMessage([{ message: 'craftee_hearts_remaining.14', sound: 'craftee.popup.popup_16', duration: 3.13 }]) },
    { tag: 'craftee_hearts_remaining.15', message: new PopupMessage([{ message: 'craftee_hearts_remaining.15', sound: 'craftee.popup.popup_15', duration: 4.52 }]) },
    { tag: 'craftee_hearts_remaining.16', message: new PopupMessage([{ message: 'craftee_hearts_remaining.16', sound: 'craftee.popup.popup_14', duration: 2.43 }]) },
    { tag: 'craftee_hearts_remaining.17', message: new PopupMessage([{ message: 'craftee_hearts_remaining.17', sound: 'craftee.popup.popup_13', duration: 3.61 }]) },
    { tag: 'craftee_hearts_remaining.18', message: new PopupMessage([{ message: 'craftee_hearts_remaining.18', sound: 'craftee.popup.popup_12', duration: 2.97 }]) },
    { tag: 'craftee_hearts_remaining.19', message: new PopupMessage([{ message: 'craftee_hearts_remaining.19', sound: 'craftee.popup.popup_11', duration: 3.21 }]) },
    { tag: 'craftee_hearts_remaining.20', message: new PopupMessage([{ message: 'craftee_hearts_remaining.20', sound: 'craftee.popup.popup_10', duration: 3.63 }]) },
    { tag: 'craftee_hearts_remaining.21', message: new PopupMessage([{ message: 'craftee_hearts_remaining.21', sound: 'craftee.popup.popup_9', duration: 2.78 }]) },
    { tag: 'craftee_hearts_remaining.22', message: new PopupMessage([{ message: 'craftee_hearts_remaining.22', sound: 'craftee.popup.popup_8', duration: 3.17 }]) },
    { tag: 'craftee_hearts_remaining.23', message: new PopupMessage([{ message: 'craftee_hearts_remaining.23', sound: 'craftee.popup.popup_7', duration: 3.55 }]) },
    { tag: 'craftee_hearts_remaining.24', message: new PopupMessage([{ message: 'craftee_hearts_remaining.24', sound: 'craftee.popup.popup_6', duration: 3.28 }]) },
    { tag: 'craftee_hearts_remaining.25', message: new PopupMessage([{ message: 'craftee_hearts_remaining.25', sound: 'craftee.popup.popup_5', duration: 3.71 }]) },
    { tag: 'craftee_hearts_remaining.26', message: new PopupMessage([{ message: 'craftee_hearts_remaining.26', sound: 'craftee.popup.popup_4', duration: 4.44 }]) },
    { tag: 'craftee_hearts_remaining.27', message: new PopupMessage([{ message: 'craftee_hearts_remaining.27', sound: 'craftee.popup.popup_3', duration: 2.98 }]) },
    { tag: 'craftee_hearts_remaining.28', message: new PopupMessage([{ message: 'craftee_hearts_remaining.28', sound: 'craftee.popup.popup_2', duration: 2.28 }]) },
    { tag: 'craftee_hearts_remaining.29', message: new PopupMessage([{ message: 'craftee_hearts_remaining.29', sound: 'craftee.popup.popup_1', duration: 3.28, onComplete: showHeartSorterHint }]) },
];
let crafteeNPC;
export const crafteePopup = new PopupSystem("Craftee", popupMessages);
utils.waitForLoopEvent(20, () => {
    const entities = utils.overworld.getEntities({ type: 'ldz:craftee' });
    if (entities.length) {
        initializeCraftee(entities[0]);
        return true;
    }
    return false;
});
function initializeCraftee(craftee) {
    utils.setScore(craftee, 'DialogueCounter', 0);
    const dilaogueTransition = { name: 'dialogue', condition: ({ entity }) => entity.isValid() && entity.hasTag('inDialogue') };
    const popupTransition = { name: 'popup', condition: () => !utils.getScoreAsBool('.var', 'YouTube') && crafteePopup.getDialoguePending() };
    const idle = new Behavior({
        ID: 'idle',
        transitions: [
            dilaogueTransition,
            popupTransition,
            { name: 'wave', randomMin: 400, randomMax: 1200, condition: ({ age, random, entity }) => !utils.getScoreAsBool('.var', 'SeenTutorial') && (age > random || (entity.isValid() && entity.hasTag('forceEmote'))) },
            { name: 'emote', randomMin: 400, randomMax: 1200, condition: ({ age, random, entity }) => utils.getScoreAsBool('.var', 'SeenTutorial') && (age > random || (entity.isValid() && entity.hasTag('forceEmote'))) },
        ]
    });
    const wave = new Behavior({
        ID: 'wave',
        running: async (entity) => {
            const emote = { anim: 'animation.craftee.wave', sound: 'craftee.emote.beckon', duration: 5.0 };
            entity.runCommand(`playanimation @s ${emote.anim} idle 0.2`);
            if (!utils.getScoreAsBool('.var', 'YouTube')) {
                mc.world.playSound(emote.sound, entity.location);
            }
            await utils.waitForTicks(Math.ceil(emote.duration * 20));
        },
        onExit(entity) {
            entity.removeTag('forceEmote');
            mc.world.getAllPlayers().forEach(player => {
                player.runCommand(`stopsound @s craftee.emote.beckon`);
            });
        },
        transitions: [
            dilaogueTransition,
            popupTransition,
            { name: 'idle', condition: ({ finished }) => finished },
        ]
    });
    const emote = new Behavior({
        ID: 'emote',
        async onEntry(entity) {
            entity.runCommand(`playanimation @s animation.craftee.lookat_off`);
            await utils.waitForTicks(1);
        },
        running: async (entity) => {
            const emoteList = [
                { anim: 'animation.craftee.rasputin', sound: 'craftee.emote.rasputin', duration: 6.32 },
                { anim: 'animation.craftee.hit_the_whoa', sound: 'craftee.emote.hit_the_whoa', duration: 5.28 },
                { anim: 'animation.craftee.napoleon_dynamite', sound: 'craftee.emote.napoleon_dynamite', duration: 8.84 },
                { anim: 'animation.craftee.spongebob', sound: 'craftee.emote.spongebob', duration: 4.56 },
                { anim: 'animation.craftee.shuffle_dance', sound: 'craftee.emote.shuffle_dance', duration: 7.96 },
            ];
            const emote = emoteList[utils.getRandomInteger(0, emoteList.length - 1)];
            entity.runCommand(`playanimation @s ${emote.anim} idle 0.2`);
            if (!utils.getScoreAsBool('.var', 'YouTube')) {
                mc.world.playSound(emote.sound, entity.location);
            }
            await utils.waitForTicks(Math.ceil(emote.duration * 20));
        },
        onExit(entity) {
            entity.runCommand(`playanimation @s animation.craftee.lookat_on`);
            entity.removeTag('forceEmote');
            mc.world.getAllPlayers().forEach(player => {
                player.runCommand(`stopsound @s craftee.emote.rasputin`);
                player.runCommand(`stopsound @s craftee.emote.hit_the_whoa`);
                player.runCommand(`stopsound @s craftee.emote.napoleon_dynamite`);
                player.runCommand(`stopsound @s craftee.emote.spongebob`);
                player.runCommand(`stopsound @s craftee.emote.shuffle_dance`);
            });
        },
        transitions: [
            dilaogueTransition,
            popupTransition,
            { name: 'idle', condition: ({ finished }) => finished },
        ]
    });
    const popup = new Behavior({
        ID: 'popup',
        onEntry(entity) {
            entity.triggerEvent('set_state_to_talk');
            entity.runCommand('event entity @e[type=ldz:friendly_fire_toggle] set_notification_to_false');
        },
        transitions: [
            dilaogueTransition,
            { name: 'idle', condition: () => !crafteePopup.getDialoguePending() },
        ],
        async onExit(entity) {
            if (!entity.hasTag('inDialogue')) {
                entity.triggerEvent('set_state_to_idle');
            }
        },
    });
    const dialogue = new Behavior({
        ID: 'dialogue',
        onEntry() {
            crafteePopup.suspend();
        },
        transitions: [
            { name: 'idle', condition: ({ entity }) => entity.isValid() && !entity.hasTag('inDialogue') },
        ],
        onExit() {
            crafteePopup.resume();
        }
    });
    crafteeNPC = new NPC({ behaviorList: [idle, emote, popup, dialogue, wave], defaultBehavior: idle, npcEntity: craftee });
    mc.system.afterEvents.scriptEventReceive.subscribe(event => {
        if (event.id === 'ldz:talk_to_craftee' && event.sourceEntity) {
            switch (event.message) {
                case 'close_dialogue':
                    closeCrafteeDialogue(event.sourceEntity);
                    break;
                default:
                    playCrafteeDialogue(event.sourceEntity);
                    break;
            }
        }
    });
}
function closeCrafteeDialogue(player) {
    utils.waitForEvent(1, () => {
        if (utils.getScore(crafteeNPC.entity, 'DialogueCounter') <= 0) {
            utils.mclog('No one is talking to Craftee');
            crafteeNPC.entity.removeTag('inDialogue');
            crafteeNPC.entity.triggerEvent('on_finished_interact');
        }
    });
}
function playCrafteeDialogue(player) {
    utils.mclog(`${player.nameTag} is talking to Craftee`);
    const count = utils.getScore(player, `Obtained_Count`);
    const scene = dialogue_scenes[utils.clamp(count, 0, dialogue_scenes.length - 1)];
    utils.setScore('.var', 'SeenTutorial', 1);
    crafteeNPC.entity.addTag('inDialogue');
    crafteeNPC.entity.runCommand(`dialogue open @e[type=ldz:craftee,c=1] "${player.nameTag}" ${scene}`);
}
export function playUnobtainedHeartsMessage(player) {
    const maxHearts = 30; // The maximum number of hearts Craftee has messages for
    let unobtainedCount = maxHearts;
    for (let index = 1; index <= maxHearts; index++) {
        crafteePopup.removeDialogueTag(player, `craftee_hearts_remaining.${index}`);
        utils.mclog(`Has ${HEART_DATA[index].displayName}: ${utils.getScoreAsBool(player, `Obtained_${index}`)}`);
        unobtainedCount -= player.getScore(`Obtained_${index}`);
    }
    player.setScore(`Obtained_Count`, maxHearts - unobtainedCount);
    crafteePopup.addDialogueTag(player, `craftee_hearts_remaining.${unobtainedCount}`);
    utils.mclog(`Adding craftee_hearts_remaining.${unobtainedCount} to ${player.nameTag}`);
}
