import * as mc from "@minecraft/server";
import { getRandomQuantity, getMultipleLoot } from "../GlobalVars";
export function add_items(eventData) {
    const id = eventData.id;
    const sourceEntity = eventData.sourceEntity;
    if (id === `cy_cr:add_cobblestone` && sourceEntity && sourceEntity.typeId === `cy_cr:cobble_factory`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`cobblestone`));
    };
    if (id === `cy_cr:add_wood` && sourceEntity && sourceEntity.typeId === `cy_cr:wood_factory`) {
        function sapling() {
            sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`oak_sapling`, 1));
            sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`oak_log`, 5));
        }
        const lootState = sourceEntity?.getProperty(`cy_cr:loot`) || "none";
        if (lootState === `sapling`) {
            sapling()
        } else if (lootState === `stick`) {
            sapling();
            sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`stick`, getRandomQuantity(1,3)));
        } else if (lootState === `apple`) {
            sapling();
            sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`apple`, getRandomQuantity(1,3)));
        } else if (lootState === `stick_apple`) {
            sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`stick`, getRandomQuantity(1,3)));
            sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`apple`, getRandomQuantity(1,3)));
        }
    };
    if (id === `cy_cr:add_potion` && sourceEntity && sourceEntity.typeId === `cy_cr:potion_farm`) {
        const lootTable = [
            { weight: 2, type: "item", value: "minecraft:glowstone_dust", quantity: getRandomQuantity(1, 2) },
            { weight: 2, type: "item", value: "minecraft:sugar", quantity: getRandomQuantity(1, 2) },
            { weight: 2, type: "item", value: "minecraft:redstone", quantity: getRandomQuantity(1, 2) },
            { weight: 2, type: "item", value: "minecraft:spider_eye", quantity: getRandomQuantity(1, 2) },
            { weight: 2, type: "item", value: "minecraft:glass_bottle", quantity: getRandomQuantity(1, 2) },
            { weight: 2, type: "item", value: "minecraft:gunpowder", quantity: getRandomQuantity(1, 2) },
            { weight: 2, type: "item", value: "minecraft:stick", quantity: getRandomQuantity(1, 2) },

            { weight: 1, type: "action", actionName: "getPotion" }
        ]
        const randomLoots = getMultipleLoot(lootTable, 1, 3);
        randomLoots.forEach(randomLoot => {
            if (randomLoot.type === `item`) {
                const item = new mc.ItemStack(randomLoot.value, randomLoot.quantity);
                sourceEntity?.getComponent('inventory').container?.addItem(item);
            } else if (randomLoot.type === `action`) {
                if (randomLoot.value === `getPotion`) {
                    const data = getRandomQuantity(5, 42);
                    const inv = sourceEntity?.getComponent("inventory").container;
                    let slot = 0;
                    for (let i = 0; i < inv.size; i++) {
                        const item = inv.getItem(i);
                        slot = i;
                        if (item === undefined) break;
                    }
                    sourceEntity?.runCommandAsync(`loot replace entity @s slot.inventory ${slot} loot "cy/cr/potion_${data}"`)
                }
            };
        })
    }
    if (id === `cy_cr:add_arrow` && sourceEntity && sourceEntity.typeId === `cy_cr:arrow_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:arrow`, 2));
    };
    if (id === `cy_cr:add_berry` && sourceEntity && sourceEntity.typeId === `cy_cr:berry_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:sweet_berries`, 9));
    };
    if (id === `cy_cr:add_beetroot` && sourceEntity && sourceEntity.typeId === `cy_cr:beetroot_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:beetroot`, getRandomQuantity(9, 27)));
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:beetroot_seeds`, getRandomQuantity(1, 27)));
    };
    if (id === `cy_cr:add_carrot` && sourceEntity && sourceEntity.typeId === `cy_cr:carrot_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:carrot`, getRandomQuantity(9, 27)));
    };
    if (id === `cy_cr:add_netherwart` && sourceEntity && sourceEntity.typeId === `cy_cr:netherwart_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:nether_wart`, getRandomQuantity(9, 27)));
    };
    if (id === `cy_cr:add_potato` && sourceEntity && sourceEntity.typeId === `cy_cr:potato_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:potato`, getRandomQuantity(9, 27)));
    };
    if (id === `cy_cr:add_wheat` && sourceEntity && sourceEntity.typeId === `cy_cr:wheat_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:wheat`, getRandomQuantity(9, 27)));
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:wheat_seeds`, getRandomQuantity(9, 27)));
    };
    if (id === `cy_cr:add_sugar_cane` && sourceEntity && sourceEntity.typeId === `cy_cr:sugar_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:sugar_cane`, 12));
    };
    if (id === `cy_cr:add_tear` && sourceEntity && sourceEntity.typeId === `cy_cr:tear_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:ghast_tear`, 1));
    };
    if (id === `cy_cr:add_egg` && sourceEntity && sourceEntity.typeId === `cy_cr:eggs_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:egg`, 1));
    };
    if (id === `cy_cr:add_cake` && sourceEntity && sourceEntity.typeId === `cy_cr:cake_farm`) {
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:cake`));
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:sugar_cane`, 9));
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:wheat_seeds`, getRandomQuantity(1, 15)));
        sourceEntity.getComponent('inventory').container?.addItem(new mc.ItemStack(`minecraft:sweet_berries`, getRandomQuantity(4, 7)));
    };
};