import { world, system, ItemStack, BlockPermutation} from '@minecraft/server';


function itemStopUseIsConsumed(item, itemTypeId, eventData)
{
        if (item.typeId == itemTypeId)
        {
            if (eventData.useDuration == 0)
            {
                return true
            }
            else
            {
                return false
            }
            
        }
        else
        {
            return false
        }
}


world.afterEvents.itemStopUse.subscribe((eventData) =>{
    let item = eventData.itemStack
    if (itemStopUseIsConsumed(item, "msls:recall_potion", eventData))
    {
        let player = eventData.source
            var spawn = player.getSpawnPoint();
            if (spawn)
            {
                player.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                player.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                player.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
                player.runCommandAsync('playsound random.magic_mirror_use')
                player.teleport({x: spawn.x, y: spawn.y, z: spawn.z}, {dimension: spawn.dimension, checkForBlocks: true});
                player.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                player.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                player.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
            }
            else
            {
                spawn = world.getDefaultSpawnLocation()
                player.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                player.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                player.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
                player.runCommandAsync('playsound random.magic_mirror_use')
                teleportToWorldSpawn(player, world)
            }
                    
            
    
    
        
    }
}
)








  world.afterEvents.itemUse.subscribe((eventData) =>{

	let player = eventData.source;


	var item = eventData.itemStack
	

	const id = item.typeId;
	const itemTags = item.getTags();
	const itemCoolDown = item.getComponent('minecraft:cooldown')

	if (item.hasTag("msls:magic_mirror"))
    {
        
        var xpNeeded = 0
        for (let i = 0; i < itemTags.length; i++)
        {
            if (itemTags[i].indexOf("xp_use:") != -1)
            {
                xpNeeded = Number(itemTags[i].slice(7))
                break
            }
            
        }
        if (xpNeeded == 0 && (id == "msls:magic_mirror" || id == "msls:ice_mirror" ))
        {
            xpNeeded = 1
        }
        if (player.level >= xpNeeded) 
        {
            player.addLevels(-(xpNeeded))
                var spawn = player.getSpawnPoint();
                if (spawn)
                {
                    player.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                    player.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                    player.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
                    player.runCommandAsync('playsound random.magic_mirror_use')
                    
                    system.runTimeout(() => {
                        player.teleport({x: spawn.x, y: spawn.y, z: spawn.z}, {dimension: spawn.dimension, checkForBlocks: true});
                        player.runCommandAsync('playsound random.magic_mirror_use')
                        player.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                        player.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                        player.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
                    }, 1)
                    
                }
                else
                {
                    spawn = world.getDefaultSpawnLocation()
                    player.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                    player.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                    player.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
                    player.runCommandAsync('playsound random.magic_mirror_use')
                    system.runTimeout(() => {
                        teleportToWorldSpawn(player, world)
                    }, 1)
                    
                }
        }
    }
    
    


});

function teleportToSpawn(target)
{

}

function teleportToWorldSpawn(target, world)
{
    var spawn = world.getDefaultSpawnLocation()
    spawn.x += (Math.floor((Math.random() * 11) - 5))
    spawn.z += (Math.floor((Math.random() * 11) - 5)) 
    target.teleport({x: spawn.x, y: 319, z: spawn.z}, {dimension: target.dimension, checkForBlocks: false});
    system.runTimeout(() => {
        for (let i = 319; i > -64; i--)
        {
            var blockCheck = target.dimension.getBlock({x: spawn.x, y: i, z: spawn.z})
            if (blockCheck.isAir == true)
            {
                continue
            }
            else
            {
                if (blockCheck.isAir == false)
                {
                    
                    target.teleport({x: Math.floor(spawn.x) , y: i + 1, z: Math.floor(spawn.z)}, {dimension: target.dimension, checkForBlocks: false});
                    break
                }
            }
        };
        target.runCommandAsync('particle msls:magic_mirror_particle_0 ~ ~1 ~')
                target.runCommandAsync('particle msls:magic_mirror_particle_1 ~ ~1 ~')
                target.runCommandAsync('particle msls:magic_mirror_particle_2 ~ ~1 ~')
                target.runCommandAsync('playsound random.magic_mirror_use')
    }, 1)
    
    
}

