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

/**
 * Vista principal del sensor de mobs
 * @param {Block} block 
 * @param {Player} player
 */
export const mobSensorViewMain = (block, player) => {
    const actionForm = new ActionFormData();
    actionForm.title({ translate: "view.title.mob_sensor" })//"Mob Sensor"
        .body({ translate: "view.descripton.mob_sensor" })//"§r§9Set up your Mob sensor"
        .button({ translate: "view.button_1.mob_sensor" })//"Filter mobs"
        .button({ translate: "view.button_2.mob_sensor" })//"Distance"
        .button({ translate: "view.button_3.mob_sensor" })//"Exit"
    
    actionForm.show(player).then((data) => {
        if (data.canceled) return;
        if (data.selection == 0) {
            mobSensorViewFilterMobs(block, player);
        } else if (data.selection == 1) {
            mobSensorViewDistance(block, player);
        }
    })
}


/**
 * Vista de filtro de mobs del sensor de mobs
 * @description Vista de filtro de mobs del sensor de mobs, donde se elige el tipo de mobs que activaran el sensor
 * @param {Block} block 
 * @param {Player} player 
 */
const mobSensorViewFilterMobs = (block, player) => { 
    const actionForm = new ActionFormData();
    
    actionForm.title({ translate: "view.title.mob_sensor" })//"Mob Sensor"
        .body({ translate: "view.descripton.mob_sensor" })//"§r§9Set up your Mob sensor"
        .button({ translate: "view.button_4.mob_sensor" })//"Monsters"
        .button({ translate: "view.button_5.mob_sensor" })//"Players"
        .button({ translate: "view.button_6.mob_sensor" })//"Mobs"
        .button({ translate: "view.button_7.mob_sensor" })//"Return"
    
    
    actionForm.show(player).then((data) => { 
        if (data.canceled) return;
        if (data.selection == 0) {
            block.setPermutation(block.permutation.withState('nps_ec:filter', "monster"));
        } else if (data.selection == 1) {
            block.setPermutation(block.permutation.withState('nps_ec:filter', "player"));
        } else if (data.selection == 2) {
            block.setPermutation(block.permutation.withState('nps_ec:filter', "mob"));
        } else {
            mobSensorViewMain(block, player);
        }
    })
}


/**
 * Vista de distancia del sensor de mobs
 * @description Vista de distancia del sensor de mobs, donde se elige la distancia de activacion del sensor
 * @param {Block} block 
 * @param {Player} player 
 */
const mobSensorViewDistance = (block, player) => { 
    const actionForm = new ActionFormData();
    actionForm.title({ translate: "view.title.mob_sensor" })//"Mob Sensor"
        .body({ translate: "view.descripton.mob_sensor" })//"§r§9Set up your Mob sensor"
        .button({ translate: "view.button_b.mob_sensor", with: ["2"]})// Blocks 2
        .button({ translate: "view.button_b.mob_sensor", with: ["4"]})// Blocks 4
        .button({ translate: "view.button_b.mob_sensor", with: ["6"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["8"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["10"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["12"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["14"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["16"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["18"]})
        .button({ translate: "view.button_b.mob_sensor", with: ["20"]})
        .button({ translate: "view.button_7.mob_sensor" })//"Return"
    
    actionForm.show(player).then((data) => { 
        if (data.canceled) return;
        if (data.selection == 0) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 2));
        } else if (data.selection == 1) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 4));
        } else if (data.selection == 2) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 6));
        } else if (data.selection == 3) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 8));
        } else if (data.selection == 4) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 10));
        } else if (data.selection == 5) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 12));
        } else if (data.selection == 6) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 14));
        } else if (data.selection == 7) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 16));
        } else if (data.selection == 8) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 18));
        } else if (data.selection == 9) {
            block.setPermutation(block.permutation.withState('nps_ec:distance', 20));
        } else {
            mobSensorViewMain(block, player);
        }
    })
}
