import { system} from "@minecraft/server"

export function RandomTeleport(player,max,min,anim)
{
    if(anim)
    {
        player.addEffect("nausea", 160);
        system.runTimeout(() => {
            player.addEffect("resistance", 500,{amplifier:254});
            player.runCommand(`spreadplayers ~ ~ ${min} ${max} @s`)
            if(player.dimension.id == "minecraft:the_end")
            {
                player.addTag('EndTeleport')
                player.addEffect("slow_falling", 200);
            }
            if(player.dimension.id == "minecraft:overworld")
                {
                    player.addTag('OverworldTeleport')
                    player.addEffect("slow_falling", 200);
                }
        },100)
    }
    else
    {
        player.addEffect("resistance", 500,{amplifier:254});
        player.runCommand(`spreadplayers ~ ~ ${min} ${max} @s`)
        if(player.dimension.id == "minecraft:the_end")
        {
            player.addTag('EndTeleport')
            player.addEffect("slow_falling", 200);
        }
        if(player.dimension.id == "minecraft:overworld")
        {
            player.addTag('OverworldTeleport')
            player.addEffect("slow_falling", 200);
        }
    }
    
}


export function NetherRoofTeleport(player)
{
    try{
        player.dimension.getBlock(player.location)//test if the chunk is loaded

        if(player.dimension.id == "minecraft:nether" && player.location.y>=127)
        {
            
            player.runCommand(`spreadplayers ~ ~ 1 30 @s`)
        }
    }
    catch(e){}
}

export function EndTpPlatform(player)
{
    //try{
        if(!player.hasTag('EndTeleport'))return;
        if(player.dimension.id !="minecraft:the_end") return;
        player.dimension.getBlock(player.location)
        const options = { maxDistance: 255}
        const block = player.dimension.getBlockFromRay(player.location,{x:0,y:-1,z:0},options)
        if(block?.block==undefined)
        {
            player.teleport({x:player.location.x,y:54,z:player.location.z},{keepVelocity:false})
            CreatePlatform(player)
            
        }
        else
            player.addEffect("slow_falling", 600);
        player.removeTag('EndTeleport')

    //}catch(e){}
    
}
export function OverworldTp(player)
{
    //try{
        if(!player.hasTag('OverworldTeleport'))return;
        if(player.dimension.id !="minecraft:overworld") return;
        
        player.dimension.getBlock({x:player.location.x,y:player.location.y-120,z:player.location.z})
        const options = { maxDistance: 400}
        const block = player.dimension.getBlockFromRay(player.location,{x:0,y:-1,z:0},options)
        if(block?.block!=undefined)
        {
            player.teleport({x:player.location.x,y:block.block.location.y+3,z:player.location.z},{keepVelocity:false})
            player.addEffect("slow_falling", 600);
        }
        else
        {
            player.teleport({x:player.location.x,y:120,z:player.location.z},{keepVelocity:false,checkForBlocks:true})
            player.addEffect("slow_falling", 1200);
        }
            
        player.removeTag('OverworldTeleport')
    //}catch(e){}
    
}

function CreatePlatform(player)
{
    //try
    //{
        for(let i = -2; i <= 2 ; i++) {
            for(let j = -2; j <= 2 ; j++) {
                const block = player.dimension.getBlock({x:player.location.x+i,y:player.location.y-7,z:player.location.z+j})
                if(block.typeId=="minecraft:air")
                {
                    block.setType('minecraft:obsidian')
                }
            }
        }
    //}
    //catch(e){}
    
}