import * as mc from "@minecraft/server";
import * as utils from "./utils/index";
import { botThatDoesntUseFuel, lineColor, loreLine } from "./bots/components/fuel_on_spawn";
import { DRILL_FUEL_ITEM, DRILL_MAX_FUEL } from "./bots/bot_types/drill";
import { GUARD_FUEL_ITEM, GUARD_MAX_FUEL } from "./bots/bot_types/guard";
import { HOVER_FUEL_ITEM, HOVER_MAX_FUEL } from "./bots/bot_types/hover";
//import { LUMBER_FUEL_ITEM, LUMBER_MAX_FUEL } from "./bots/bot_types/lumber";
import { MEDIC_FUEL_ITEM, MEDIC_MAX_FUEL } from "./bots/bot_types/medic";
import { MINER_FUEL_ITEM, MINER_MAX_FUEL } from "./bots/bot_types/miner";
import { TELEPORT_FUEL_ITEM, TELEPORT_MAX_FUEL } from "./bots/bot_types/portal";
import { SPRINKLER_FUEL_ITEM, SPRINKLER_MAX_FUEL } from "./bots/bot_types/sprinkler";
import { TORCH_FUEL_ITEM, TORCH_MAX_FUEL } from "./bots/bot_types/torch";
import { INIT_FUEL_PERCENT } from "./bots/components/fuel_on_spawn";
const LOG_TAG = "logLore";
const REPLACE_INTERVAL = 2 * mc.TicksPerSecond;
// #region fuel and item name info
const drill_initial_fuel = Math.round(DRILL_MAX_FUEL * INIT_FUEL_PERCENT);
const guard_initial_fuel = Math.round(GUARD_MAX_FUEL * INIT_FUEL_PERCENT);
const hover_initial_fuel = Math.round(HOVER_MAX_FUEL * INIT_FUEL_PERCENT);
//const lumber_initial_fuel 	= Math.round(LUMBER_MAX_FUEL * INIT_FUEL_PERCENT)
const medic_initial_fuel = Math.round(MEDIC_MAX_FUEL); // based on entity health
const miner_initial_fuel = Math.round(MINER_MAX_FUEL * INIT_FUEL_PERCENT);
const teleport_initial_fuel = Math.round(TELEPORT_MAX_FUEL * INIT_FUEL_PERCENT);
const sprinkler_initial_fuel = Math.round(SPRINKLER_MAX_FUEL * INIT_FUEL_PERCENT);
const torch_initial_fuel = Math.round(TORCH_MAX_FUEL * INIT_FUEL_PERCENT);
const drill_fuel_item = DRILL_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
const guard_fuel_item = GUARD_FUEL_ITEM.replace("minecraft:", "").replace("_", " ").replace("arrow", "arrows"); // makes the word arrow plural
const hover_fuel_item = HOVER_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
//const lumber_fuel_item 	= LUMBER_FUEL_ITEM.replace("minecraft:", "").replace("_", " ")
const medic_fuel_item = MEDIC_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
const miner_fuel_item = MINER_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
const teleport_fuel_item = TELEPORT_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
const sprinkler_fuel_item = SPRINKLER_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
const torch_fuel_item = TORCH_FUEL_ITEM.replace("minecraft:", "").replace("_", " ");
// #endregion
const ITEMS = {
    "ldz_240927:drill_bot_spawn_egg": [loreLine + drill_initial_fuel + "%", lineColor + drill_initial_fuel + " " + drill_fuel_item + " stored"],
    "ldz_240927:guard_bot_spawn_egg": [loreLine + guard_initial_fuel + "%", lineColor + guard_initial_fuel + " " + guard_fuel_item + " stored"],
    "ldz_240927:hover_bot_spawn_egg": [loreLine + hover_initial_fuel + "%", lineColor + hover_initial_fuel + " " + hover_fuel_item + " stored"],
    //"ldz_240927:lumber_bot_spawn_egg"		: [ loreLine + lumber_initial_fuel 		+ "%", 		lineColor + lumber_initial_fuel 	+ " " + lumber_fuel_item 	+ " stored"],
    "ldz_240927:medic_bot_spawn_egg": [loreLine + medic_initial_fuel + "%", lineColor + medic_initial_fuel + " " + medic_fuel_item + " stored"],
    "ldz_240927:miner_bot_dummy_spawn_egg": [loreLine + miner_initial_fuel + "%", lineColor + miner_initial_fuel + " " + miner_fuel_item + " stored"],
    "ldz_240927:portal_bot_spawn_egg": [loreLine + teleport_initial_fuel + "%", lineColor + teleport_initial_fuel + " " + teleport_fuel_item + " stored"],
    "ldz_240927:sprinkler_bot_spawn_egg": [loreLine + sprinkler_initial_fuel + "%", lineColor + sprinkler_initial_fuel + " " + sprinkler_fuel_item + " stored"],
    "ldz_240927:torch_bot_spawn_egg": [loreLine + torch_initial_fuel + "%", lineColor + torch_initial_fuel + " " + torch_fuel_item + " stored"],
    "ldz_240927:lumber_bot_spawn_egg": [botThatDoesntUseFuel],
    "ldz_240927:fan_bot_spawn_egg": [botThatDoesntUseFuel],
    "ldz_240927:angler_bot_spawn_egg": [botThatDoesntUseFuel],
    "ldz_240927:scuffle_bot_spawn_egg": [botThatDoesntUseFuel],
    "ldz_240927:time_bot_spawn_egg": [botThatDoesntUseFuel],
    "ldz_240927:vacuum_bot_spawn_egg": [botThatDoesntUseFuel],
    "ldz_240927:weather_bot_spawn_egg": [botThatDoesntUseFuel],
};
function startLoreLoop() {
    mc.system.runInterval(() => mc.system.runJob(loreLoop()), REPLACE_INTERVAL);
}
function* loreLoop() {
    const players = mc.world.getAllPlayers().map(player => new utils.SafetyCheckedPlayer(player));
    for (const player of players) {
        const inventory = player.Inventory;
        for (let slot = 0; slot < inventory.size; slot++) {
            const item = inventory.getItem(slot);
            if (item && item.getLore().length === 0) {
                const lore = ITEMS[item.typeId];
                if (lore) {
                    utils.mclog(`Setting lore: <${player.nameTag}> ${item.typeId} => ${lore.join(", ")}`, LOG_TAG);
                    item.setLore(lore);
                    inventory.setItem(slot, item);
                }
            }
        }
        yield;
    }
}
mc.world.afterEvents.worldInitialize.subscribe(startLoreLoop);
