import { ActionFormData } from "@minecraft/server-ui"

import { GEAR_TIERS_DESCRIPTION, MAIN_MENU_DESCRIPTION, NEW_ITEM_DESCRIPTION, NEW_MATERIAL_DESCRIPTION, NEW_WEAPON_DESCRIPTION } from "./description"

function openInfo(player, title, description, back) {
  const actionForm = new ActionFormData()
  actionForm.title(title)
  actionForm.body(description)
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.button_back"
      },
    ],
  })
  actionForm.show(player).then((e) => {
    if (e.canceled) return
    back(player)
  })
}

function openGearTiers(player) {
  const actionForm = new ActionFormData()
  actionForm.title({
    rawtext: [
      {
        translate: "4ks_wp.gear_button"
      },
    ],
  })
  actionForm.body(GEAR_TIERS_DESCRIPTION)
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.button_back"
      },
    ],
  })
  actionForm.show(player).then((e) => {
    if (e.canceled) return
    openGuidebook(player)
  })
}


function openNewMaterials(player) {
  const actionForm = new ActionFormData()
  actionForm.title({
    rawtext: [
      {
        translate: "4ks_wp.material_button"
      },
    ],
  })
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.bedrock"
      },
    ],
  }, "textures/4ks/wp/items/bedrock_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.cryo"
      },
    ],
  }, "textures/4ks/wp/items/cryo_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.demon"
      },
    ],
  }, "textures/4ks/wp/items/demonic_shard")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.divine"
      },
    ],
  }, "textures/4ks/wp/items/divine_shard")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.dragon"
      },
    ],
  }, "textures/4ks/wp/items/dragon_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.flame"
      },
    ],
  }, "textures/4ks/wp/items/flame_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.platinum"
      },
    ],
  }, "textures/4ks/wp/items/platinum_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.ruby"
      },
    ],
  }, "textures/4ks/wp/items/ruby_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.sapphire"
      },
    ],
  }, "textures/4ks/wp/items/sapphire_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.steel"
      },
    ],
  }, "textures/4ks/wp/items/steel_ingot")


  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.sculk"
      },
    ],
  }, "textures/4ks/wp/items/sculk_ingot")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.xp_ore"
      },
    ],
  }, "textures/4ks/wp/blocks/xp_ore")



  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.uranium_ingot"
      },
    ],
  }, "textures/4ks/wp/items/uranium_ingot")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.button_back"
      },
    ],
  })
  actionForm.show(player).then(e => {
    if (e.canceled) return
    else if (e.selection == 0) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.bedrock_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.bedrock, openNewMaterials)
    else if (e.selection == 1) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.cryo_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.cryo, openNewMaterials)
    else if (e.selection == 2) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.demon_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.demonic, openNewMaterials)
    else if (e.selection == 3) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.divine_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.divine, openNewMaterials)
    else if (e.selection == 4) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.dragon_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.dragon, openNewMaterials)
    else if (e.selection == 5) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.flame_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.flame, openNewMaterials)
    else if (e.selection == 6) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.platinum_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.platinum, openNewMaterials)
    else if (e.selection == 7) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.ruby_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.ruby, openNewMaterials)
    else if (e.selection == 8) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.sapphire_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.sapphire, openNewMaterials)
    else if (e.selection == 9) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.steel_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.steel, openNewMaterials)


    else if (e.selection == 10) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.sculk_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.sculk, openNewMaterials)

    else if (e.selection == 11) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.xp_ore"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.xp_ore, openNewMaterials)


    else if (e.selection == 12) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.uranium_ingot"
        },
      ],
    }, NEW_MATERIAL_DESCRIPTION.uranium, openNewMaterials)
    else openGuidebook(player)
  })
}

function openNewItems(player) {
  const actionForm = new ActionFormData()
  actionForm.title({
    rawtext: [
      {
        translate: "4ks_wp.item_button"
      },
    ],
  })
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.platinum_rod"
      },
    ],
  }, "textures/4ks/wp/items/platinum_stick")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.quartz_stick"
      },
    ],
  }, "textures/4ks/wp/items/quartz_stick")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.steel_stick"
      },
    ],
  }, "textures/4ks/wp/items/steel_stick")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.upgradium"
      },
    ],
  }, "textures/4ks/wp/items/upgradium_ingot")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.button_back"
      },
    ],
  })
  actionForm.show(player).then(e => {
    if (e.canceled) return
    else if (e.selection == 0) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.platinum_rod"
        },
      ],
    }, NEW_ITEM_DESCRIPTION.platinum_rod, openNewItems)
    else if (e.selection == 1) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.quartz_stick"
        },
      ],
    }, NEW_ITEM_DESCRIPTION.quartz_stick, openNewItems)
    else if (e.selection == 2) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.steel_stick"
        },
      ],
    }, NEW_ITEM_DESCRIPTION.steel_rod, openNewItems)
    else if (e.selection == 3) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.upgradium"
        },
      ],
    }, NEW_ITEM_DESCRIPTION.upgradium, openNewItems)
    else openGuidebook(player)
  })
}

