import { world, system } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

//Uso do item de custom
world.afterEvents.itemUse.subscribe(function (use) {
  const item = use.itemStack.typeId
  if (item === "yokai:menu_c") {
    const player = use.source
    Pele(player)
  }
})

//Menu pele
function Pele(player) {
  player.runCommand("tp @s ~~~ ~ 0")
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^0.5^2.3^2.5 facing @s")
  player.addTag("ui");
  const pele = new ActionFormData()
    .title("custom")
    .button("custom", "textures/ui/menu/custom/pele/w")
    .button("custom", "textures/ui/menu/custom/pele/d")
    .button("custom", "textures/ui/menu/custom/pele/b")
    .button("")
    .button("")
    .button("")
  pele.show(player).then(escolha => {
    if (escolha.canceled) {
      Pele(player)
    }
    if (escolha.selection === 0) {
      player.triggerEvent("pele_w")
      Pele(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("pele_d")
      Pele(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("pele_b")
      Pele(player)
    }
    if (escolha.selection === 5) {
      Olhos(player)
    }
  })
}

//Menu Olhos
function Olhos(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^ ^1.5 ^1.5 rot ~ ~180")
  const olhos = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/olhos/preto")
    .button("custom", "textures/ui/menu/custom/olhos/verde")
    .button("custom", "textures/ui/menu/custom/olhos/azul")
    .button("custom", "textures/ui/menu/custom/olhos/amarelo")
    .button("")
    .button("")
  olhos.show(player).then(escolha => {
    if (escolha.canceled) return Olhos(player)
    if (escolha.selection === 0) {
      player.triggerEvent("o_preto")
      Olhos(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("o_verde")
      Olhos(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("o_azul")
      Olhos(player)
    }
    if (escolha.selection === 3) {
      player.triggerEvent("o_amarelo")
      Olhos(player)
    }
    if (escolha.selection === 4) return Pele(player)
    if (escolha.selection === 5) Expre(player)
  })
}


//Menu Expressões 
function Expre(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^ ^1.5 ^1.5 rot ~ ~180")
  const expre = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/expre/expre1")
    .button("custom", "textures/ui/menu/custom/expre/expre2")
    .button("custom", "textures/ui/menu/custom/expre/expre3")
    .button("")
    .button("")
    .button("")
  expre.show(player).then(escolha => {
    if (escolha.canceled) return Expre(player)
    if (escolha.selection === 0) {
      player.triggerEvent("expre1")
      Expre(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("expre2")
      Expre(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("expre3")
      Expre(player)
    }
    if (escolha.selection === 4) return Olhos(player)
    if (escolha.selection === 5) Sombra(player)
  })
}

//Menu Sombrancelha 
function Sombra(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^ ^1.5 ^1.5 rot ~ ~180")
  const sombra = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/sombra/sombra1")
    .button("custom", "textures/ui/menu/custom/sombra/sombra2")
    .button("custom", "textures/ui/menu/custom/sombra/sombra3")
    .button("")
    .button("")
    .button("")
  sombra.show(player).then(escolha => {
    if (escolha.canceled) return Sombra(player)
    if (escolha.selection === 0) {
      player.triggerEvent("sombra1")
      Sombra(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("sombra2")
      Sombra(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("sombra3")
      Sombra(player)
    }
    if (escolha.selection === 4) return Expre(player)
    if (escolha.selection === 5) Scar(player)
  })
}

//Menu Scar
function Scar(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^ ^1.5 ^1.5 rot ~ ~180")
  const scar = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/scar/null")
    .button("custom", "textures/ui/menu/custom/scar/scar1")
    .button("custom", "textures/ui/menu/custom/scar/scar2")
    .button("custom", "textures/ui/menu/custom/scar/scar3")
    .button("")
    .button("")
  scar.show(player).then(escolha => {
    if (escolha.canceled) return Scar(player)
    if (escolha.selection === 0) {
      player.triggerEvent("scar_null")
      Scar(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("scar1")
     Scar(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("scar2")
      Scar(player)
    }
    if (escolha.selection === 3) {
      player.triggerEvent("scar3")
      Scar(player)
    }
    if (escolha.selection === 4) return Sombra(player)
    if (escolha.selection === 5) Mouth(player)
  })
}

//Menu Sorrisos
function Mouth(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^ ^1.5 ^1.5 rot ~ ~180")
  const mouth = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/mouth/mouth1")
    .button("custom", "textures/ui/menu/custom/mouth/mouth2")
    .button("custom", "textures/ui/menu/custom/mouth/mouth3")
    .button("")
    .button("")
    .button("")
  mouth.show(player).then(escolha => {
    if (escolha.canceled) return Mouth(player)
    if (escolha.selection === 0) {
      player.triggerEvent("mouth1")
      Mouth(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("mouth2")
      Mouth(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("mouth3")
      Mouth(player)
    }
    if (escolha.selection === 4) return Scar(player)
    if (escolha.selection === 5) Hair(player)
  })
}

//Menu Hair
function Hair(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^-0.9^2.2^1 rot ~20 ~220")
  const hair = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/hairs/none")
    .button("custom", "textures/ui/menu/custom/hairs/yuno")
    .button("custom", "textures/ui/menu/custom/hairs/asta")
    .button("custom", "textures/ui/menu/custom/hairs/magna")
    .button("")
    .button("")
  hair.show(player).then(escolha => {
    if (escolha.canceled) return Hair(player)
    if (escolha.selection === 0) {
      player.triggerEvent("no_hair")
      Hair(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("hair_yuno")
      Hair(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("hair_asta")
      Hair(player)
    }
    if (escolha.selection === 3) {
      player.triggerEvent("hair_magna")
      Hair(player)
    }
    if (escolha.selection === 4) Mouth(player)
    if (escolha.selection === 5) HairColor(player)
  })
}


//Menu Hair Color
function HairColor(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^-0.9^2.2^1 rot ~20 ~220")
  const hairColor = new ActionFormData()
    .title("customM")
    .button("custom", "textures/ui/menu/custom/hair_colors/b")
    .button("custom", "textures/ui/menu/custom/hair_colors/y")
    .button("custom", "textures/ui/menu/custom/hair_colors/w")
    .button("custom", "textures/ui/menu/custom/hair_colors/g")
    .button("")
    .button("")
  hairColor.show(player).then(escolha => {
    if (escolha.canceled) return HairColor(player)
    if (escolha.selection === 0) {
      player.triggerEvent("hair_b")
      HairColor(player)
    }
    if (escolha.selection === 1) {
      player.triggerEvent("hair_y")
      HairColor(player)
    }
    if (escolha.selection === 2) {
      player.triggerEvent("hair_w")
      HairColor(player)
    }
    if (escolha.selection === 3) {
      player.triggerEvent("hair_g")
      HairColor(player)
    }
    if (escolha.selection === 4) Hair(player)
    if (escolha.selection === 5) Confirm(player)
  })
}


//Menu Confirm
function Confirm(player) {
  player.runCommand("camera @s set minecraft:free ease 0.3 linear pos ^0.5^2.3^2.5 facing @s")
  const confirm = new ActionFormData()
    .title("confirm")
    .button("")
    .button("")
  confirm.show(player).then(escolha => {
    if (escolha.canceled) return Confirm(player)
    if (escolha.selection === 0) {
      HairColor(player)
    }
    if (escolha.selection === 1) {
      player.runCommand("camera @s clear")
      const inventory = player.getComponent("inventory").container
      inventory.setItem(0, undefined)
      player.removeTag("ui")
      player.addTag("customF")
    }
  })
}