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

const scoreboardName = "settings";
const settingPlayers = {
    blockParticles: "blockParticles",
    ambientFog: "ambientFog",
    islandFog: "islandFog",
    islandClouds: "islandClouds"
};

world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {

    function getSetting(setting) {
        try {
            return world.scoreboard.getObjective(scoreboardName).getScore(setting) === 1;
        } catch {
            return false;
        }
    }

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_1", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.4x4", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_2", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.6x6", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_3", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.8x8", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_4", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.10x10", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_face_1", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.face.4x4", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_face_2", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.face.6x6", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_face_3", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.face.8x8", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:floor_face_4", {
        onTick: ({ block }) => {
            const islandFog = getSetting(settingPlayers.islandFog);
            if (!islandFog) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:floor_fog.face.10x10", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:island_cloud_1", {
        onTick: ({ block }) => {
            const islandClouds = getSetting(settingPlayers.islandClouds);
            if (!islandClouds) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:island_clouds", anchorLoc);
        },
    });

    blockComponentRegistry.registerCustomComponent("gs_ce:island_cloud_2", {
        onTick: ({ block }) => {
            const islandClouds = getSetting(settingPlayers.islandClouds);
            if (!islandClouds) return;
            let location = block.location
            let anchorLoc = { x: location.x + 0.5, y: location.y, z: location.z + 0.5}
            block.dimension.spawnParticle("gs_ce:island_clouds.1", anchorLoc);
        },
    });

})