import { Block, Dimension, Player } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

/**
 * 
 * @param {Player} player 
 * @param {Block} block 
 * @param {Dimension} dimension
 */
export const weatherControlView = (player, block, dimension) => {
    if (block.permutation.getState('nps_ec:charge') != 4) return;
    const actionForm = new ActionFormData();
    actionForm.title("view.title.weather_control");
    actionForm.button("view.button_1.weather_control");
    actionForm.button("view.button_2.weather_control");
    actionForm.button("view.button_3.weather_control");

    let permutation = block.permutation;

    actionForm.show(player).then((data) => {
        if (data.canceled) return;
        const selectedButton = data.selection;
        switch (selectedButton) {
            case 0:
                dimension.runCommand("weather clear")
                dimension.runCommand(`particle nps_ec:weather_control.clear ${block.location.x} ${block.location.y + 1} ${block.location.z}`);
                dimension.runCommand(`playsound mob.breeze.idle_air @a ${block.x} ${block.y} ${block.z}`)
                permutation = permutation.withState('nps_ec:charge', 0);
                block.setPermutation(permutation);
                break;
            case 1:
                dimension.runCommand("weather rain")
                dimension.runCommand(`particle nps_ec:weather_control.rain ${block.location.x} ${block.location.y + 1} ${block.location.z}`);
                dimension.runCommand(`playsound weather_control.rain @a ${block.x} ${block.y} ${block.z} 5`)
                permutation = permutation.withState('nps_ec:charge', 0);
                block.setPermutation(permutation);
                break;
            case 2:
                dimension.runCommand("weather thunder")
                dimension.runCommand(`particle nps_ec:weather_control.thunder ${block.location.x} ${block.location.y + 1} ${block.location.z}`);
                dimension.runCommand(`playsound ambient.weather.thunder @a ${block.x} ${block.y} ${block.z}`)
                permutation = permutation.withState('nps_ec:charge', 0);
                block.setPermutation(permutation);
                break;
        }
    })
}