import * as mc from "@minecraft/server";
import { highlightBlock, itemManager, randomWholeNum } from "../sgs_farm_lib";

const FISH_SUCCES_TRESHOLD = 0.20;

export const fishTrapLoot = [
    {
        id: "minecraft:cod",
        weight: 70
    },
    {
        id: "minecraft:salmon",
        weight: 25
    },
    {
        id: "minecraft:tropical_fish",
        weight: 5
    },
    {
        id: "minecraft:pufferfish",
        weight: 8
    },
    //JUNK
    {
        id: "minecraft:stick",
        weight: 8
    },
    {
        id: "minecraft:bowl",
        weight: 5
    },
    {
        id: "minecraft:string",
        weight: 8
    },
    {
        id: "minecraft:rotten_flesh",
        weight: 5
    },
    //TREASURE
    {
        id: "minecraft:nautilus_shell",
        weight: 2
    },
    {
        id: "minecraft:name_tag",
        weight: 2
    },
    //SEAFOOD
    {
        id: "sgs_farm:crab",
        weight: 15
    },
    {
        id: "sgs_farm:lobster",
        weight: 10
    },
    {
        id: "sgs_farm:shrimp",
        weight: 20
    },
    {
        id: "sgs_farm:clam",
        weight: 12
    },
]

var weighteLoot = [];

buildWeighteLoot();

function buildWeighteLoot() {
    for (let index = 0; index < fishTrapLoot.length; index++) {
        const element = fishTrapLoot[index];

        for (let index = 0; index < element.weight; index++) {
            weighteLoot.push(element.id);
        }

    };
}

export class fishTrapManager {
    static beforeplace(data) {
        const block = data.block;

        mc.system.runTimeout(() => {
            const entity = mc.world.getDimension(block.dimension.id).spawnEntity("sgs_farm:fish_trap_inventory", block.bottomCenter());
            entity.nameTag = "Fish Trap";

            if (block.hasTag("water")) {
                mc.system.runTimeout(() => {
                    block.setWaterlogged(true);
                }, 1)
            }

            this.highLightWater(block);
        }, 0)
    }

    static destroy(data) {

        const block = data.block;
        const location = block.bottomCenter();

        const entity = mc.world.getDimension(block.dimension.id).getEntities({ location, families: ["sgs_farm_fish_trap"], maxDistance: 0.2 })[0];
        if (entity == undefined) return;
        entity.triggerEvent("sgs_farm:transform");
    }

    static tick(block) {

        if (this.canFish(block) == false) return;
        const location = block.bottomCenter();
        const entity = mc.world.getDimension(block.dimension.id).getEntities({ location, families: ["sgs_farm_fish_trap"], maxDistance: 0.2 })[0];

        if (entity == undefined) {
            block.dimension.runCommandAsync(`setblock ${block.x} ${block.y} ${block.z} air [] destroy`);
            return;
        }

        const inventory = entity?.getComponent('minecraft:inventory').container;
        const item = inventory.getItem(0);
        if (item == undefined) return;

        if (item.typeId != "sgs_farm:bait") return; //no bait no deal

        const dimension = block.dimension;
        mc.world.getDimension(dimension.id).spawnParticle("sgs_farm:trap_water_ripple_s", block.bottomCenter());

        if (Math.random() > FISH_SUCCES_TRESHOLD) {
            block.dimension.playSound("bucket.fill_fish", block.center());
            mc.world.getDimension(dimension.id).spawnParticle("sgs_farm:water_splash_trap", block.center());

            const num = randomWholeNum(0, weighteLoot.length);
            inventory.addItem(new mc.ItemStack(weighteLoot[num], 1))
        }

        inventory.setItem(0, itemManager.reduceDurabilityEntity(entity, item, 10));
    }

    static highLightWater(block) {
        highlightBlock(block.north().west());
        highlightBlock(block.north().east());
        highlightBlock(block.north());

        highlightBlock(block.south().west());
        highlightBlock(block.south().east());
        highlightBlock(block.south());

        highlightBlock(block.east());
        highlightBlock(block.west());
    }

    static canFish(block) {
        if (!block.isWaterlogged) return false;
        if (!block.north().west().hasTag("water")) return false;
        if (!block.north().hasTag("water")) return false;
        if (!block.north().east().hasTag("water")) return false;
        if (!block.east().hasTag("water")) return false;
        if (!block.west().hasTag("water")) return false;
        if (!block.south().west().hasTag("water")) return false;
        if (!block.south().hasTag("water")) return false;
        if (!block.south().east().hasTag("water")) return false;

        return true;
    }
}