import Biome_Manager from "../Biome/Biome_Manager";
import Progression_Manager from "../Progression/Progression_Manager";
import Form_Dialog_Manager from "../Form/Managers/Form_Dialog_Manager";
import Entity_Inventory_Manager from "../Entity/Managers/Entity_Inventory_Manager";
import Item_Manager from "../Item/Item_Manager";
export default class Quest_Manager {
    static get(location) {
        return Biome_Manager.get(Biome_Manager.get_Area_ID(location)).quest;
    }
    static async dialog(player, entity, name, texture) {
        const quest = Biome_Manager.get(Biome_Manager.get_Area_ID(player.location)).quest;
        const progression = Progression_Manager.get(quest.id);
        const task = quest.tasks[progression.active_Task];
        if (progression.active_Task == 3) {
            const dialog = {
                name: name,
                texture: texture,
                messages: ["You've completed all the quests on this island."],
                buttons: [
                    {
                        text: "Okay",
                        async callback(player, entity) { },
                    },
                ],
            };
            await Form_Dialog_Manager.show(player, dialog, 0, entity);
            return;
        }
        if (task.collect != undefined) {
            const collect = "§eCollect§f:\n   " + task.collect.amount + " " + Item_Manager.info(task.collect.id).name;
            let rewards = "§eRewards§f:";
            for (let i = 0; i < task.rewards.length; i++) {
                rewards = rewards + "\n   " + task.rewards[i].amount + " " + Item_Manager.info(task.rewards[i].id).name;
            }
            const dialog = {
                name: name,
                texture: texture,
                messages: ["I need you to collect this item to help unlock the raid.\n\n" + collect + "\n" + rewards],
                buttons: [
                    {
                        text: "I have the items",
                        async callback(player, entity) {
                            if (Entity_Inventory_Manager.reduce(player, task.collect.id, task.collect.amount) == false) {
                                const dialog = {
                                    name: name,
                                    texture: texture,
                                    messages: ["You don't have enough of the items."],
                                    buttons: [{ text: "Okay", async callback(player, entity) { } }],
                                };
                                await Form_Dialog_Manager.show(player, dialog, 0, entity);
                                return;
                            }
                            progression.active_Task = progression.active_Task + 1;
                            for (let i = 0; i < task.rewards.length; i++) {
                                if (Entity_Inventory_Manager.empty_Slots(player) > 0) {
                                    Entity_Inventory_Manager.add_Item(player, task.rewards[i]);
                                }
                                else {
                                    player.dimension.spawnItem(Item_Manager.new(task.rewards[i]), player.location);
                                }
                            }
                        },
                    },
                ],
            };
            await Form_Dialog_Manager.show(player, dialog, 0, entity);
        }
        else if (task.defeat != undefined) {
            const amount = task.defeat.amount - progression.defeated <= 0 ? 0 : task.defeat.amount - progression.defeated;
            const defeat = "§eDefeat§f:\n   " + String(amount) + " " + Item_Manager.info(task.defeat.id).name;
            let rewards = "§eRewards§f:";
            for (let i = 0; i < task.rewards.length; i++) {
                rewards = rewards + "\n   " + task.rewards[i].amount + " " + Item_Manager.info(task.rewards[i].id).name;
            }
            const dialog = {
                name: name,
                texture: texture,
                messages: [
                    "I need you to defeat this entity on this island to help unlock the raid.\n\n" + defeat + "\n" + rewards,
                ],
                buttons: [
                    {
                        text: "All Done!",
                        async callback(player, entity) {
                            if (progression.defeated < task.defeat.amount) {
                                const dialog = {
                                    name: name,
                                    texture: texture,
                                    messages: ["Still need to defeat more entities."],
                                    buttons: [{ text: "Okay", async callback(player, entity) { } }],
                                };
                                await Form_Dialog_Manager.show(player, dialog, 0, entity);
                                return;
                            }
                            progression.active_Task = progression.active_Task + 1;
                            for (let i = 0; i < task.rewards.length; i++) {
                                if (Entity_Inventory_Manager.empty_Slots(player) > 0) {
                                    Entity_Inventory_Manager.add_Item(player, task.rewards[i]);
                                }
                                else {
                                    player.dimension.spawnItem(Item_Manager.new(task.rewards[i]), player.location);
                                }
                            }
                        },
                    },
                ],
            };
            await Form_Dialog_Manager.show(player, dialog, 0, entity);
        }
        Progression_Manager.set(progression);
    }
    static defeated(entity) {
        const quest = Biome_Manager.get(Biome_Manager.get_Area_ID(entity.location)).quest;
        const progression = Progression_Manager.get(quest.id);
        const task = quest.tasks[progression.active_Task];
        if (task == undefined)
            return;
        if (task.defeat == undefined)
            return;
        if (entity.typeId != task.defeat.id)
            return;
        progression.defeated = progression.defeated + 1;
        Progression_Manager.set(progression);
    }
}
