import { Player } from '@minecraft/server'
import { ActionFormData, ModalFormData, MessageFormData } from '@minecraft/server-ui'


/**
 * 
 * @param {Player} player 
 */
export function HowToPlay(player) {
    const form = new ActionFormData()
        .title('form.plank_waao.how_to_play_main.title')
        .body({ rawtext: [{ translate: 'form.plank_waao.how_to_play_main.body', with: Array(10).fill('\n') }] })
        .button('form.plank_waao.how_to_play_main.button.mobs', 'textures/eternal/warden_addon/items/egg_sculk_jaw')
        .button('form.plank_waao.how_to_play_main.button.weapons', 'textures/eternal/warden_addon/items/warden_sword')
        .button('form.plank_waao.how_to_play_main.button.armour', 'textures/eternal/warden_addon/items/warden_chestplate')
        .button('form.plank_waao.how_to_play_main.button.items', 'textures/eternal/warden_addon/items/warden_heart')
        .button('form.plank_waao.how_to_play_main.button.close', 'textures/eternal/warden_addon/ui/quit');

    form.show(player).then((r) => {
        if (r.canceled) return;
        const actions = [HowToPlayMobs, HowToPlayWeapons, HowToPlayArmour, HowToPlayItems];
        if (r.selection >= 0 && r.selection < actions.length) {
            actions[r.selection](player);
        }
    });
}

/**
 * 
 * @param {Player} player 
 */
function HowToPlayMobs(player) {
    const pages = [
        { text: 'void_beast', image: 'textures/eternal/warden_addon/ui/boss' },
        { text: 'sculk_pillbug', image: 'textures/eternal/warden_addon/ui/pillbug' },
        { text: 'sculk_jaw', image: 'textures/eternal/warden_addon/ui/sculk_jaw' },
        { text: 'sculk_bug', image: 'textures/eternal/warden_addon/ui/sculk_bug' },
        { text: 'sculker_mine', image: 'textures/eternal/warden_addon/ui/sculk_mine' }
    ];
    actionForm(player, 'mobs', pages, HowToPlayMobs);
}

/**
 * 
 * @param {Player} player 
 */
function HowToPlayWeapons(player) {
    const pages = [
        { text: 'echo_beamer', image: 'textures/eternal/warden_addon/items/sculk_beamer' },
        { text: 'sculk_crusher', image: 'textures/eternal/warden_addon/items/sculk_crusher' },
        { text: 'striker_bow', image: 'textures/eternal/warden_addon/items/striker_bow' },
        { text: 'void_staff', image: 'textures/eternal/warden_addon/items/void_staff' },
        { text: 'sculk_sword', image: 'textures/eternal/warden_addon/items/warden_sword' }
    ];
    actionForm(player, 'weapons', pages, HowToPlayWeapons);
}

/**
 * 
 * @param {Player} player 
 */
function HowToPlayArmour(player) {
    const pages = [
        { text: 'sculk_helmet', image: 'textures/eternal/warden_addon/items/warden_helmet' },
        { text: 'sculk_chestplate', image: 'textures/eternal/warden_addon/items/warden_chestplate' },
        { text: 'sculk_leggings', image: 'textures/eternal/warden_addon/items/warden_leggings' },
        { text: 'sculk_boots', image: 'textures/eternal/warden_addon/items/warden_boots' }
    ];
    actionForm(player, 'armour', pages, HowToPlayArmour);
}

/**
 * 
 * @param {Player} player 
 */
function HowToPlayItems(player) {
    const pages = [
        { text: 'sculk_essence', image: 'textures/eternal/warden_addon/items/sculk_essence' },
        { text: 'sculk_heart', image: 'textures/eternal/warden_addon/items/warden_heart' },
        { text: 'sculk_lighter', image: 'textures/eternal/warden_addon/items/soul_fire_lighter' }
    ];
    actionForm(player, 'items', pages, HowToPlayItems);
}

/**
 * 
 * @param {Player} player 
 * @param {string} category 
 * @param {string} topic 
 * @param {string[]|{{text: string, image: string}}} pages 
 */
function actionForm(player, category, pages, back) {
    const form = new ActionFormData()
        .title(`form.plank_waao.how_to_play_${category}.title`)
        .body(`form.plank_waao.how_to_play_${category}.body`);

    getButtons(form, category, pages);

    form.show(player).then((r) => {
        if (r.canceled || r.selection === pages.length + 1) return;
        if (r.selection === pages.length) { HowToPlay(player); return; };
        messageForm(player, category, pages[r.selection].text || pages[r.selection], back);
    });
}

/**
 * @param {ActionFormData} form
 * @param {string} category 
 * @param {string[]|{{text: string, image: string}}} pages 
 */
function getButtons(form, category, pages) {
    pages.forEach(p => {
        if (p?.image) form.button(`form.plank_waao.how_to_play_${category}.button.${p.text}`, p.image);
        else form.button(`form.plank_waao.how_to_play_${category}.button.${p}`);
    });
    form.button(`form.plank_waao.how_to_play_${category}.button.back`, 'textures/eternal/warden_addon/ui/back');
    form.button(`form.plank_waao.how_to_play_${category}.button.close`, 'textures/eternal/warden_addon/ui/quit');
}


/**
 * 
 * @param {Player} player 
 * @param {string} category 
 * @param {string} topic 
 */
function messageForm(player, category, topic, back) {
    new MessageFormData()
        .title(`form.plank_waao.how_to_play_${category}.${topic}.title`)
        .body({ rawtext: [{ translate: `form.plank_waao.how_to_play_${category}.${topic}.body`, with: Array(20).fill('\n') }] })
        .button2(`form.plank_waao.message_form.button.main_menu`)
        .button1(`form.plank_waao.message_form.button.close`)
        .show(player)
        .then((r) => {
            if (r.canceled) return;
            if (r.selection === 1) {
                back(player);
            }
        });
}


