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


export function initBoat() {

    const DAMAGE_INTERVAL = 20; 
    const MAX_HITS = 36; 
    const BOAT_DAMAGE = 1.3; 

    const chance = 1 / 24; 
    const forebodingCheck = fo.scoreChapter4; 
    const checkInterval = 5987; 

    let trackedBoats = new Map(); 
    let eventFlag = false; 
    let activeBoatId = null; 


    
    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)
        );
    };

    function ValidBoad() { 
        if (activeBoatId === null) {
            const boats = fo.overworld.getEntities({ type: "minecraft:boat" });

            
            const validBoats = boats.filter(boat => {
                return world.getPlayers().some(player => {
                    const distance = getDistance(boat.location, player.location);
                    return distance >= 10 && distance <= 32;
                });
            });

            
            if (validBoats.length > 0) {
                const randomBoat = validBoats[Math.floor(Math.random() * validBoats.length)];
                activeBoatId = randomBoat.id; 
                
            }
        }
    };

    function IsValidBoatAlive() {
        if (activeBoatId !== null) {
            
            const boats = fo.overworld.getEntities({ type: "minecraft:boat" });
            const boatExists = boats.some(boat => boat.id === activeBoatId);

            if (!boatExists) {
                
                
                eventFlag = false;
                activeBoatId = null; 
            }
        }
    }

    system.runInterval(() => {
        if (eventFlag) {
            ValidBoad()
            IsValidBoatAlive()

            for (const player of world.getPlayers()) {
                const boats = fo.overworld.getEntities({ type: "minecraft:boat" });

                for (const boat of boats) {
                    
                    if (boat.id !== activeBoatId) continue;

                    const distance = getDistance(boat.location, player.location);

                    if (distance >= 10 && distance <= 32) {
                        if (!trackedBoats.has(boat.id)) {
                            trackedBoats.set(boat.id, { hits: 0, lastHit: 0 });
                        }

                        let boatData = trackedBoats.get(boat.id);

                        if (system.currentTick - boatData.lastHit >= DAMAGE_INTERVAL) {
                            boatData.hits++;
                            boatData.lastHit = system.currentTick;
                            boat.applyDamage(BOAT_DAMAGE, { cause: EntityDamageCause.void });

                            if (boatData.hits >= MAX_HITS) {
                                boat.kill();
                                trackedBoats.delete(boat.id);
                                eventFlag = false;
                                activeBoatId = null; 
                                
                            } else {
                                trackedBoats.set(boat.id, boatData);
                            }
                        }
                    } else if (distance < 6 && trackedBoats.has(boat.id)) {
                        
                        trackedBoats.delete(boat.id);
                        eventFlag = false;
                        activeBoatId = null; 
                        
                    } else if (distance > 32 && trackedBoats.has(boat.id)) {
                        
                        trackedBoats.delete(boat.id);
                        eventFlag = false;
                        activeBoatId = null; 
                        
                    }
                }
            }
        }
    }, 20); 

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

}