import { world, ItemStack, ItemTypes, MolangVariableMap } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";
import { age1_quests_en, age1desc_en, splitquests_en, splitquestsdesc_en, age1_itself_en, age2_itself_en, questbooktitle_en, select_age_en, change_language_en, claim_quest_en, in_progress_en, claim_rewards_en, finished_en, locked_en, rewards_itself_en, rewards_en, splitquestsreward_en, back_en, age3_itself_en, stillinprogress_en, age2_quests_en, age2desc_en, enemiesupgrade_en } from "./languages.js"
import { age1_chs, age1_itself_chs, age2_itself_chs, questbooktitle_chs, select_age_chs, change_language_chs, age1desc_chs, splitquests_chs, splitquestsdesc_chs, back_itself_chs, claim_quest_chs, in_progress_chs, claim_rewards_chs, finished_chs, locked_chs, rewards_itself_chs, rewards_chs, splitquestsreward_chs, age3_itself_chs, stillinprogress_chs, age2_quests_chs, age2desc_chs, enemiesupgrade_chs } from "./languages_ch_s.js"
import { age1_jp, age1_itself_jp, age2_itself_jp, questbooktitle_jp, select_age_jp, change_language_jp, age1desc_jp, splitquests_jp, splitquestsdesc_jp, claim_quest_jp, in_progress_jp, claim_rewards_jp, finished_jp, locked_jp, rewards_itself_jp, rewards_jp, back_jp, splitquestsreward_jp, age3_itself_jp, stillinprogress_jp, age2_quests_jp, age2desc_jp, enemiesupgrade_jp } from "./languages_jp.js"
import { age1_cht, age1_itself_cht, age2_itself_cht, questbooktitle_cht, select_age_cht, change_language_cht, age1desc_cht, splitquests_cht, splitquestsdesc_cht, back_itself_cht, claim_quest_cht, in_progress_cht, claim_rewards_cht, finished_cht, locked_cht, rewards_itself_cht, rewards_cht, splitquestsreward_cht, age3_itself_cht, stillinprogress_cht, age2_quests_cht, age2desc_cht, enemiesupgrade_cht } from "./languages_ch_t.js"
import { age1_esmx, age1_itself_esmx, age2_itself_esmx, questbooktitle_esmx, select_age_esmx, change_language_esmx, age1desc_esmx, splitquests_esmx, splitquestsdesc_esmx, back_itself_esmx, claim_quest_esmx, in_progress_esmx, claim_rewards_esmx, finished_esmx, locked_esmx, rewards_itself_esmx, rewards_esmx, splitquestsreward_esmx, age3_itself_esmx, stillinprogress_esmx, age2_quests_esmx, age2desc_esmx, enemiesupgrade_esmx } from "./languages_esmx.js"

const age1image = ["textures/items/redpetal", "textures/blocks/_trees/bum_lemon_sapling", "textures/items/makeshiftaxe", "textures/blocks/crafting_table_side", "textures/items/campfire", "textures/items/dried leaf", "textures/items/torchflower_seeds", "textures/items/wooden_shavings", "textures/items/makeshiftsieve", "textures/items/flint", "textures/items/paper", "textures/items/fluidbladder", "textures/items/leather", "textures/items/parts/stone/stone_blueprint", "textures/items/diamond_shard", "textures/items/wood_pickaxe", "textures/items/pebble", "textures/blocks/furnace_front_on", "textures/items/obsidianpestle", "textures/items/flint_and_steel", "textures/items/brick", "textures/items/clay_ball", "textures/items/bum_mug", "textures/items/brewer_pottery_sherd", "textures/items/burn_pottery_sherd", "textures/items/dye_powder_white_new", "textures/items/plantfiber", "textures/items/bone"]
const age2image = ["textures/items/brush", "textures/items/pamobile/mb_mossy_bony_helmet", "textures/items/pamobile/mb_mossy_bone", "textures/items/quartz", "textures/items/spider_eye_fermented", "textures/items/pamobile/mb_gloomy_ender_pearl", "textures/items/pamobile/mb_mossy_bony_pickaxe", "textures/blocks/copper_ore", "textures/items/woodupg", "textures/items/bed_white", "textures/ui/recipe_book_icon", "textures/items/woodgear", "textures/items/obsidianpestle", "textures/items/ash", "textures/items/honey_bottle", "textures/items/runeofunbreaking", "textures/items/nethersummon", "textures/items/warpedbark", "textures/items/parts/copper/copper_blueprint", "textures/items/cursedblazee", "textures/items/flame", "textures/blocks/ashenbrick1", "textures/blocks/ashenmeltertop2", "textures/blocks/forgefronton2", "textures/items/copper_ingot", "textures/items/smeltableprimalbronzeblend", "textures/items/bronze_pickaxe", "textures/items/smeltableiron", "textures/items/iron_ingot"]