function openNewWeapons(player) {
  const actionForm = new ActionFormData()
  actionForm.title({
    rawtext: [
      {
        translate: "4ks_wp.weapon_button"
      },
    ],
  })
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.battleaxe"
      },
    ],
  }, "textures/4ks/wp/items/diamond_battleaxe")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.bow"
      },
    ],
  }, "textures/4ks/wp/items/diamond_bow_standby")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.cactus_sword"
      },
    ],
  }, "textures/4ks/wp/items/cactus_sword")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.club"
      },
    ],
  }, "textures/4ks/wp/items/diamond_club")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.crossbow"
      },
    ],
  }, "textures/4ks/wp/crossbow/diamond/crossbow_standby")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.cutlass"
      },
    ],
  }, "textures/4ks/wp/items/diamond_cutlass")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.dagger"
      },
    ],
  }, "textures/4ks/wp/items/diamond_dagger")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.dirt_sword"
      },
    ],
  }, "textures/4ks/wp/items/dirt_sword")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.gladius"
      },
    ],
  }, "textures/4ks/wp/items/diamond_gladius")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.greatsword"
      },
    ],
  }, "textures/4ks/wp/items/diamond_greatsword")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.halberd"
      },
    ],
  }, "textures/4ks/wp/items/diamond_halberd")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.javelin"
      },
    ],
  }, "textures/4ks/wp/items/diamond_javelin")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.khopesh"
      },
    ],
  }, "textures/4ks/wp/items/diamond_khopesh")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.long_bow"
      },
    ],
  }, "textures/4ks/wp/long_bow/diamond/bow_standby")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.mace"
      },
    ],
  }, "textures/4ks/wp/items/diamond_mace")


  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.shuriken"
      },
    ],
  }, "textures/4ks/wp/items/stars/diamond_star")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.spear"
      },
    ],
  }, "textures/4ks/wp/items/diamond_spear")


  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.trident"
      },
    ],
  }, "textures/4ks/wp/items/diamond_trident")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.v2_bows"
      },
    ],
  }, "textures/4ks/wp/v2bow/divine/bow_standby")

  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.v2_swords"
      },
    ],
  }, "textures/4ks/wp/items/demonic_sword_special")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.guidebook.button_back"
      },
    ],
  })
  actionForm.show(player).then(e => {
    if (e.canceled) return
    else if (e.selection == 0) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.battleaxe"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.battleaxe, openNewWeapons)

    else if (e.selection == 1) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.bow"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.bow, openNewWeapons)

    else if (e.selection == 2) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.cactus_sword"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.cactus_sword, openNewWeapons)

    else if (e.selection == 3) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.club"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.club, openNewWeapons)

    else if (e.selection == 4) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.crossbow"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.crossbow, openNewWeapons)

    else if (e.selection == 5) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.cutlass"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.cutlass, openNewWeapons)

    else if (e.selection == 6) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.dagger"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.dagger, openNewWeapons)

    else if (e.selection == 7) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.dirt_sword"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.dirt_sword, openNewWeapons)

    else if (e.selection == 8) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.gladius"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.gladius, openNewWeapons)

    else if (e.selection == 9) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.greatsword"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.greatsword, openNewWeapons)

    else if (e.selection == 10) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.halberd"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.halberd, openNewWeapons)

    else if (e.selection == 11) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.javelin"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.javelin, openNewWeapons)

    else if (e.selection == 12) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.khopesh"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.khopesh, openNewWeapons)

    else if (e.selection == 13) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.long_bow"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.long_bow, openNewWeapons)

    else if (e.selection == 14) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.mace"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.Mace, openNewWeapons)

    else if (e.selection == 15) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.shuriken"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.shuriken, openNewWeapons)

    else if (e.selection == 16) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.spear"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.spear, openNewWeapons)

    else if (e.selection == 17) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.trident"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.trident, openNewWeapons)

    else if (e.selection == 18) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.v2_bows"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.v2_bows, openNewWeapons)

    else if (e.selection == 19) openInfo(player, {
      rawtext: [
        {
          translate: "4ks_wp.guidebook.v2_swords"
        },
      ],
    }, NEW_WEAPON_DESCRIPTION.ultra_swords, openNewWeapons)
    else openGuidebook(player)
  })
}

function openWhatsNew(player) {
  openInfo(player, {
    rawtext: [
      {
        translate: "4ks_wp.whats_new"
      },
    ],
  },NEW_WEAPON_DESCRIPTION.whats_new_description , openGuidebook)
}

export const openGuidebook = (player) => {
  const actionForm = new ActionFormData()
  actionForm.title({
    rawtext: [
      {
        translate: "4ks_wp.guidebook_title"
      },
    ],
  })
  actionForm.body(MAIN_MENU_DESCRIPTION)
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.gear_button"
      },
    ],
  }, "textures/items/diamond_chestplate")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.material_button"
      },
    ],
  }, "textures/items/diamond")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.item_button"
      },
    ],
  }, "textures/items/stick")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.weapon_button"
      },
    ],
  }, "textures/items/diamond_sword")


  actionForm.button({
    rawtext: [
      {
        translate: "4ks_wp.whats_new"
      },
    ],
  }, "textures/4ks/wp/items/star")
  actionForm.show(player).then((e) => {
    if (e.canceled) return
    if (e.selection == 0) openGearTiers(player)
    else if (e.selection == 1) openNewMaterials(player)
    else if (e.selection == 2) openNewItems(player)
    else if (e.selection == 3) openNewWeapons(player)
    else if (e.selection == 4) openWhatsNew(player)
  })
}