import { world, system } from "@minecraft/server";
import * as utils from "./utils";
import * as Vector3 from "./vector3";

const SHOOT_SPEED = 0.6;

export async function onScriptEventReceive(event) {
    const sourceEntity = event.sourceEntity;

    if (event.message === "warden_king") {
        wardenKingProjectiles(sourceEntity);
    } else if (event.message === "follow_player") {
        followPlayer(sourceEntity);
    } else if (event.message === "follow_target") {
        // followTarget(sourceEntity);
    }
}

function wardenKingProjectiles(entity) {
    // Summon projectiles
    const targetLocation = entity.location;
    targetLocation.y += 3;

    let projectile = entity.dimension.spawnEntity("goe_bos:wither_projectile<goe_bos:follow_player>", targetLocation);
    let projectileComponent = projectile.getComponent("projectile");
    projectileComponent.owner = entity;
    projectileComponent.shoot({ x: 0, y: 0.1, z: 0 });

    targetLocation.x += 1;
    projectile = entity.dimension.spawnEntity("goe_bos:wither_projectile<goe_bos:follow_player>", targetLocation);
    projectileComponent = projectile.getComponent("projectile");
    projectileComponent.owner = entity;
    projectileComponent.shoot({ x: 0.3, y: 0, z: 0 });

    targetLocation.x -= 2;
    projectile = entity.dimension.spawnEntity("goe_bos:wither_projectile<goe_bos:follow_player>", targetLocation);
    projectileComponent = projectile.getComponent("projectile");
    projectileComponent.owner = entity;
    projectileComponent.shoot({ x: -0.3, y: 0, z: 0 });

    targetLocation.x += 1;
    targetLocation.z += 1;
    projectile = entity.dimension.spawnEntity("goe_bos:wither_projectile", targetLocation);
    projectile.triggerEvent("goe_bos:follow_player");
    projectileComponent = projectile.getComponent("projectile");
    projectileComponent.owner = entity;
    projectileComponent.shoot({ x: 0, y: 0, z: 0.3 });

    targetLocation.z -= 2;
    projectile = entity.dimension.spawnEntity("goe_bos:wither_projectile", targetLocation);
    projectile.triggerEvent("goe_bos:follow_player");
    projectileComponent = projectile.getComponent("projectile");
    projectileComponent.owner = entity;
    projectileComponent.shoot({ x: 0, y: 0, z: -0.3 });
}

export async function shootProjectile(entity, type, location, direction, speed) {
    const projectile = entity.dimension.spawnEntity(`goe_bos:wither_projectile<goe_bos:${type}>`, location);
    projectile.setDynamicProperty("goe_bos_owner_id", entity.id);
    let projectileComponent = projectile.getComponent("projectile");
    projectileComponent.owner = entity;
    projectileComponent.shoot({ 
        x: direction.x * speed, 
        y: direction.y * speed, 
        z: direction.z * speed 
    });
}

function followPlayer(projectile) {
    try {
        // Find the closest player
        const target = projectile.dimension.getEntities({
            location: projectile.location,
            type: "minecraft:player",
            closest: 1
        })[0];
    
        const targetLocation = target.location;
        targetLocation.y += 1;
    
        const direction = Vector3.getDirection(projectile.location, targetLocation);
        direction.x *= SHOOT_SPEED;
        direction.y *= SHOOT_SPEED;
        direction.z *= SHOOT_SPEED;
    
        let projectileComponent = projectile.getComponent("projectile");
        projectileComponent.shoot(direction);
    } catch (error) {
        
    }
}

async function followTarget(projectile) {
    try {
        // Find the closest target
        const targets = projectile.dimension.getEntities({
            location: projectile.location,
            excludeTypes: [ "goe_bos:wither_projectile", "minecraft:item" ],
            excludeFamilies: [ "utility", "inanimate" ],
            maxDistance: 20,
            closest: 2
        });
        if (targets.length === 0) return;

        const ownerId = projectile.getDynamicProperty("goe_bos_owner_id");
        const target = targets[0].id === ownerId ? targets[1] : targets[0];
        if (target === undefined) return;
    
        const targetLocation = target.location;
        targetLocation.y += 1;
    
        const direction = Vector3.getDirection(projectile.location, targetLocation);
        direction.x *= SHOOT_SPEED;
        direction.y *= SHOOT_SPEED;
        direction.z *= SHOOT_SPEED;
    
        let projectileComponent = projectile.getComponent("projectile");
        projectileComponent.shoot(direction);
    } catch (error) {
        // utils.debug(error.message);
    }
}