import * as mc from '@minecraft/server';
import * as config from '../pb_scraft_config';
import * as altarconfig from '../pb_scraft_altar_config';
import { randomWholeNum } from './lib';

export const GLYPHS = [
    {
        identifier: "pb_scraft:glyph_accelerate",
        name: "Accelerate",
        symbol: "§u[Ac+]§r",
        mana: 0.3   //30% shorter cast time
    },
    {
        identifier: "pb_scraft:glyph_alleviate",
        name: "Alleviate",
        symbol: "§u[Al+]§r",
        castTime: 0.2   //20% mana cost decrease
    },
    {
        identifier: "pb_scraft:glyph_intensify",
        name: "Intensify",
        symbol: "§u[I+]§r",
        spellPower: 10, //10 spellpower increase
    },
    {
        identifier: "pb_scraft:glyph_mirror",
        name: "Mirror",
        symbol: "§u[M]§r"   //change spell into something else
    }
]

export function applySpell(item, itemdata, spelldata, g1, g2) {
    item.nameTag = itemdata.name.replace("SPELLINFO", spelldata.display_name + "§f [§d" + spelldata.manacost + "§f]");

    //const glyph_1 = GLYPHS[randomWholeNum(0,3)];
    //const glyph_2 = GLYPHS[randomWholeNum(0,3)];

    const glyph_1 = GLYPHS.find((f) => f.identifier == g1);
    const glyph_2 = GLYPHS.find((f) => f.identifier == g2);

    item.setLore(createSpellLore(spelldata, glyph_1, glyph_2));

    item.setDynamicProperty("pb_scraft_spell", spelldata.identifier);

    if (glyph_1 != undefined) item.setDynamicProperty("pb_scraft_glyph_1", glyph_1.identifier);
    if (glyph_2 != undefined) item.setDynamicProperty("pb_scraft_glyph_2", glyph_2.identifier);

    return item;
}

export function assembleSpellOrb(item, spelldata, g1, g2) {
    item.nameTag = "Orb " + spelldata.display_name;

    //const glyph_1 = GLYPHS[randomWholeNum(0,3)];
    //const glyph_2 = GLYPHS[randomWholeNum(0,3)];

    const glyph_1 = GLYPHS.find((f) => f.identifier == g1);
    const glyph_2 = GLYPHS.find((f) => f.identifier == g2);

    item.setLore(createSpellLore(spelldata, glyph_1, glyph_2));

    item.setDynamicProperty("pb_scraft_spell", spelldata.identifier);

    if (glyph_1 != undefined) item.setDynamicProperty("pb_scraft_glyph_1", glyph_1.identifier);
    if (glyph_2 != undefined) item.setDynamicProperty("pb_scraft_glyph_2", glyph_2.identifier);

    return item;
}

export function createSpellLore(data, glyph_1, glyph_2) {
    let glyphstring = "Glyphs: §r"

    if (glyph_1 == undefined) glyphstring = glyphstring + "§7[  ]";
    if (glyph_1 != undefined) glyphstring = glyphstring + glyph_1.symbol;

    if (glyph_2 == undefined) glyphstring = glyphstring + "§7[  ]";
    if (glyph_2 != undefined) glyphstring = glyphstring + glyph_2.symbol;

    //mc.world.sendMessage("§f" + glyphstring);

    return [
        " ",
        "§f" + data.display_name,
        "§f" + glyphstring,
        "§fMana: §d" + data.manacost,
        "§fCast Time: §b" + data.castTime,
    ]
}


