import { world, system, EntityComponentTypes } from "@minecraft/server";
import { fo } from "./utilities/fo_constants";


export function initFamine() { 

    
    const foodDrops = [
        'minecraft:beef',
        'minecraft:cooked_beef',
        "minecraft:mutton",
        "minecraft:cooked_mutton",
        "minecraft:chicken",
        "minecraft:cooked_chicken",
        "minecraft:porkchop",
        "minecraft:cooked_porkchop",
        "minecraft:rabbit",
        "minecraft:cooked_rabbit",
        "minecraft:cod",
        "minecraft:cooked_cod",
        "minecraft:salmon",
        "minecraft:cooked_salmon",
        "minecraft:fishing_hook",
    ];

    const fishingDrops = [
        "minecraft:cod",
        "minecraft:salmon",
    ];

    
    const animals = [
        "minecraft:cow",
        "minecraft:mooshroom",
        "minecraft:sheep",
        "minecraft:chicken",
        "minecraft:pig",
        "minecraft:hoglin",
        "minecraft:rabbit",
        "minecraft:cod",
        "minecraft:salmon",
    ];

    let meatDropChance = 1; 

    const
        ForebodingCheck1 = fo.scoreChapter3,
        ForebodingCheck2 = fo.scoreChapter4;

    
    function getDistance(pos1, pos2) {
        return Math.sqrt(
            Math.pow(pos1.x - pos2.x, 2) +
            Math.pow(pos1.y - pos2.y, 2) +
            Math.pow(pos1.z - pos2.z, 2)
        );
    };

    
    world.afterEvents.entityDie.subscribe((event) => {
        const entity = event.deadEntity;

        const forebodingScore = fo.forebodingScore;

        
        if (forebodingScore < ForebodingCheck1) return; 

        if (forebodingScore >= ForebodingCheck1 && forebodingScore < ForebodingCheck2) {
            meatDropChance = 0.35
        } else if (forebodingScore >= ForebodingCheck2) {
            meatDropChance = 0.05
        };

        
        if (animals.includes(entity.typeId)) {
            
            const items = entity.dimension.getEntities({
                location: entity.location,
                maxDistance: 1, 
            });

            for (const item of items) {
                const itemComp = item.getComponent(EntityComponentTypes.Item);
                if (itemComp) {
                    
                    if (foodDrops.includes(itemComp.itemStack.typeId)) {
                        let distance = getDistance(item.location, entity.location);
                        if (distance <= 0.65 && Math.random() > meatDropChance) {
                            
                            item.remove();
                            
                            
                        }
                    }
                }
            }
        }
    });


    world.beforeEvents.entityRemove.subscribe((event) => {
        const entity = event.removedEntity;
        
        if (entity.typeId === "minecraft:fishing_hook") {

            const forebodingScore = fo.forebodingScore;

            
            if (forebodingScore < ForebodingCheck1) return; 

            if (forebodingScore >= ForebodingCheck1 && forebodingScore < ForebodingCheck2) {
                meatDropChance = 0.35
            } else if (forebodingScore >= ForebodingCheck2) {
                meatDropChance = 0.05
            };

            const
                dim = entity.dimension,
                loc = entity.location;

            
            system.runTimeout(() => {
                const items = dim.getEntities({
                    location: loc,
                    maxDistance: 8, 
                });

                for (const item of items) {
                    const itemComp = item.getComponent(EntityComponentTypes.Item);
                    if (itemComp) {
                        
                        if (fishingDrops.includes(itemComp.itemStack.typeId)) {
                            
                            let distance = getDistance(item.location, loc);
                            if (distance <= 8 && Math.random() > meatDropChance) {
                                
                                item.remove();
                                
                            }
                        }
                    }
                }
            }, 1)

        }

    });

}