const splitquestsimage = ["textures/items/campfireitem", "textures/items/tinder", "textures/items/apple_charcoal", "textures/items/charcoal", "textures/blocks/ladder", "textures/items/cattail", "textures/items/kelp", "textures/items/stone_hoe", "textures/items/wheat", "textures/items/thatch", "textures/items/flintscraper", "textures/items/flintaxe", "textures/items/flintsword", "textures/items/reeds", "textures/items/paperpulp", "textures/items/book_portfolio", "textures/items/lantern", "textures/items/fluidbladder", "textures/items/fullfluidbladder", "textures/skins/scraped", "textures/skins/tanned", "textures/items/leather", "textures/items/wood_sword", "textures/items/dryingrackupgrade", "textures/bp/backpack", "textures/items/respawnnest", "textures/blocks/stone_andesite", "textures/blocks/deepslate/cobbled_deepslate", "textures/blocks/tuff", "textures/items/magma_cream", "textures/items/pestle", "textures/items/pinksalt", "textures/items/dye_powder_purple", "textures/items/wheat", "textures/items/brick", "textures/items/rope", "textures/items/woodgear", "textures/items/bum_coffee_brew", "textures/items/amethyst_shard", "textures/items/porcleinclay", "textures/items/porclein",
    "textures/blocks/mb_glowing_skeleton_chiseled_gloomy_stone", "textures/items/mossclump", "textures/items/mossyscraper", "textures/blocks/qcactiveempty", "textures/blocks/pale_oak_sapling", "textures/items/resin_clump", "textures/blocks/creaking_heart_side_dormant", "textures/blocks/altarside", "textures/blocks/pedestal_side", "textures/blocks/moss_block", "textures/blocks/mushroom_brown", "textures/blocks/coal_ore", "textures/items/raw_copper", "textures/items/raw_tin", "textures/items/bed_white", "textures/items/shloud", "textures/items/tinalloy", "textures/ui/recipe_book_icon", "textures/items/twine", "textures/items/ash", "textures/blocks/dust", "textures/items/honey_bottle", "textures/blocks/honeycomb", "textures/items/sets/copper/copper_greatsword", "textures/items/sets/tin/tin_lance", "textures/items/bow_standby", "textures/items/copper_ingot", "textures/items/tin_ingot", "textures/items/raw_iron", "textures/items/iron_ingot"]


const languageselect = ["English", "Japanese (日本語)", "Chinese Simplified (简体中文)", "Chinese Traditional (繁体中文)", "Español"]
const header = [age1_itself_en, age1_itself_jp, age1_itself_chs, age1_itself_cht, age1_itself_esmx]
const header2 = [age2_itself_en, age2_itself_jp, age2_itself_chs, age2_itself_cht, age2_itself_esmx]
const header3 = [age3_itself_en, age3_itself_jp, age3_itself_chs, age3_itself_cht, age3_itself_esmx]
const questbooktitlelang = [questbooktitle_en, questbooktitle_jp, questbooktitle_chs, questbooktitle_cht, questbooktitle_esmx]
const select_agelang = [select_age_en, select_age_jp, select_age_chs, select_age_cht, select_age_esmx]
const change_languagelang = [change_language_en, change_language_jp, change_language_chs, change_language_cht, change_language_esmx]
const claim_questlang = [claim_quest_en, claim_quest_jp, claim_quest_chs, claim_quest_cht, claim_quest_esmx]
const in_progresslang = [in_progress_en, in_progress_jp, in_progress_chs, in_progress_cht, in_progress_esmx]
const claim_rewardlang = [claim_rewards_en, claim_rewards_jp, claim_rewards_chs, claim_rewards_cht, claim_rewards_esmx]
const finishedlang = [finished_en, finished_jp, finished_chs, finished_cht, finished_esmx]
const lockedlang = [locked_en, locked_jp, locked_chs, locked_cht, locked_esmx]
const rewards_itselflang = [rewards_itself_en, rewards_itself_jp, rewards_itself_chs, rewards_itself_cht, rewards_itself_esmx]
const back_itselflang = [back_en, back_jp, back_itself_chs, back_itself_cht, back_itself_esmx]
const still_in_progress = [stillinprogress_en, stillinprogress_jp, stillinprogress_chs, stillinprogress_cht, stillinprogress_esmx]


const enemiesupgrade = [enemiesupgrade_en, enemiesupgrade_jp, enemiesupgrade_chs, enemiesupgrade_cht, enemiesupgrade_esmx]

var age1 = age1_quests_en
var age1desc = age1desc_en
var rewards = rewards_en
var splitquests = splitquests_en
var age2 = age2_quests_en
var age2desc = age2desc_en
var splitquestsdesc = splitquestsdesc_en
var splitquestsreward = splitquestsreward_en