export function checkSpellApply(heldItem, pedestalEntities) {
    const itemdata = config.CAST_ITEM_LIST.find((f) => f.identifier == heldItem.typeId);

    if (itemdata == undefined) return  //item is no staff
    if (heldItem.getDynamicProperty("pb_scraft_spell") != undefined) return    //staff has no free spellslot
    if (pedestalEntities.length != 1) return;    //more than one valid spell orb present
    if (pedestalEntities[0].getDynamicProperty("pb_scraft_spell") == undefined) return    //first pedestal entity is not a spell orb
    
    let glyph_1 = pedestalEntities[0].getDynamicProperty("pb_scraft_glyph_1");
    let glyph_2 = pedestalEntities[0].getDynamicProperty("pb_scraft_glyph_2");

    const spelldata = config.SPELL_LIST.find((f) => f.identifier == pedestalEntities[0].getDynamicProperty("pb_scraft_spell"));

    return applySpell(heldItem, itemdata, spelldata, glyph_1, glyph_2);
}

export function checkSpellRemove(heldItem, pedestalEntities) {
    const itemdata = config.CAST_ITEM_LIST.find((f) => f.identifier == heldItem.typeId);

    if (itemdata == undefined) return  //item is no staff
    if (heldItem.getDynamicProperty("pb_scraft_spell") == undefined) return    //staff has spell applied
    if (pedestalEntities.length != 1) return;    //more than item present
    if (pedestalEntities[0].getDynamicProperty("pb_scraft_id") != "minecraft:amethyst_shard") return    //first pedestal entity is not empty glyph
    //todo - give spell orb back maybe
    return new mc.ItemStack(heldItem.typeId, 1);
}

export function checkOrbModify(heldItem, pedestalEntities) {
    if (!heldItem.hasTag("pb_scraft:spellorb")) return;  //item is not a spell orb
    if (heldItem.getDynamicProperty("pb_scraft_spell") == undefined) return    //orb has no spell applied
    if (pedestalEntities.length != 1) return;    //more than one rune present
    if (pedestalEntities[0].getDynamicProperty("pb_scraft_id") == "pb_scraft:glyph_mirror") return; //mirror transformation case 

    //check if pedestal items are modification glyphs
    for (let index = 0; index < pedestalEntities.length; index++) {
        const entity = pedestalEntities[index];
        const inventory = entity.getComponent('minecraft:inventory').container;
        if (!inventory.getItem(0).hasTag("pb_scraft:mod_glyph")) return;
    }

    let glyph_1 = heldItem.getDynamicProperty("pb_scraft_glyph_1");
    let glyph_2 = heldItem.getDynamicProperty("pb_scraft_glyph_2");

    if (glyph_1 != undefined && glyph_2 != undefined) return //glyph slots filled
    if (glyph_1 == undefined) glyph_1 = pedestalEntities[0].getDynamicProperty("pb_scraft_id");
    else glyph_2 = pedestalEntities[0].getDynamicProperty("pb_scraft_id");

    const spelldata = config.SPELL_LIST.find((f) => f.identifier == heldItem.getDynamicProperty("pb_scraft_spell"));

    return assembleSpellOrb(heldItem, spelldata, glyph_1, glyph_2);
}

export function checkOrbMirror(heldItem, pedestalEntities, altarRecipe) {
    if (!heldItem.hasTag("pb_scraft:spellorb")) return;  //item is not a spell orb
    if (heldItem.getDynamicProperty("pb_scraft_spell") == undefined) return    //orb has no spell applied
    if (pedestalEntities.length != 1) return;    //more than one rune present

    const orbspelldata = config.SPELL_LIST.find((f) => f.identifier == heldItem.getDynamicProperty("pb_scraft_spell"));

    if (altarRecipe.spell_base != orbspelldata.identifier) return //spell data on orb does match recipe

    let glyph_1 = heldItem.getDynamicProperty("pb_scraft_glyph_1");
    let glyph_2 = heldItem.getDynamicProperty("pb_scraft_glyph_2");

    if (glyph_1 != undefined && glyph_2 != undefined) return //glyph slots filled

    if (glyph_1 == undefined) glyph_1 = "pb_scraft:glyph_mirror";
    else glyph_2 = "pb_scraft:glyph_mirror";

    const mirrorspelldata = config.SPELL_LIST.find((f) => f.identifier == altarRecipe.spell_result);

    return assembleSpellOrb(heldItem, mirrorspelldata, glyph_1, glyph_2);
}