import { ItemDurabilityComponent } from '@minecraft/server';
import * as mc from "@minecraft/server"

export function highlightBlock(block) {

    let location = block.location;
    let dimension = block.dimension.id;

    let mvm = new mc.MolangVariableMap();
    mvm.setFloat(`variable.dirx`, 0);
    mvm.setFloat(`variable.diry`, 1);
    mvm.setFloat(`variable.dirz`, 0);
    mc.world.getDimension(dimension).spawnParticle('sgs_farm:block_marker', { x: location.x + 0.5, y: location.y - 0.01, z: location.z + 0.5 }, mvm);
    mc.world.getDimension(dimension).spawnParticle('sgs_farm:block_marker', { x: location.x + 0.5, y: location.y + 1.01, z: location.z + 0.5 }, mvm);

    mvm.setFloat(`variable.dirx`, 1);
    mvm.setFloat(`variable.diry`, 0);
    mvm.setFloat(`variable.dirz`, 0);
    mc.world.getDimension(dimension).spawnParticle('sgs_farm:block_marker', { x: location.x + 1.01, y: location.y + 0.5, z: location.z + 0.5 }, mvm);
    mc.world.getDimension(dimension).spawnParticle('sgs_farm:block_marker', { x: location.x - 0.01, y: location.y + 0.5, z: location.z + 0.5 }, mvm);

    mvm.setFloat(`variable.dirx`, 0);
    mvm.setFloat(`variable.diry`, 0);
    mvm.setFloat(`variable.dirz`, 1);
    mc.world.getDimension(dimension).spawnParticle('sgs_farm:block_marker', { x: location.x + 0.5, y: location.y + 0.5, z: location.z + 1.01 }, mvm);
    mc.world.getDimension(dimension).spawnParticle('sgs_farm:block_marker', { x: location.x + 0.5, y: location.y + 0.5, z: location.z + 0.01 }, mvm);
}

export function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

export function randomWholeNum(min, max) {
    const maxPlus1 = max + 1;
    let result = Math.random() * (max - min) + min;
    //return a random whole number
    return Math.round(result);
}

export function randomNum(min, max) {
    //return a random number
    return Math.random() * (max - min) + min;
}

export class itemManager {
    static reduceDurability(player, item, reduceAmount) {
        const comp = item.getComponent(ItemDurabilityComponent.componentId);
        if (comp.damage + reduceAmount <= comp.maxDurability) {
            comp.damage = comp.damage + reduceAmount;
            return item;
        } else {
            player.dimension.playSound("random.break", player.location);
            return undefined;
        }
    }
    static addDurability(player, item, addAmount) {
        const comp = item.getComponent(ItemDurabilityComponent.componentId);
        if (comp.damage - addAmount < 0) {
            comp.damage = 0;
            return item;
        } else {
            comp.damage = comp.damage - comp.addAmount;
            return item;
        }
    }
    static reduceAmount(item, reduceBy) {
        const amount = item.amount;
        if (amount - reduceBy <= 0) {
            return undefined;
        } else {
            item.amount = amount - reduceBy;
            return item;
        }
    }
}