import { system, world } from '@minecraft/server';
import { ChestFormData } from './extensions/forms';
import { ActionFormData } from '@minecraft/server-ui';

system.runInterval(() => {
for (let player of world.getPlayers()) {
if (player.hasTag("sl_page_list")) {
sl_page_list(player);
player.removeTag("sl_page_list");
}
}
}, 20);

function sl_page_list(player) {
const form = new ChestFormData('large')
.title('§9<{ §dSl Page menu List §9}> §b{2 Page}')
// Page List button
.button(10, '§8<{§d Page Sl Menu: §o§7{Page 1}:', 
[
'§r§8  ',
'§r§8  Klik saya untuk Membuka Page menu sl'
],
'textures/items/book_normal')

// Button Fungsional
.button(0, '§l§fKembali ke Menu Sl', 
[
'§r§8  ',
'§r§8  Klik saya untuk Kembali ke Menu Sl'
],
'textures/items/arrow')
.button(49, '§l§cTutup Menu Page list', 
[
'§r§8  ',
'§r§8  Klik saya untuk Tutup page list'
],
'textures/blocks/barrier')
.button(16, '§8<{§d Credits §8}>', 
[
'§e  > §fCredits :',
'§d    Addons By @Dyfrii§f',
'§d    ChestUI + Command By @VelixsCraftMCYT§f',
'§r§8  '
],
'minecraft:purple_wall_banner', 1, 0, true)

.pattern([
'_eeeeeeee', // 0 1 2 3 4 5 6 7 8
'a_____e_a', // 9 10 11 12 13 14   15 16 17
'a_____e_a', // 18 19 20 21 22 23 24 25 26
'a_____e_a', // 27 28 29 30 31 32 33 34 35
'a_____e_a', // 36 37 38 39 40 41 42 43 44
'eeee_eeee', // 45 46 47 48  49  50 51 52 53
], {
a: { itemName: '§f', enchanted: false, stackAmount: 1, texture: 'textures/blocks/glass_gray' },
e: { itemName: '§f', enchanted: false, stackAmount: 1, texture: 'textures/blocks/glass_white' },
});

form.show(player).then(response => {
if (response.canceled) return;
if (response.selection === 49) {
player.runCommand(`tellraw @s {"rawtext":[{"text":"§aAnda Keluar dari menu"}]}`);
} else if (response.selection === 10) {
player.playSound("conduit.activate", { pitch: 1.0, volume: 0.5 });
player.runCommand(`tag @s add sl_page_1`);

} else if (response.selection === 0) {
player.playSound("conduit.activate", { pitch: 1.0, volume: 0.5 });
player.runCommand(`tag @s add sl_page_1`);
} else if (response.selection === 16) {
player.playSound("conduit.activate", { pitch: 1.0, volume: 0.5 });
player.runCommand(`tag @s add sl_page_list`);
} else if (response.selection === 25) {
player.playSound("conduit.activate", { pitch: 1.0, volume: 0.5 });
player.runCommand(`tag @s add sl_page_list`);

} else {
player.playSound("conduit.activate", { pitch: 1.0, volume: 0.5 });
player.runCommand(`tag @s add sl_page_list`);
}
}).catch(error => {
player.runCommand(`tellraw @s {"rawtext":[{"text":"Terjadi kesalahan: ${error.message}"}]}`);
});
}