import { world, Player, EntityInventoryComponent, ItemStack, system } from '@minecraft/server';
import { Command } from '../../handler';
import { sendPlayerMessage, config } from '../../../config';
import { randomN } from '../../../lifesteal';

export function getHelp(player: Player) {
    sendPlayerMessage(player, `§e§lLifesteal Help Page -- Get§r§6
> See a player's heart count

> Usage: ${config.prefix}get <player>

> Examples: [
${config.prefix}get heart ${randomN(2, 4)}
${config.prefix}get heart_of_life ${randomN(3, 5)}
]`);
}

new Command({
    name: `get`,
    aliases: [`give`],
    staffOnly: true,
    code({ player, args }) {
        const acceptable = [
            `heart`,
            `heart_essence`,
            `heart_fragment`,
            `heart_of_life`,
            `beacon_of_life`,
            `withdraw_heart`,
        ];

        if (!args[0]) return getHelp(player);
        if (!acceptable.includes(args[0].toLowerCase())) return sendPlayerMessage(player, `§cThat isn't an item! Do ?help get for a list of items`);
        if (!args[1]) args[1] = `1`;
        const amount = +args[1];

        if (isNaN(amount)) return sendPlayerMessage(player, `§cThat isn't a number!`);

        switch (args[0]) {
            case `beacon_of_life`:
                try {
                    let inventory = player.getComponent(`inventory`) as EntityInventoryComponent;
                    let inventorySize = inventory.container.size;
                    for (let i = 0; i < inventorySize; i++) {
                        let item = inventory.container.getItem(i);
                        if (!item) continue;

                        let newItem = new ItemStack(`lifesteal:beacon_of_life`, item.amount);

                        if (item.typeId == `lifesteal:heart`) {
                            inventory.container.setItem(i, newItem);
                        }
                    }
                    player.runCommandAsync(`give @s lifesteal:beacon_of_life ${amount}`);
                } catch {
                    sendPlayerMessage(player, `§cFailed to give item`);
                }
                break;
            case `heart`:
                try {
                    let inventory = player.getComponent(`inventory`) as EntityInventoryComponent;
                    let inventorySize = inventory.container.size;
                    for (let i = 0; i < inventorySize; i++) {
                        let item = inventory.container.getItem(i);
                        if (!item) continue;

                        let newItem = new ItemStack(`lifesteal:heart`, item.amount);

                        if (item.typeId == `lifesteal:heart`) {
                            inventory.container.setItem(i, newItem);
                        }
                    }
                    player.runCommandAsync(`give @s lifesteal:heart ${amount}`);
                } catch {
                    sendPlayerMessage(player, `§cFailed to give item`);
                }
                break;
            case `heart_fragment`:
                try {
                    let inventory = player.getComponent(`inventory`) as EntityInventoryComponent;
                    let inventorySize = inventory.container.size;
                    for (let i = 0; i < inventorySize; i++) {
                        let item = inventory.container.getItem(i);
                        if (!item) continue;

                        let newItem = new ItemStack(`lifesteal:heart_fragment`, item.amount);

                        if (item.typeId == `lifesteal:heart_fragment`) {
                            inventory.container.setItem(i, newItem);
                        }
                    }
                    player.runCommandAsync(`give @s lifesteal:heart_fragment ${amount}`);
                } catch {
                    sendPlayerMessage(player, `§cFailed to give item`);
                }
                break;
            case `heart_essence`:
                try {
                    let inventory = player.getComponent(`inventory`) as EntityInventoryComponent;
                    let inventorySize = inventory.container.size;
                    for (let i = 0; i < inventorySize; i++) {
                        let item = inventory.container.getItem(i);
                        if (!item) continue;

                        let newItem = new ItemStack(`lifesteal:heart_essence`, item.amount);

                        if (item.typeId == `lifesteal:heart_essence`) {
                            inventory.container.setItem(i, newItem);
                        }
                    }
                    player.runCommandAsync(`give @s lifesteal:heart_essence ${amount}`);
                } catch {
                    sendPlayerMessage(player, `§cFailed to give item`);
                }
                break;
            case `heart_of_life`:
                try {
                    let inventory = player.getComponent(`inventory`) as EntityInventoryComponent;
                    let inventorySize = inventory.container.size;
                    for (let i = 0; i < inventorySize; i++) {
                        let item = inventory.container.getItem(i);
                        if (!item) continue;

                        let newItem = new ItemStack(`lifesteal:heart_of_life`, item.amount);

                        if (item.typeId == `lifesteal:heart_of_life`) {
                            inventory.container.setItem(i, newItem);
                        }
                    }
                    player.runCommandAsync(`give @s lifesteal:heart_of_life ${amount}`);
                } catch {
                    sendPlayerMessage(player, `§cFailed to give item`);
                }
                break;
            case `withdraw_heart`:
                try {
                    let inventory = player.getComponent(`inventory`) as EntityInventoryComponent;
                    let inventorySize = inventory.container.size;
                    for (let i = 0; i < inventorySize; i++) {
                        let item = inventory.container.getItem(i);
                        if (!item) continue;

                        let newItem = new ItemStack(`lifesteal:withdraw_heart`, item.amount);

                        if (item.typeId == `lifesteal:withdraw_heart`) {
                            inventory.container.setItem(i, newItem);
                        }
                    }
                    player.runCommandAsync(`give @s lifesteal:withdraw_heart ${amount}`);
                } catch {
                    sendPlayerMessage(player, `§cFailed to give item`);
                }
                break;
        }
    }
});