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



export function initCat() {

    
    const chance = 1 / 240; 
    const forebodingCheck = fo.scoreChapter4; 
    const checkInterval = 1200; 

    const cat_NAMES = [
        
        "Whiskers", "Mittens", "Shadow", "Smokey",

        
        "Mimi", "Pelusa", "Batustina", "Porfirio",

        
        "Minou", "Choupette", "Moustache", "Bijou",

        
        "Pamuk", "Tekir", "Limon", "Maviş",

        
        "Мурка", "Васька", "Сима", "Барсик",

        
        "小猫", "咪咪", "雪白", "花花",

        
        "たま", "くろ", "はな", "ちび",

        
        "Minoes", "Pluis", "Sam", "Tijger",

        
        "قط", "ليلى", "سلطان", "نورة",

        
        "Misse", "Mirri", "Kisu", "Pörrö",

        
        "باستت", "سمبا", "فرح", "مشمش",

        "Mongster", "Phurpa", "Hermit",
    ];

    let catSpawned = false;
    let playerTriggered = false; 
    let eventFlag = false

    
    function attemptSpawncat() {
        const kitties = fo.overworld.getEntities({ type: "minecraft:cat" });
        const existingcat = kitties.some(cat => cat_NAMES.includes(cat.nameTag));
        if (catSpawned || existingcat) return; 

        if (fo.forebodingScore >= forebodingCheck && Math.random() < (chance * fo.netProbabilityModifier) && eventFlag != true) { 
            eventFlag = true
            
        }

        if (eventFlag === true) { 
            const players = fo.overworld.getPlayers();
            if (players.length === 0) return;

            const player = players[Math.floor(Math.random() * players.length)]; 
            const nearbyCows = fo.overworld.getEntities({
                type: "minecraft:cow",
                location: player.location,
                minDistance: 32,
                maxDistance: 120,
            });

            if (nearbyCows.length > 0) {
                const cow = nearbyCows[Math.floor(Math.random() * nearbyCows.length)];
                
                const randomName = cat_NAMES[Math.floor(Math.random() * cat_NAMES.length)];

                fo.overworld.runCommand(`summon cat ${cow.location.x} ${cow.location.y} ${cow.location.z} ~ ~ minecraft:on_tame ${randomName}`);
                system.runTimeout(() => { fo.overworld.runCommand(`execute as @e [type=cat, name=${randomName}, c=1] run spreadplayers ~ ~ 1 10 @s ~3`) }, 1)

                catSpawned = true;
                playerTriggered = false; 
                
            }
        }
    }

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

    
    function trackcatProximity() {
        const kitty = fo.overworld.getEntities({ type: "minecraft:cat" });
        const namedKitties = kitty.find(cat => cat_NAMES.includes(cat.nameTag));

        if (!namedKitties) return;

        const players = fo.overworld.getPlayers({
            location: namedKitties.location,
            maxDistance: 60,
        });

        let anyPlayerNearby = false;

        for (const player of players) {
            const distance = getDistance(player.location, namedKitties.location);

            if (!playerTriggered && distance <= 5) {
                playerTriggered = true; 
                
            }

            if (distance <= 60) {
                anyPlayerNearby = true;
            }
        }

        
        if (playerTriggered && !anyPlayerNearby) {
            namedKitties.remove();
            playerTriggered = false;
            
        }
    }

    
    system.runInterval(attemptSpawncat, checkInterval);
    
    system.runInterval(trackcatProximity, 20);

}