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

const openSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Which type of vehicle would you like a schematic for?")
    .button(
      "Planes",
      "textures/PixelBiester/Airplane/form_icons/sch_options/planes"
    )
    .button(
      "Helicopters",
      "textures/PixelBiester/Airplane/form_icons/sch_options/helicopters"
    )
    .button(
      "Drones",
      "textures/PixelBiester/Airplane/form_icons/sch_options/drones"
    )
    .button(
      "Blimps",
      "textures/PixelBiester/Airplane/form_icons/sch_options/blimps"
    )
    .button(
      "Cars",
      "textures/PixelBiester/Airplane/form_icons/sch_options/cars"
    )
    .button(
      "Boats",
      "textures/PixelBiester/Airplane/form_icons/sch_options/boats"
    )
    .button("Exit", "textures/PixelBiester/Airplane/form_icons/exit_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        planeSchematicForm(player);
        break;
      case 1:
        helicopterSchematicForm(player);
        break;
      case 2:
        droneSchematicForm(player);
        break;
      case 3:
        blimpSchematicForm(player);
        break;
      case 4:
        carSchematicForm(player);
        break;
      case 5:
        boatSchematicForm(player);
        break;
      case 6:
        break;
    }
  });
};

const planeSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Choose a schematic you would like to create:")
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon")
    .button(
      "Airtrike",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/airtrike"
    )
    .button(
      "Air Tractor",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/air_tractor"
    )
    .button(
      "Biplane",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/biplane"
    )
    .button(
      "Skyhawk",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/skyhawk"
    )
    .button(
      "Cadanair",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/cadanair"
    )
    .button(
      "Cruiser",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/cruiser"
    )
    .button(
      "Ember 120",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/ember_120"
    )
    .button(
      "Magister",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/magister"
    )
    .button(
      "Gilbert",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/gilbert"
    )
    .button(
      "Glider",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/glider"
    )
    .button(
      "Galapagos",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/galapagos"
    )
    .button(
      "Northstar",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/northstar"
    )
    .button(
      "Wooden Plane",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/wooden"
    )
    .button(
      "Lightning",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/lightning"
    )
    .button(
      "Passenger Jet",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/passenger_jet"
    )
    .button(
      "Ricarde",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/ricarde"
    )
    .button(
      "Seaplane",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/seaplane"
    )
    .button(
      "Marine",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/marine"
    )
    .button(
      "Triplane",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/triplane"
    )
    .button(
      "Vampire",
      "textures/PixelBiester/Airplane/form_icons/sch_planes/vampire"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        openSchematicForm(player);
        break;
      case 1:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_airtrike");
        break;
      case 2:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_air_tractor");
        break;
      case 3:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_biplane");
        break;
      case 4:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_skyhawk");
        break;
      case 5:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_cadanair");
        break;
      case 6:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_cruiser");
        break;
      case 7:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_ember_120");
        break;
      case 8:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_magister");
        break;
      case 9:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_gilbert");
        break;
      case 10:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_glider");
        break;
      case 11:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_galapagos");
        break;
      case 12:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_northstar");
        break;
      case 13:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_wooden");
        break;
      case 14:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_lightning");
        break;
      case 15:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_passenger_jet");
        break;
      case 16:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_ricarde");
        break;
      case 17:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_seaplane");
        break;
      case 18:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_marine");
        break;
      case 19:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_triplane");
        break;
      case 20:
        giveSchematicToPlayer(player, "pb_plane:sch_plane_vampire");
        break;
      case 21:
        openSchematicForm(player);
        break;
    }
  });
};

const droneSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Choose a schematic you would like to create:")
    .button(
      "Passenger Drone",
      "textures/PixelBiester/Airplane/form_icons/sch_other/passenger"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        giveSchematicToPlayer(player, "pb_plane:sch_drone_passenger");
        break;
      case 1:
        openSchematicForm(player);
        break;
    }
  });
};

const blimpSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Choose a schematic you would like to create:")
    .button(
      "Zeppelin Blimp",
      "textures/PixelBiester/Airplane/form_icons/sch_other/zeppelin"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        giveSchematicToPlayer(player, "pb_plane:sch_blimp_zeppelin");
        break;
      case 1:
        openSchematicForm(player);
        break;
    }
  });
};

const helicopterSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Choose a schematic you would like to create:")
    .button(
      "Cargo",
      "textures/PixelBiester/Airplane/form_icons/sch_helicopters/cargo"
    )
    .button(
      "Leopard",
      "textures/PixelBiester/Airplane/form_icons/sch_helicopters/leopard"
    )
    .button(
      "Remedy",
      "textures/PixelBiester/Airplane/form_icons/sch_helicopters/remedy"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        giveSchematicToPlayer(player, "pb_plane:sch_helicopter_cargo");
        break;
      case 1:
        giveSchematicToPlayer(player, "pb_plane:sch_helicopter_leopard");
        break;
      case 2:
        giveSchematicToPlayer(player, "pb_plane:sch_helicopter_remedy");
        break;
      case 3:
        openSchematicForm(player);
        break;
    }
  });
};

const carSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Choose a schematic you would like to create:")
    .button(
      "Sports Car",
      "textures/PixelBiester/Airplane/form_icons/sch_cars/sports"
    )
    .button(
      "Police Car",
      "textures/PixelBiester/Airplane/form_icons/sch_cars/police"
    )
    .button(
      "Fire Truck",
      "textures/PixelBiester/Airplane/form_icons/sch_cars/fire_truck"
    )
    .button(
      "Large Fire Truck",
      "textures/PixelBiester/Airplane/form_icons/sch_cars/fire_truck_large"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        giveSchematicToPlayer(player, "pb_plane:sch_car_sports");
        break;
      case 1:
        giveSchematicToPlayer(player, "pb_plane:sch_car_police");
        break;
      case 2:
        giveSchematicToPlayer(player, "pb_plane:sch_car_fire_truck");
        break;
      case 3:
        giveSchematicToPlayer(player, "pb_plane:sch_car_fire_truck_large");
        break;
      case 4:
        openSchematicForm(player);
        break;
    }
  });
};

const boatSchematicForm = (player) => {
  const form = new ActionFormData()
    .title("Schematic Designer")
    .body("Choose a schematic you would like to create:")
    .button(
      "Speed Boat",
      "textures/PixelBiester/Airplane/form_icons/sch_boats/speed"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  // Show the form to the player and handle the response
  form.show(player).then((response) => {
    switch (response.selection) {
      case 0:
        giveSchematicToPlayer(player, "pb_plane:sch_speed_boat");
        break;
      case 1:
        openSchematicForm(player);
        break;
    }
  });
};

// Helper function to give an item to the player with custom messages
const giveSchematicToPlayer = (player, itemId) => {
  const item = new ItemStack(itemId, 1);
  player.getComponent("inventory").container.addItem(item);
  let message;
  // PLANES
  if (itemId === "pb_plane:sch_plane_air_tractor") {
    message = "You received an Air Tractor schematic";
  } else if (itemId === "pb_plane:sch_plane_airtrike") {
    message = "You received an Airtrike schematic";
  } else if (itemId === "pb_plane:sch_plane_biplane") {
    message = "You received a Biplane schematic";
  } else if (itemId === "pb_plane:sch_plane_skyhawk") {
    message = "You received a Skyhawk schematic";
  } else if (itemId === "pb_plane:sch_plane_cadanair") {
    message = "You received a Cadanair schematic";
  } else if (itemId === "pb_plane:sch_plane_cruiser") {
    message = "You received a Cruiser schematic";
  } else if (itemId === "pb_plane:sch_plane_ember_120") {
    message = "You received an Ember 120 schematic";
  } else if (itemId === "pb_plane:sch_plane_magister") {
    message = "You received a Magister schematic";
  } else if (itemId === "pb_plane:sch_plane_gilbert") {
    message = "You received a Gilbert schematic";
  } else if (itemId === "pb_plane:sch_plane_glider") {
    message = "You received a Glider schematic";
  } else if (itemId === "pb_plane:sch_plane_galapagos") {
    message = "You received a Galapagos schematic";
  } else if (itemId === "pb_plane:sch_plane_lockheed_12a") {
    message = "You received a Lockheed 12A schematic";
  } else if (itemId === "pb_plane:sch_plane_northstar") {
    message = "You received a Northstar schematic";
  } else if (itemId === "pb_plane:sch_plane_wooden") {
    message = "You received an Wooden Plane schematic";
  } else if (itemId === "pb_plane:sch_plane_lightning") {
    message = "You received a Lightning schematic";
  } else if (itemId === "pb_plane:sch_plane_passenger_jet") {
    message = "You received a Passenger Jet schematic";
  } else if (itemId === "pb_plane:sch_plane_ricarde") {
    message = "You received a Ricarde schematic";
  } else if (itemId === "pb_plane:sch_plane_seaplane") {
    message = "You received a Seaplane schematic";
  } else if (itemId === "pb_plane:sch_plane_marine") {
    message = "You received a Marine schematic";
  } else if (itemId === "pb_plane:sch_plane_triplane") {
    message = "You received a Triplane schematic";
  } else if (itemId === "pb_plane:sch_plane_vampire") {
    message = "You received a Vampire schematic";
  }
  // HELICOPTERS
  else if (itemId === "pb_plane:sch_helicopter_cargo") {
    message = "You received a Cargo Helicopter schematic";
  } else if (itemId === "pb_plane:sch_helicopter_leopard") {
    message = "You received a Leopard Helicopter schematic";
  } else if (itemId === "pb_plane:sch_helicopter_remedy") {
    message = "You received a Remedy Helicopter schematic";
  }
  // BLIMP AND DRONE
  else if (itemId === "pb_plane:sch_blimp_zeppelin") {
    message = "You received a Zeppelin schematic";
  } else if (itemId === "pb_plane:sch_drone_passenger") {
    message = "You received a Drone schematic";
  }
  // BOAT
  else if (itemId === "pb_plane:sch_speed_boat") {
    message = "You received a Speed Boat schematic";
  }
  // CARS
  else if (itemId === "pb_plane:sch_car_sports") {
    message = "You received a Sports Car schematic";
  } else if (itemId === "pb_plane:sch_car_police") {
    message = "You received a Police Car schematic";
  } else if (itemId === "pb_plane:sch_car_fire_truck") {
    message = "You received a Fire Truck schematic";
  } else if (itemId === "pb_plane:sch_car_fire_truck_large") {
    message = "You received a Large Fire Truck schematic";
  }

  player.onScreenDisplay.setActionBar(message);
};

export { openSchematicForm };
