import {
	EntityEquippableComponent,
	EntityInventoryComponent,
	EquipmentSlot,
	GameMode,
	ItemStack,
	system,
} from "@minecraft/server";
import { Piki } from "../../utils/typingConstants";
import { arrayIncludes, testing } from "../../utils/systemHelper";

export const REPLACED_ITEMS = {
	_equipmentSlots: [
		EquipmentSlot.Chest,
		EquipmentSlot.Feet,
		EquipmentSlot.Head,
		EquipmentSlot.Legs,
		EquipmentSlot.Offhand,
	],
	_supportedItems: {
		skull: {
			suffix: "_placer",
			lore: "Use Item to wear / equip",
			isBlock: true,
		},
		head: {
			suffix: "_placer",
			lore: "Use Item to wear / equip",
			isBlock: true,
		},
		skull_placer: {
			suffix: "",
			lore: "Use Item to wear / equip",
			isBlock: true,
		},
		head_placer: {
			suffix: "",
			lore: "Use Item to wear / equip",
			isBlock: true,
		},
		slab: {
			suffix: "_placer",
			lore: false,
			isBlock: true,
		},
		torch: {
			suffix: "_item",
			lore: false,
			isBlock: true,
		},
		purple_rose: {
			suffix: "_item",
			lore: false,
			isBlock: true,
		},
		glacial_necklace: {
			suffix: "",
			lore: "§e25% §8chance of §bslowing §8attacked entities.",
			isBlock: false,
		},
		infernal_necklace: {
			suffix: "",
			lore: "§e25% §8chance of §cburning §8attacked entities.",
			isBlock: false,
		},
		duskite_chestplate: {
			suffix: "",
			lore: "§8Equip full set to vanish the §5shadows§8.",
			isBlock: false,
		},
		duskite_helmet: {
			suffix: "",
			lore: "§8Equip full set to vanish the §5shadows§8.",
			isBlock: false,
		},
		duskite_leggings: {
			suffix: "",
			lore: "§8Equip full set to vanish the §5shadows§8.",
			isBlock: false,
		},
		duskite_boots: {
			suffix: "",
			lore: "§8Equip full set to vanish the §5shadows§8.",
			isBlock: false,
		},
		venomite_chestplate: {
			suffix: "",
			lore: "§e25% §8chance to §2poison §8attackers",
			isBlock: false,
		},
		venomite_helmet: {
			suffix: "",
			lore: "§e25% §8chance to §2poison §8attackers",
			isBlock: false,
		},
		venomite_leggings: {
			suffix: "",
			lore: "§e25% §8chance to §2poison §8attackers",
			isBlock: false,
		},
		venomite_boots: {
			suffix: "",
			lore: "§e25% §8chance to §2poison §8attackers",
			isBlock: false,
		},
		cobaltium_chestplate: {
			suffix: "",
			lore: "§8Equip full set to get §ehaste§8.",
			isBlock: false,
		},
		cobaltium_helmet: {
			suffix: "",
			lore: "§8Equip full set to get §ehaste§8.",
			isBlock: false,
		},
		cobaltium_leggings: {
			suffix: "",
			lore: "§8Equip full set to get §ehaste§8.",
			isBlock: false,
		},
		cobaltium_boots: {
			suffix: "",
			lore: "§8Equip full set to get §ehaste§8.",
			isBlock: false,
		},
		venomite_pickaxe: {
			suffix: "",
			lore: "§eDestroys §8nearby blocks §2after 3 seconds§8.",
			isBlock: false,
		},
		venomite_axe: {
			suffix: "",
			lore: "§eDestroys §8nearby blocks §2after 3 seconds§8.",
			isBlock: false,
		},
		venomite_shovel: {
			suffix: "",
			lore: "§eDestroys §8nearby blocks §2after 3 seconds§8.",
			isBlock: false,
		},
		venomite_hoe: {
			suffix: "",
			lore: "§eDestroys §8nearby blocks §2after 3 seconds§8.",
			isBlock: false,
		},
		venomite_sword: {
			suffix: "",
			lore: "§eDestroys §8nearby blocks §2after 3 seconds§8.",
			isBlock: false,
		}
	},

	/**
	 *
	 * @param {EntityInventoryComponent} inventory
	 * @param {EntityEquippableComponent} equippable
	 * @returns
	 */
	runReplace: function (inventory, equippable) {
		// const start = Date.now()
		let success = 0;
		for (const slot of this._equipmentSlots) {
			const item = equippable.getEquipment(slot);
			if (!item) continue;

			const itemTypeId = item.typeId;
			if (!item.typeId.startsWith(Piki.namespace)) continue;

            for (const supportedId in this._supportedItems) {
                const suffix = this._supportedItems[supportedId].suffix;
                if (!itemTypeId.includes(supportedId)) continue;
                const itemPlacerLore = this._supportedItems[supportedId].lore;
                const isBlock = this._supportedItems[supportedId].isBlock;
                const oldLore = item.getLore();
                if (!suffix && suffix != "") continue;
                if (!suffix && arrayIncludes(oldLore, itemPlacerLore)) continue;
                if (!suffix && isBlock) continue;

                try {
                    const newItem = new ItemStack(itemTypeId + suffix);
                    newItem.amount = item.amount;

                    if (itemPlacerLore) {
                        oldLore.push(itemPlacerLore);
                        newItem.setLore(oldLore);
                    }

                    equippable.setEquipment(slot, newItem);
                    success++;
                } catch (err) {
                    // console.warn(err)
                    continue;
                }
            }
		}

		const container = inventory.container;
		if (!container) return;

		for (let slot = 0; slot < container.size; slot++) {
			const item = container.getItem(slot);

			if (!item) continue;

			const itemTypeId = item.typeId;
			if (!item.typeId.startsWith(Piki.namespace)) continue;

            for (const supportedId in this._supportedItems) {
                const suffix = this._supportedItems[supportedId].suffix;
                if (!itemTypeId.includes(supportedId)) continue;
                const itemPlacerLore = this._supportedItems[supportedId].lore;
                const oldLore = item.getLore();
                if (!suffix && suffix != "") continue;
                if (arrayIncludes(oldLore, itemPlacerLore)) continue;

                try {
                    const newItem = new ItemStack(itemTypeId + suffix);
                    newItem.amount = item.amount;
                    if (itemPlacerLore) {
                        oldLore.push(itemPlacerLore);
                        newItem.setLore(oldLore);
                    }

                    container.setItem(slot, newItem);
                    success++;
                } catch (err) {
                    // console.warn(err)
                    continue;
                }
            }
		}
		// const end = Date.now()
		// if (success) testing.success("Successfully replaced", success, "item/s. Took", Math.floor((end - start) / 1000), "ms")
	},
};
