import {world} from "@minecraft/server";
let block_speed = false;

const magmaSocksBlocks =[
    'minecraft:netherrack',
    'minecraft:crimson_nylium',
    'minecraft:warped_nylium',
    "edx:deceitweb_block"
]
const furBootsBlocks =[
    'minecraft:snow_layer',
    'minecraft:snow'
]
const penguinLeggingBlocks =[
    'minecraft:ice',
    'minecraft:packed_ice',
    'minecraft:blue_ice',
    "minecraft:water"
]
const voidSocksBlocks =[
    'edx:void_dust'
]
const chorusSocksBlocks =[
    'edx:chorus_grass'
]
const shulkSocksBlocks =[
    'edx:shulk_grass'
]
export function socksSpeedEffect(player)
{
    socksBlockSpeed(player)
    enchantedSocks(player)
}

function enchantedSocks(player)
{
    const speed = world.scoreboard.getObjective("speed");
    if(player.hasTag('enchanted_socks') && !player.hasTag('already_enchanted_socks'))
    {
        speed.addScore(player, 2);
        player.addTag('already_enchanted_socks')
    }
    else if(!player.hasTag('enchanted_socks') && player.hasTag('already_enchanted_socks'))
    {
        speed.addScore(player, -2);
        player.removeTag('already_enchanted_socks')
    }
}
function socksBlockSpeed(player)
{
    if(!player.isOnGround) return
    const speed = world.scoreboard.getObjective("speed");
    block_speed = false
    var already_block_speed = player.hasTag('already_block_speed')
    const blockBelow = player.dimension.getBlock({x:player.location.x,y:player.location.y-0.5,z:player.location.z})
    const block = player.dimension.getBlock(player.location)
    sockBlockBelow(player,block,blockBelow,'magma_socks',magmaSocksBlocks,block_speed)
    sockBlockBelow(player,block,blockBelow,'fur_boots',furBootsBlocks)
    sockBlockBelow(player,block,blockBelow,'penguin_leggings',penguinLeggingBlocks)
    sockBlockBelow(player,block,blockBelow,'void_socks',voidSocksBlocks)
    sockBlockBelow(player,block,blockBelow,'chorus_socks',chorusSocksBlocks)
    sockBlockBelow(player,block,blockBelow,'purpur_socks',shulkSocksBlocks)
    
    if(!already_block_speed && block_speed )
    {
        player.addTag('already_block_speed')
        speed.addScore(player, 1);
    }
        
    else if (already_block_speed && !block_speed )
    {
        player.removeTag('already_block_speed')
        speed.addScore(player, -1);
    }
}
function sockBlockBelow(player,block,blockBelow,tag,blockList)
{
    
    if(player.hasTag(tag) && (blockList.includes(block.typeId) || blockList.includes(blockBelow.typeId)))
    {
        block_speed = true
    }
        
}

export function CloudSocks(player)
{
    if(!player.hasTag('ground') && !player.hasTag('already_double_jump') && !player.isInWater && player.dimension.getBlock(player.location).typeId != "minecraft:lava")
    {
        player.dimension.spawnParticle('edx:cloud_socks_particle',{x:player.location.x,y:player.location.y-0.5,z:player.location.z});
        player.addTag('already_double_jump')
        player.applyKnockback({x:player.getViewDirection().x/2,z: player.getViewDirection().z/2}, 0.5)
    }
}