import * as mc from "@minecraft/server";
import { dinoList, syringeEmpty, syringeFull, syringeHalf, syringeReady, unmorphSyringe } from "../constants";
import { getEntityName, getSyringeDNAInfo, isDinosaur, isPlayer, setSyringeDNAInfo, takeOneItem } from "../utilities";
import { NAMESPACE } from "../anvilConstants";
import { startCentrifuge } from "../machines/centrifuge";
import { endMorphing, startMorphing } from "../morphManager/morphManager";
const syringes = [syringeEmpty, syringeHalf, syringeFull, syringeReady, unmorphSyringe];
function raycastGetDinoDNA(player) {
    const dino = player.getEntitiesFromViewDirection({ maxDistance: 4 })[0];
    if (dino && isDinosaur(dino.entity)) {
        return dino.entity.typeId;
    }
    else {
        player.runCommand(`titleraw @s actionbar ${JSON.stringify({ rawtext: [{ translate: `${NAMESPACE}.syringe_empty` }] })}`);
        return undefined;
    }
}
function syringeFillDinosaurDNA(player, ItemStack) {
    const dinoType = raycastGetDinoDNA(player);
    if (dinoType) {
        const playerInventory = player.getComponent("minecraft:inventory");
        takeOneItem(player, ItemStack);
        const dinoName = getEntityName(dinoType);
        const dinoSyringe = new mc.ItemStack(syringeHalf, 1);
        setSyringeDNAInfo(dinoSyringe, false, dinoName);
        playerInventory.container?.addItem(dinoSyringe);
        let message = JSON.stringify({ rawtext: [{ translate: `${NAMESPACE}.syringe_half` }] });
        player.runCommand(`titleraw @s actionbar ${message}`);
    }
}
function syringeFillPlayerDNA(player, ItemStack) {
    const playerEquippable = player.getComponent("minecraft:equippable");
    takeOneItem(player, ItemStack);
    const playerSyringe = new mc.ItemStack(syringeFull, 1);
    setSyringeDNAInfo(playerSyringe, false, ItemStack.getDynamicProperty(`${NAMESPACE}:dinosaur`), player.name);
    playerEquippable.setEquipment(mc.EquipmentSlot.Mainhand, playerSyringe);
    player.runCommand(`titleraw @s actionbar ${JSON.stringify({ rawtext: [{ translate: `${NAMESPACE}.syringe_full` }] })}`);
}
function syringePlaceInCentrifuge(player, ItemStack) {
    startCentrifuge(player, ItemStack);
}
function syringeReadyToUse(player, ItemStack) {
    if (!player.getDynamicProperty(`${NAMESPACE}:morph_name`)) {
        const playerEquippable = player.getComponent("minecraft:equippable");
        takeOneItem(player, ItemStack);
        const emptySyringe = new mc.ItemStack(syringeEmpty, 1);
        playerEquippable.setEquipment(mc.EquipmentSlot.Mainhand, emptySyringe);
        startMorphing(player, ItemStack.getDynamicProperty(`${NAMESPACE}:dinosaur`));
    }
    else {
        endMorphing(player);
    }
}
function unmorphSyringeUse(player) {
    if (player.getDynamicProperty(`${NAMESPACE}:morph_name`)) {
        endMorphing(player);
    }
}
export function initSyringe(player) {
    if (player.isValid()) {
        const playerEquippable = player.getComponent("minecraft:equippable");
        const itemStack = playerEquippable.getEquipment(mc.EquipmentSlot.Mainhand);
        if (itemStack && player.typeId === "minecraft:player" && itemStack.typeId === syringeEmpty) {
            setSyringeDNAInfo(itemStack, false);
            playerEquippable.setEquipment(mc.EquipmentSlot.Mainhand, itemStack);
        }
    }
}
export function syringeManager(eventData) {
    const ItemStack = eventData.itemStack;
    const player = eventData.source;
    if (syringes.includes(ItemStack.typeId) && isPlayer(player)) {
        player.playSound(`${NAMESPACE}:weapons.syringe`);
        if (ItemStack.typeId !== syringeEmpty && ItemStack.typeId !== unmorphSyringe) {
            const itemInfo = getSyringeDNAInfo(ItemStack);
            if (ItemStack.getDynamicProperty(`${NAMESPACE}:dinosaur`) === undefined &&
                ItemStack.getDynamicProperty(`${NAMESPACE}:player`) === undefined &&
                ItemStack.getDynamicProperty(`${NAMESPACE}:ready`) === undefined) {
                setSyringeDNAInfo(ItemStack, itemInfo.status, itemInfo.dinoName, itemInfo.playerName);
            }
        }
        switch (ItemStack.typeId) {
            case syringeEmpty:
                syringeFillDinosaurDNA(player, ItemStack);
                break;
            case syringeHalf:
                syringeFillPlayerDNA(player, ItemStack);
                break;
            case syringeFull:
                syringePlaceInCentrifuge(player, ItemStack);
                break;
            case syringeReady:
                syringeReadyToUse(player, ItemStack);
                break;
            case unmorphSyringe:
                unmorphSyringeUse(player, ItemStack);
                break;
            default:
                return;
        }
    }
}
export function spawnFullSyringes(eventData) {
    const player = eventData.sourceEntity;
    if (eventData.id === `${NAMESPACE}:spawn_syringes`) {
        if (player) {
            for (const value of Object.keys(dinoList)) {
                const syringe = new mc.ItemStack(syringeReady, 1);
                setSyringeDNAInfo(syringe, true, value, player.name);
                player.dimension.spawnItem(syringe, player.location);
            }
        }
    }
}
