import { world, system } from "@minecraft/server";
import { VEHICLES } from "./VehiclesRoot.js";
const dimensions = [
    world.getDimension("minecraft:overworld"),
    world.getDimension("minecraft:nether"),
    world.getDimension("minecraft:the_end"),
];

let isCameraSystemActive = false;
let cameraInterval = null;

function handleCameraSystem() {
    if (!isCameraSystemActive) return;

    for (const dimension of dimensions) {
        for (const [vehicleType, config] of Object.entries(VEHICLES)) {
            const vehicles = dimension.getEntities({ type: config.identifier });
            
            for (const vehicle of vehicles) {
                const engineOn = vehicle.getDynamicProperty("ttav:engine");
                if (!engineOn) {
                    const riderTag = Array.from(vehicle.getTags()).find(tag => tag.startsWith('rider_'));
                    if (riderTag) {
                        const riderId = riderTag.split('_')[1];
                        for (const dimension of dimensions) {
                            const players = dimension.getPlayers();
                            for (const player of players) {
                                if (player.id === riderId) {
                                    player.runCommand(`camera @s clear`);
                                }
                            }
                        }
                    }
                    continue;
                }

                const riderTag = Array.from(vehicle.getTags()).find(tag => tag.startsWith('rider_'));
                if (!riderTag) continue;

                const riderId = riderTag.split('_')[1];
                const mainGunActive = vehicle.getDynamicProperty("ttav:main_gun");
                const subGunActive = vehicle.getDynamicProperty("ttav:sub_gun");
                const flareGunActive = vehicle.getDynamicProperty("ttav:flare_gun");

                let cameraY;
                if (mainGunActive) {
                    cameraY = config.mainGun.cameraYvalue;
                } else if (subGunActive) {
                    cameraY = config.subGun.cameraYvalue;
                } else if (flareGunActive) {
                    cameraY = config.flareGun.cameraYvalue;
                } else {
                    cameraY = config.defaultCamera.cameraYvalue;
                }

                for (const dimension of dimensions) {
                    const players = dimension.getPlayers();
                    for (const player of players) {
                        if (player.id === riderId) {
                            player.runCommand(`camera @s set minecraft:free ease 0.12 linear pos ~~${cameraY}~ rot ~~`);
                        }
                    }
                }
            }
        }
    }
}

export function startCameraSystem() {
    isCameraSystemActive = true;
    if (!cameraInterval) {
        cameraInterval = system.runInterval(handleCameraSystem, 1);
    }
}

export function stopCameraSystem() {
    isCameraSystemActive = false;
}

world.afterEvents.entityDie.subscribe((event) => {
    const { deadEntity } = event;
    const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === deadEntity.typeId);
    if (vehicleConfig) {
        const riderTag = Array.from(deadEntity.getTags()).find(tag => tag.startsWith('rider_'));
        if (riderTag) {
            const riderId = riderTag.split('_')[1];
            for (const dimension of dimensions) {
                const players = dimension.getPlayers();
                for (const player of players) {
                    if (player.id === riderId) {
                        player.runCommand(`camera @s clear`);
                    }
                }
            }
        }
    }
});

world.afterEvents.entityRemove.subscribe((event) => {
    if (!event.removedEntity) return;
    
    const removedEntity = event.removedEntity;
    const vehicleConfig = Object.values(VEHICLES).find(v => v.identifier === removedEntity.typeId);
    if (vehicleConfig) {
        const riderTag = Array.from(removedEntity.getTags()).find(tag => tag.startsWith('rider_'));
        if (riderTag) {
            const riderId = riderTag.split('_')[1];
            for (const dimension of dimensions) {
                const players = dimension.getPlayers();
                for (const player of players) {
                    if (player.id === riderId) {
                        player.runCommand(`camera @s clear`);
                    }
                }
            }
        }
    }
});

system.runInterval(handleCameraSystem, 1);