var questlist = age1
var questlistdesc = age1desc

world.afterEvents.itemUse.subscribe((eventData) => {
    const itemTypeId = eventData.itemStack.typeId
    if (itemTypeId == "pcr:questbook") {
        questing.questbookopen(eventData.source)
    }
})

function showquestlist(player, age) {

    const lang = world.scoreboard.getObjective('language').getScore(player)
    const questfinished = world.scoreboard.getObjective('questfin').getScore(player)

    const form = new ActionFormData()
    questing.setdescription(lang)
    if (age == 1) {
        form.title(header[lang])
        const ageheader = age1
        for (let quests in age1) {
            form.button(age1[quests], age1image[quests])
        }
        form.show(player).then((response) => {
            switch (response?.selection) {
                case 0:
                    questing.createquest(0, 0, 0, -1, player, "bumble:cherry_pie", 1, undefined, "pcr:offeringbox")
                    break
                case 1:
                    questing.createquest(1, 1, 1, 0, player, "bumble:grapes", 1)
                    break
                case 2:
                    questing.createquest(2, 2, 2, 1, player, "rns:pebble", 1)
                    break
                case 3:
                    questing.createquest(3, 3, 3, 2, player, "air", 1)
                    break
                case 4:
                    if (questfinished >= 4) {
                        showsplitquest(0, 3, player, 1, 4, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 5:
                    if (questfinished >= 4) {
                        showsplitquest(4, 6, player, 2, 5, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 6:
                    if (questfinished >= 4) {
                        showsplitquest(7, 9, player, 3, 6, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 7:
                    questing.createquest(7, 4, 4, 3, player, "air", 1)
                    break
                case 8:
                    questing.createquest(8, 5, 5, 4, player, "dropper", 1)
                    break
                case 9:
                    if (questfinished >= 6) {
                        showsplitquest(10, 12, player, 4, 9, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 10:
                    if (questfinished >= 6) {
                        showsplitquest(13, 16, player, 5, 10, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 11:
                    if (questfinished >= 6) {
                        showsplitquest(17, 18, player, 6, 11, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 12:
                    if (questfinished >= 6) {
                        showsplitquest(19, 21, player, 7, 12, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 13:
                    questing.createquest(13, 6, 6, 5, player, "rns:roastedstuffedbeetroot", 1, "confetti")
                    break
                case 14:
                    if (questfinished >= 7) {
                        showsplitquest(22, 25, player, 8, 14, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 15:
                    questing.createquest(15, 7, 7, 6, player, "air")
                    break
                case 16:
                    if (questfinished >= 8) {
                        showsplitquest(26, 29, player, 9, 16, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 17:
                    questing.createquest(17, 8, 8, 7, player, "coal", 1)
                    break
                case 18:
                    if (questfinished >= 4) {
                        showsplitquest(30, 32, player, 10, 18, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 19:
                    questing.createquest(19, 9, 9, 8, player, "hax:obsidian_dagger", 1)
                    break
                case 20:
                    if (questfinished >= 4) {
                        showsplitquest(33, 34, player, 11, 20, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 21:
                    if (questfinished >= 4) {
                        showsplitquest(35, 37, player, 12, 21, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 22:
                    questing.createquest(22, 10, 10, 9, player, "bumble:coffee_beans", 1)
                    break
                case 23:
                    questing.createquest(23, 11, 11, 10, player, "light_blue_glazed_terracotta", 1)
                    break
                case 24:
                    questing.createquest(24, 12, 12, 11, player, "red_glazed_terracotta", 1)
                    break
                case 25:
                    if (questfinished >= 4) {
                        showsplitquest(38, 40, player, 13, 25, 1)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 26:
                    questing.createquest(26, 13, 13, 12, player, "rns:rocksaltlamp", 1)
                    break
                case 27:
                    questing.createquest(27, 14, 14, 13, player, "cake", 1, "confetti")
                    break
            }
        })
    }
    if (age == 2) {
        form.title(header2[lang])
        for (let quests in age2) {
            form.button(age2[quests], age2image[quests])
        }
        form.show(player).then((response) => {
            switch (response?.selection) {
                case 0:
                    questing.createquest(1, 1, 15, 14, player, "c8164:caked_on", 2)
                    break
                case 1:
                    if (questfinished >= 15) {
                        showsplitquest(41, 43, player, 14, 1, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 2:
                    questing.createquest(2, 2, 16, 15, player, "minecraft:bone_block", 2)
                    break
                case 3:
                    if (questfinished >= 16) {
                        showsplitquest(44, 49, player, 15, 3, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 4:
                    questing.createquest(4, 3, 17, 16, player, "air", 2)
                    break
                case 5:
                    if (questfinished >= 17) {
                        showsplitquest(50, 52, player, 16, 5, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 6:
                    questing.createquest(6, 4, 18, 17, player, "mb:mossy_skeleton_2", 2)
                    break
                case 7:
                    if (questfinished >= 18) {
                        showsplitquest(53, 54, player, 17, 7, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 8:
                    questing.createquest(8, 5, 19, 18, player, "bumble:orange_sapling_item", 2)
                    break
                case 9:
                    if (questfinished >= 19) {
                        showsplitquest(55, 56, player, 18, 9, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 10:
                    if (questfinished >= 19) {
                        showsplitquest(57, 58, player, 19, 10, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 11:
                    questing.createquest(11, 6, 20, 19, player, "hax:raw_tin", 2)
                    break
                case 12:
                    questing.createquest(12, 7, 21, 20, player, "minecraft:hay_block", 2)
                    break
                case 13:
                    if (questfinished >= 21) {
                        showsplitquest(59, 61, player, 20, 13, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 14:
                    if (questfinished >= 21) {
                        showsplitquest(62, 63, player, 21, 14, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 15:
                    questing.createquest(15, 8, 22, 21, player, "c8164:hat_witch", 2)
                    break
                case 16:
                    questing.createquest(16, 9, 23, 22, player, "bumble:chili", 2)
                    break
                case 17:
                    questing.createquest(17, 10, 24, 23, player, "air", 2)
                    break
                case 18:
                    if (questfinished >= 24) {
                        showsplitquest(64, 66, player, 22, 18, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 19:
                    questing.createquest(19, 11, 25, 24, player, "minecraft:golden_apple", 2)
                    break
                case 20:
                    questing.createquest(20, 12, 26, 25, player, "minecraft:experience_bottle", 2)
                    break
                case 21:
                    questing.createquest(21, 13, 27, 26, player, "minecraft:clay", 2)
                    break
                case 22:
                    questing.createquest(22, 14, 28, 27, player, "air", 2)
                    break
                case 23:
                    questing.createquest(23, 15, 29, 28, player, "c8164:mask_furnace", 2)
                    break
                case 24:
                    if (questfinished >= 29) {
                        showsplitquest(67, 68, player, 23, 24, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 25:
                    questing.createquest(25, 16, 30, 29, player, "hax:bronze_spadehead", 2)
                    break
                case 26:
                    questing.createquest(26, 17, 31, 30, player, "minecraft:cooked_porkchop", 2)
                    break
                case 27:
                    if (questfinished >= 31) {
                        showsplitquest(69, 70, player, 24, 27, 2)
                    }
                    else {
                        player.runCommandAsync('say ' + lockedlang[lang])
                    }
                    break
                case 28:
                    questing.createquest(28, 18, 32, 31, player, "minecraft:goat_horn", 2, "confetti")

                    break
            }
        })
    }
}

//Amount should stack up with the same quest set lines!!

function showsplitquest(start, end, player, questset, settitle, age) {
    const lang = world.scoreboard.getObjective('language').getScore(player)
    const form = new ActionFormData()
    questing.whatage(age)
    questing.setdescription(lang)
    form.title(questlist[settitle])
    let backamount = 0
    for (let i = start; i < end + 1; i++) {
        form.button(splitquests[i], splitquestsimage[i])
        backamount += 1
    }
    form.button(back_itselflang[lang])
    form.show(player).then((response) => {
        if (response?.selection === backamount) {
            showquestlist(player, age)
        }
        if (questset === 1) {
            if (response?.selection === 0) {
                //Quest in Index, Split Quest Progression Number (aka questline 1, questline 2), Player, Reward, Quests Needed, Which quest gate to choose, Return the Quest set, Start Index of set, End index of set, settitle, age
                questing.createsplitquest(1, 1, player, "log", 12, 1, 1, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(2, 2, player, "air", 12, 1, 1, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(3, 3, player, "apple", 12, 1, 1, start, end, settitle, age)
            }
            else if (response?.selection === 3) {
                questing.createsplitquest(4, 4, player, "torch", 12, 1, 1, start, end, settitle, age)
            }
        }
        else if (questset === 2) {
            if (response?.selection === 0) {
                //adjust first row to +1
                questing.createsplitquest(5, 1, player, "air", 12, 2, 2, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(6, 2, player, "rns:applechip", 12, 2, 2, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(7, 3, player, "c8164:hat_sun", 12, 2, 2, start, end, settitle, age)
            }
        }
        else if (questset === 3) {
            if (response?.selection === 0) {
                questing.createsplitquest(8, 1, player, "flint", 12, 3, 3, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(9, 2, player, "bumble:random_seed_bag", 12, 3, 3, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(10, 3, player, "air", 12, 3, 3, start, end, settitle, age)
            }
        }
        else if (questset === 4) {
            if (response?.selection === 0) {
                questing.createsplitquest(11, 5, player, "flint", 18, 1, 4, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(12, 6, player, "air", 18, 1, 4, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(13, 7, player, "hax:slime_berserker_helmet", 18, 1, 4, start, end, settitle, age)
            }
        }
        else if (questset === 5) {
            if (response?.selection === 0) {
                questing.createsplitquest(14, 4, player, "hax:wood_cleaver", 18, 2, 5, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(15, 5, player, "air", 18, 2, 5, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(16, 6, player, "add:washed_hide", 18, 2, 5, start, end, settitle, age)
            }
            else if (response?.selection === 3) {
                questing.createsplitquest(17, 7, player, "hax:slime_boots", 18, 2, 5, start, end, settitle, age)
            }
        }
        else if (questset === 6) {
            if (response?.selection === 0) {
                questing.createsplitquest(18, 4, player, "air", 18, 3, 6, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(19, 5, player, "bumble:potted_cattail", 18, 3, 6, start, end, settitle, age)
            }
        }
        else if (questset === 7) {
            if (response?.selection === 0) {
                questing.createsplitquest(20, 8, player, "bumble:chili", 18, 1, 7, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(21, 9, player, "btb:makeshiftsieve", 18, 1, 7, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(22, 10, player, "leather", 18, 1, 7, start, end, settitle, age)
            }
        }
        else if (questset === 8) {
            if (response?.selection === 0) {
                questing.createsplitquest(23, 8, player, "air", 21, 2, 8, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(24, 9, player, "rns:pinkrocksalt", 21, 2, 8, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(25, 10, player, "c8164:backpack_travelling", 21, 2, 8, start, end, settitle, age)
            }
            else if (response?.selection === 3) {
                questing.createsplitquest(26, 11, player, "c8164:blindfold1", 21, 2, 8, start, end, settitle, age)
            }
        }
        else if (questset === 9) {
            if (response?.selection === 0) {
                questing.createsplitquest(27, 6, player, "air", 24, 3, 9, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(28, 7, player, "air", 24, 3, 9, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(29, 8, player, "torch", 24, 3, 9, start, end, settitle, age)
            }
            else if (response?.selection === 3) {
                questing.createsplitquest(30, 9, player, "torch", 24, 3, 9, start, end, settitle, age)
            }
        }
        else if (questset === 10) {
            if (response?.selection === 0) {
                questing.createsplitquest(31, 11, player, "sweet_berries", 27, 1, 10, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(32, 12, player, "glow_berries", 27, 1, 10, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(33, 13, player, "rns:nimberry", 27, 1, 10, start, end, settitle, age)
            }
        }
        else if (questset === 11) {
            if (response?.selection === 0) {
                questing.createsplitquest(34, 12, player, "air", 30, 2, 11, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(35, 13, player, "bumble:popcorn", 30, 2, 11, start, end, settitle, age)
            }
        }
        else if (questset === 12) {
            if (response?.selection === 0) {
                questing.createsplitquest(36, 10, player, "sev:twine", 30, 3, 12, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(37, 11, player, "sev:twine", 30, 3, 12, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(38, 12, player, "clay", 30, 3, 12, start, end, settitle, age)
            }
        }
        else if (questset === 13) {
            if (response?.selection === 0) {
                questing.createsplitquest(39, 14, player, "budding_amethyst", 39, 1, 13, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(40, 15, player, "bone", 39, 1, 13, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(41, 16, player, "calcite", 39, 1, 13, start, end, settitle, age)
            }
        }
        else if (questset === 14) {
            if (response?.selection === 0) {
                questing.createsplitquest(42, 14, player, "mb:gloomy_vase", 45, 2, 14, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(43, 15, player, "small_dripleaf_block", 45, 2, 14, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(44, 16, player, "rns:flintscraper", 45, 2, 14, start, end, settitle, age)
            }
        }
        else if (questset === 15) {
            if (response?.selection === 0) {
                questing.createsplitquest(45, 13, player, "pcr:claycooker", 48, 3, 15, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(46, 14, player, "light_gray_bundle", 48, 3, 15, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(47, 15, player, "air", 48, 3, 15, start, end, settitle, age)
            }
            else if (response?.selection === 3) {
                questing.createsplitquest(48, 16, player, "baked_potato", 48, 3, 15, start, end, settitle, age)
            }
            else if (response?.selection === 4) {
                questing.createsplitquest(49, 17, player, "pumpkin_pie", 48, 3, 15, start, end, settitle, age)
            }
            else if (response?.selection === 5) {
                questing.createsplitquest(50, 18, player, "quartz", 48, 3, 15, start, end, settitle, age)
            }
        }
        else if (questset === 16) {
            if (response?.selection === 0) {
                questing.createsplitquest(51, 17, player, "air", 51, 1, 16, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(52, 18, player, "air", 51, 1, 16, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(53, 19, player, "c8164:crown_flower", 51, 1, 16, start, end, settitle, age)
            }
        }
        else if (questset === 17) {
            if (response?.selection === 0) {
                questing.createsplitquest(54, 17, player, "bumble:sandwich", 54, 2, 17, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(55, 18, player, "bumble:sandwich", 54, 2, 17, start, end, settitle, age)
            }
        }
        else if (questset === 18) {
            if (response?.selection === 0) {
                questing.createsplitquest(56, 19, player, "bumble:tea_chamomile", 57, 3, 18, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(57, 20, player, "minecraft:name_tag", 57, 3, 18, start, end, settitle, age)
            }
        }
        else if (questset === 19) {
            if (response?.selection === 0) {
                questing.createsplitquest(58, 20, player, "hax:tin_spartan_helmet", 57, 1, 19, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(59, 21, player, "air", 57, 1, 19, start, end, settitle, age)
            }
        }
        else if (questset === 20) {
            if (response?.selection === 0) {
                questing.createsplitquest(60, 19, player, "air", 60, 2, 20, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(61, 20, player, "fuelov:beetroot_charcoal", 60, 2, 20, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(62, 21, player, "minecraft:potion", 60, 2, 20, start, end, settitle, age)
            }
        }
        else if (questset === 21) {
            if (response?.selection === 0) {
                questing.createsplitquest(63, 21, player, "minecraft:glass_bottle", 60, 3, 21, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(64, 22, player, "minecraft:honeycomb_block", 60, 3, 21, start, end, settitle, age)
            }
        }
        else if (questset === 22) {
            if (response?.selection === 0) {
                questing.createsplitquest(65, 22, player, "minecraft:raw_copper", 66, 1, 22, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(66, 23, player, "hax:raw_tin", 66, 1, 22, start, end, settitle, age)
            }
            else if (response?.selection === 2) {
                questing.createsplitquest(67, 24, player, "minecraft:target", 66, 1, 22, start, end, settitle, age)
            }
        }
        else if (questset === 23) {
            if (response?.selection === 0) {
                questing.createsplitquest(68, 22, player, "air", 75, 2, 23, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(69, 23, player, "air", 75, 2, 23, start, end, settitle, age)
            }
        }
        else if (questset === 24) {
            if (response?.selection === 0) {
                questing.createsplitquest(70, 23, player, "air", 78, 3, 24, start, end, settitle, age)
            }
            else if (response?.selection === 1) {
                questing.createsplitquest(71, 24, player, "air", 78, 3, 24, start, end, settitle, age)
            }
        }
    })
}


function confetti(player) {
    player.playSound("random.levelup")
    for (let i = 0; i < 200; i++) {
        const molang = new MolangVariableMap()

        molang.setColorRGBA('variable.color', {
            red: Math.random(),
            green: Math.random(),
            blue: Math.random(),
            alpha: 1
        })

        const newLocation = {
            x: player.location.x + Math.floor(Math.random() * 6) - 3,
            y: player.location.y + Math.floor(Math.random() * 6) - 3,
            z: player.location.z + Math.floor(Math.random() * 6) - 3,
        }
        player.dimension.spawnParticle("minecraft:falling_dust_concrete_powder_particle", newLocation, molang)
    }
}

function givecom(item, amount, player) {
    if (item != "air") {
        player.runCommandAsync('give @s ' + item + " " + amount)
    }
    player.runCommandAsync('summon xp_orb')
}




class questing {
    constructor(player, lang, questnum, questdesc, questtest, questlist, questlistdesc) {

        //Progressive Enemies Age
        this.questnum = questnum
        this.questdesc = questdesc


        //Age Selectors

        this.questtest = questtest
    }

    static questbookopen(player) {
        const lang = world.scoreboard.getObjective('language').getScore(player)
        const quests = world.scoreboard.getObjective('quests').getScore(player)
        const page = world.scoreboard.getObjective('page').getScore(player)

        const form = new ActionFormData()

        form.title(questbooktitlelang[lang])
        form.body(select_agelang[lang])
        form.button(header[lang], "textures/items/redbook")
        form.button(header2[lang], "textures/items/orangebook")
        form.button(header3[lang], "textures/items/yellowbook")
        form.button(change_languagelang[lang], "textures/items/language")

        form.show(player).then((response) => {
            switch (response?.selection) {
                case 0:
                    showquestlist(player, 1)
                    break
                case 1:
                    if (quests >= 45) {
                        showquestlist(player, 2)
                        if (page < 1) {
                            player.runCommandAsync('scoreboard players set @s page 1')
                            player.sendMessage(enemiesupgrade[lang])
                        }
                    }
                    else {
                        player.sendMessage(lockedlang[lang])
                    }
                    break
                case 2:
                    //quests >= 98
                    player.sendMessage(still_in_progress[lang])
                    break
                case 3:
                    this.languagechoice(player)
                    break
            }
        })
    }

    static languagechoice(player) {
        const form = new ActionFormData()
        const language = world.scoreboard.getObjective('language').getScore(player)
        form.title(change_languagelang[language])
        for (let lang in languageselect) {
            form.button(languageselect[lang])
        }
        form.show(player).then((response) => {
            switch (response?.selection) {
                case 0:
                    player.runCommandAsync('scoreboard players set @s language 0')
                    break
                case 1:
                    player.runCommandAsync('scoreboard players set @s language 1')
                    break
                case 2:
                    player.runCommandAsync('scoreboard players set @s language 2')
                    break
                case 3:
                    player.runCommandAsync('scoreboard players set @s language 3')
                    break
                case 4:
                    player.runCommandAsync('scoreboard players set @s language 4')
                    break
            }
        })
    }

    static whatage(age) {
        if (age == 1) {
            questlist = age1
            questlistdesc = age1desc
        }
        if (age == 2) {
            questlist = age2
            questlistdesc = age2desc
        }
    }
    //                 Quest In Index, Quest Description Index, Quest Progression Number(Locked, Claim, In Progress, Finished), Previous Quest Needed, Player, Reward Item, What Age, Particle(Not Necessary), Press Claim Get Item
    static createquest(questnum, questdesc, amount, previousquestsfinished, player, reward, age, particle, claimreward) {

        const lang = world.scoreboard.getObjective('language').getScore(player)
        const quests = world.scoreboard.getObjective('quests').getScore(player)
        const questfinished = world.scoreboard.getObjective('questfin').getScore(player)

        this.whatage(age)
        const form = new ActionFormData()
        this.setdescription(lang)
        form.title(questlist[questnum])
        form.body(questlistdesc[questdesc] + "\n" + rewards_itselflang[lang] + rewards[amount])
        if (quests == amount * 3 && questfinished > previousquestsfinished) {
            form.button(claim_questlang[lang])
        }
        else if (quests == amount * 3 + 1 && questfinished > previousquestsfinished) {
            form.button(in_progresslang[lang])
        }
        else if (quests == amount * 3 + 2 && questfinished > previousquestsfinished) {
            form.button(claim_rewardlang[lang])
        }
        else if (quests > 3 * amount) {
            form.button(finishedlang[lang])
        }
        else {
            form.button(lockedlang[lang])
        }
        form.button(back_itselflang[lang])
        form.show(player).then((response) => {
            if (quests == amount * 3 && questfinished > previousquestsfinished && response?.selection === 0) {
                if (claimreward != undefined) {
                    givecom(claimreward, 1, player)
                }
                player.runCommandAsync('scoreboard players add @s quests 1')
                showquestlist(player, age)
            }
            if (quests == amount * 3 + 2 && questfinished > previousquestsfinished && response?.selection === 0) {
                player.runCommandAsync('scoreboard players add @s quests 1')
                givecom(reward, 1, player)
                player.runCommandAsync('scoreboard players add @s questfin 1')
                if (particle != undefined) {
                    if (particle == "confetti") {
                        confetti(player)
                    }
                }
            }
            else if (response?.selection === 1) {
                showquestlist(player, age)
            }
        })
    }
    //Quest in Index, Split Quest Progression Number, Player, Reward, Quests Needed, Which quest gate to choose, Return the Quest set, Start Index of set, End index of set, settitle, age
    static createsplitquest(questnum, amount, player, reward, requirement, questset, returnquestset, start, end, settitle, age) {
        const lang = world.scoreboard.getObjective('language').getScore(player)
        //Overall Quests
        const quests = world.scoreboard.getObjective('quests').getScore(player)
        //Each track a different quest gating mechanism
        const quest1 = world.scoreboard.getObjective('quest1').getScore(player)
        const quest2 = world.scoreboard.getObjective('quest2').getScore(player)
        const quest3 = world.scoreboard.getObjective('quest3').getScore(player)


        const form = new ActionFormData()
        questing.setdescription(lang)
        //Quests starting from 1
        form.title(splitquests[questnum - 1])
        form.body(splitquestsdesc[questnum - 1] + rewards_itselflang[lang] + splitquestsreward[questnum - 1])
        if (questset === 1) {
            if (quest1 == amount * 3 - 3 && quests >= requirement) {
                form.button(claim_questlang[lang])
            }
            else if (quest1 == amount * 3 - 2 && quests >= requirement) {
                form.button(in_progresslang[lang])
            }
            else if (quest1 == amount * 3 - 1 && quests >= requirement) {
                form.button(claim_rewardlang[lang])
            }
            else if (quest1 > 3 * amount - 3) {
                form.button(finishedlang[lang])
            }
            else {
                form.button(lockedlang[lang])
            }
            form.button(back_itselflang[lang])
            form.show(player).then((response) => {
                if (quest1 == amount * 3 - 3 && quests >= requirement && response?.selection === 0) {
                    player.runCommandAsync('scoreboard players add @s quest1 1')
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
                if (quest1 == amount * 3 - 1 && quests >= requirement && response?.selection === 0) {
                    player.runCommandAsync('scoreboard players add @s quest1 1')
                    givecom(reward, 1, player)
                    player.runCommandAsync('scoreboard players add @s questfin 1')
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
                else if (response?.selection === 1) {
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
            })
        }
        else if (questset === 2) {
            if (quest2 == amount * 3 - 3 && quests >= requirement) {
                form.button(claim_questlang[lang])
            }
            else if (quest2 == amount * 3 - 2 && quests >= requirement) {
                form.button(in_progresslang[lang])
            }
            else if (quest2 == amount * 3 - 1 && quests >= requirement) {
                form.button(claim_rewardlang[lang])
            }
            else if (quest2 > 3 * amount - 3) {
                form.button(finishedlang[lang])
            }
            else {
                form.button(lockedlang[lang])
            }
            form.button(back_itselflang[lang])
            form.show(player).then((response) => {
                if (quest2 == amount * 3 - 3 && quests >= requirement && response?.selection === 0) {
                    player.runCommandAsync('scoreboard players add @s quest2 1')
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
                if (quest2 == amount * 3 - 1 && quests >= requirement && response?.selection === 0) {
                    player.runCommandAsync('scoreboard players add @s quest2 1')
                    givecom(reward, 1, player)
                    player.runCommandAsync('scoreboard players add @s questfin 1')
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
                else if (response?.selection === 1) {
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
            })
        }
        else if (questset === 3) {
            if (quest3 == amount * 3 - 3 && quests >= requirement) {
                form.button(claim_questlang[lang])
            }
            else if (quest3 == amount * 3 - 2 && quests >= requirement) {
                form.button(in_progresslang[lang])
            }
            else if (quest3 == amount * 3 - 1 && quests >= requirement) {
                form.button(claim_rewardlang[lang])
            }
            else if (quest3 > 3 * amount - 3) {
                form.button(finishedlang[lang])
            }
            else {
                form.button(lockedlang[lang])
            }
            form.button(back_itselflang[lang])
            form.show(player).then((response) => {
                if (quest3 == amount * 3 - 3 && quests >= requirement && response?.selection === 0) {
                    player.runCommandAsync('scoreboard players add @s quest3 1')
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
                if (quest3 == amount * 3 - 1 && quests >= requirement && response?.selection === 0) {
                    player.runCommandAsync('scoreboard players add @s quest3 1')
                    givecom(reward, 1, player)
                    player.runCommandAsync('scoreboard players add @s questfin 1')
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
                else if (response?.selection === 1) {
                    showsplitquest(start, end, player, returnquestset, settitle, age)
                }
            })
        }
    }

    static testing2() {
        world.sendMessage("wow")
    }

    static setdescription(lang) {
        if (lang == 0) {
            age1 = age1_quests_en
            age1desc = age1desc_en
            rewards = rewards_en
            splitquests = splitquests_en
            splitquestsdesc = splitquestsdesc_en
            splitquestsreward = splitquestsreward_en
            age2 = age2_quests_en
            age2desc = age2desc_en
        }
        else if (lang == 1) {
            age1 = age1_jp
            age1desc = age1desc_jp
            rewards = rewards_jp
            splitquests = splitquests_jp
            splitquestsdesc = splitquestsdesc_jp
            splitquestsreward = splitquestsreward_jp
            age2 = age2_quests_jp
            age2desc = age2desc_jp
        }
        else if (lang == 2) {
            age1 = age1_chs
            age1desc = age1desc_chs
            rewards = rewards_chs
            splitquests = splitquests_chs
            splitquestsdesc = splitquestsdesc_chs
            splitquestsreward = splitquestsreward_chs
            age2 = age2_quests_chs
            age2desc = age2desc_chs
        }
        else if (lang == 3) {
            age1 = age1_cht
            age1desc = age1desc_cht
            rewards = rewards_cht
            splitquests = splitquests_cht
            splitquestsdesc = splitquestsdesc_cht
            splitquestsreward = splitquestsreward_cht
            age2 = age2_quests_cht
            age2desc = age2desc_cht
        }
        else if (lang == 4) {
            age1 = age1_esmx
            age1desc = age1desc_esmx
            rewards = rewards_esmx
            splitquests = splitquests_esmx
            splitquestsdesc = splitquestsdesc_esmx
            splitquestsreward = splitquestsreward_esmx
            age2 = age2_quests_esmx
            age2desc = age2desc_esmx
        }
    }
}