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

// Event listener for ScriptEventCommand
system.afterEvents.scriptEventReceive.subscribe((eventData) => {
  const { id, sourceEntity } = eventData;

  // Ensure that the source and dimension are defined
  if (id === "pb_plane:color_options_1") {
    // Check if a player was found within the specified distance
    const playersNearby = sourceEntity.dimension.getPlayers({
      maxDistance: 16,
      location: sourceEntity.location,
      closest: 1,
    });
    const player = playersNearby[0];
    opencolorOptions1(player, sourceEntity);
  }
  if (id === "pb_plane:color_options_2") {
    // Check if a player was found within the specified distance
    const playersNearby = sourceEntity.dimension.getPlayers({
      maxDistance: 16,
      location: sourceEntity.location,
      closest: 1,
    });
    const player = playersNearby[0];
    opencolorOptions2(player, sourceEntity);
  }
});

function hexToRgb(hex) {
  // Remove the '#' if it exists
  hex = hex.replace(/^#/, "");

  // Check if it's a valid hex code (3 or 6 characters)
  if (hex.length === 3) {
    // Expand shorthand hex (e.g., #abc -> #aabbcc)
    hex = hex
      .split("")
      .map((char) => char + char)
      .join("");
  }

  if (hex.length !== 6) {
    return null; // Invalid hex code
  }

  // Parse the hex string into integer RGB values
  const bigint = parseInt(hex, 16);
  const r = (bigint >> 16) & 255;
  const g = (bigint >> 8) & 255;
  const b = bigint & 255;

  return { r, g, b }; // Return an object with the RGB values
}

/// FORMS FOR 1 color TYPE
function opencolorOptions1(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Color Options")
    .body(
      "Here you can customise the look of your vehicle, please select an option below to begin:"
    )
    .button(
      "Basic Colors",
      "textures/PixelBiester/Airplane/form_icons/coloriser/basic"
    )
    .button(
      "Complex Colors",
      "textures/PixelBiester/Airplane/form_icons/coloriser/complex"
    )
    .button(
      "Pattern Style",
      "textures/PixelBiester/Airplane/form_icons/coloriser/style"
    )
    .button(
      "Remove Colors",
      "textures/PixelBiester/Airplane/form_icons/coloriser/remove"
    )
    .button("Exit", "textures/PixelBiester/Airplane/form_icons/exit_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        openOptions1Basic(player, sourceEntity);
        break;
      case 1:
        openOptions1ComplexMenu(player, sourceEntity);
        break;
      case 2:
        openOptions1StyleMenu(player, sourceEntity);
        break;
      case 3:
        openOptions1Remove(player, sourceEntity);
        break;
      case 4:
        break;
    }
  });
}
/// Basic color picker
function openOptions1Basic(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Basic Color Chooser")
    .body("Select a preset color from below to apply it to your vehicle:")
    .button("Red", "textures/PixelBiester/Airplane/form_icons/coloriser/red")
    .button(
      "Orange",
      "textures/PixelBiester/Airplane/form_icons/coloriser/orange"
    )
    .button(
      "Yellow",
      "textures/PixelBiester/Airplane/form_icons/coloriser/yellow"
    )
    .button("Lime", "textures/PixelBiester/Airplane/form_icons/coloriser/lime")
    .button(
      "Green",
      "textures/PixelBiester/Airplane/form_icons/coloriser/green"
    )
    .button("Cyan", "textures/PixelBiester/Airplane/form_icons/coloriser/cyan")
    .button(
      "Light Blue",
      "textures/PixelBiester/Airplane/form_icons/coloriser/light_blue"
    )
    .button("Blue", "textures/PixelBiester/Airplane/form_icons/coloriser/blue")
    .button(
      "Purple",
      "textures/PixelBiester/Airplane/form_icons/coloriser/purple"
    )
    .button(
      "Magenta",
      "textures/PixelBiester/Airplane/form_icons/coloriser/magenta"
    )
    .button("Pink", "textures/PixelBiester/Airplane/form_icons/coloriser/pink")
    .button(
      "Brown",
      "textures/PixelBiester/Airplane/form_icons/coloriser/brown"
    )
    .button(
      "Black",
      "textures/PixelBiester/Airplane/form_icons/coloriser/black"
    )
    .button("Gray", "textures/PixelBiester/Airplane/form_icons/coloriser/gray")
    .button(
      "Light Gray",
      "textures/PixelBiester/Airplane/form_icons/coloriser/light_gray"
    )
    .button(
      "White",
      "textures/PixelBiester/Airplane/form_icons/coloriser/white"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      // Red
      case 0:
        sourceEntity.setProperty("pb_plane:red_1", 0.7);
        sourceEntity.setProperty("pb_plane:green_1", 0.2);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Orange
      case 1:
        sourceEntity.setProperty("pb_plane:red_1", 1);
        sourceEntity.setProperty("pb_plane:green_1", 0.5);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Yellow
      case 2:
        sourceEntity.setProperty("pb_plane:red_1", 1);
        sourceEntity.setProperty("pb_plane:green_1", 0.8);
        sourceEntity.setProperty("pb_plane:blue_1", 0.2);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Lime
      case 3:
        sourceEntity.setProperty("pb_plane:red_1", 0.5);
        sourceEntity.setProperty("pb_plane:green_1", 0.8);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Green
      case 4:
        sourceEntity.setProperty("pb_plane:red_1", 0.4);
        sourceEntity.setProperty("pb_plane:green_1", 0.5);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Cyan
      case 5:
        sourceEntity.setProperty("pb_plane:red_1", 0.1);
        sourceEntity.setProperty("pb_plane:green_1", 0.6);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Light Blue
      case 6:
        sourceEntity.setProperty("pb_plane:red_1", 0.2);
        sourceEntity.setProperty("pb_plane:green_1", 0.7);
        sourceEntity.setProperty("pb_plane:blue_1", 0.9);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Blue
      case 7:
        sourceEntity.setProperty("pb_plane:red_1", 0.2);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Purple
      case 8:
        sourceEntity.setProperty("pb_plane:red_1", 0.5);
        sourceEntity.setProperty("pb_plane:green_1", 0.2);
        sourceEntity.setProperty("pb_plane:blue_1", 0.7);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Magenta
      case 9:
        sourceEntity.setProperty("pb_plane:red_1", 0.8);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.7);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Pink
      case 10:
        sourceEntity.setProperty("pb_plane:red_1", 1);
        sourceEntity.setProperty("pb_plane:green_1", 0.5);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Brown
      case 11:
        sourceEntity.setProperty("pb_plane:red_1", 0.5);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.2);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Black
      case 12:
        sourceEntity.setProperty("pb_plane:red_1", 0.1);
        sourceEntity.setProperty("pb_plane:green_1", 0.1);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Gray
      case 13:
        sourceEntity.setProperty("pb_plane:red_1", 0.3);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.3);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Light Gray
      case 14:
        sourceEntity.setProperty("pb_plane:red_1", 0.6);
        sourceEntity.setProperty("pb_plane:green_1", 0.6);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // White
      case 15:
        sourceEntity.setProperty("pb_plane:red_1", 0.9);
        sourceEntity.setProperty("pb_plane:green_1", 0.9);
        sourceEntity.setProperty("pb_plane:blue_1", 0.9);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      case 16:
        opencolorOptions1(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Complex color picker
function openOptions1ComplexMenu(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Complex Color Chooser")
    .body(
      "Here you can define your chosen colour by using RGB sliders, or even type in a HEX value"
    )
    .button(
      "Open Colour Chooser",
      "textures/PixelBiester/Airplane/form_icons/coloriser/complex"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        openOptions1Complex(player, sourceEntity);
        break;
      case 1:
        opencolorOptions1(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Complex color picker
function openOptions1Complex(player, sourceEntity) {
  // Convert RGB float properties (0.0 to 1.0) to integers (0 to 255)
  const currentRed = Math.floor(
    sourceEntity.getProperty("pb_plane:red_1") * 255
  );
  const currentGreen = Math.floor(
    sourceEntity.getProperty("pb_plane:green_1") * 255
  );
  const currentBlue = Math.floor(
    sourceEntity.getProperty("pb_plane:blue_1") * 255
  );
  const currentAlpha = Math.floor(
    sourceEntity.getProperty("pb_plane:alpha_1") * 10
  );

  const form = new ModalFormData()
    .title("Complex Color Chooser")
    .slider("Red", 0, 255, 1, currentRed) // Sliders for RGB
    .slider("Green", 0, 255, 1, currentGreen)
    .slider("Blue", 0, 255, 1, currentBlue)
    .slider("Alpha", 0, 8, 1, currentAlpha) // Slider for Alpha (0.0 to 1.0 * 10)
    .textField("Hex Color", "#FFFFFF");

  form
    .show(player)
    .then((response) => {
      if (response.canceled) {
        return;
      } else {
        sourceEntity.setProperty("pb_plane:colored_1", true);
        // Get the new values from the sliders
        const newRed = (response.formValues[0] / 255).toFixed(1); // Convert back to float and round to 1 decimal place
        const newGreen = (response.formValues[1] / 255).toFixed(1);
        const newBlue = (response.formValues[2] / 255).toFixed(1);
        const newAlpha = (response.formValues[3] / 10).toFixed(1);

        // Get the hex value from the text field
        const hexColor = response.formValues[4];
        const rgbFromHex = hexToRgb(hexColor);

        // Set properties based on the slider values
        sourceEntity.setProperty("pb_plane:red_1", parseFloat(newRed));
        sourceEntity.setProperty("pb_plane:green_1", parseFloat(newGreen));
        sourceEntity.setProperty("pb_plane:blue_1", parseFloat(newBlue));
        sourceEntity.setProperty("pb_plane:alpha_1", parseFloat(newAlpha));

        // If a valid hex color is provided, update RGB values accordingly
        if (rgbFromHex) {
          player.sendMessage(
            `Hex: ${hexColor} converted to: Red = ${rgbFromHex.r}, Green = ${rgbFromHex.g}, Blue = ${rgbFromHex.b}`
          );
          // Optionally update the properties with RGB from hex as well
          sourceEntity.setProperty("pb_plane:red_1", rgbFromHex.r / 255);
          sourceEntity.setProperty("pb_plane:green_1", rgbFromHex.g / 255);
          sourceEntity.setProperty("pb_plane:blue_1", rgbFromHex.b / 255);
        } else {
          player.sendMessage("Invalid hex color provided.");
        }
      }
    })
    .catch((error) => {
      player.sendMessage(`An error occurred: ${error}`);
    });
}

/// Complex color picker
function openOptions1StyleMenu(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Complex Color Chooser")
    .body("Here you can choose the style of your vehicle")
    .button(
      "Open Style Chooser",
      "textures/PixelBiester/Airplane/form_icons/coloriser/style"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        openOptions1Style(player, sourceEntity);
        break;
      case 1:
        opencolorOptions1(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Style selector
function openOptions1Style(player, sourceEntity) {
  // Convert RGB float properties (0.0 to 1.0) to integers (0 to 255)
  const currentStyle = sourceEntity.getProperty("pb_plane:pattern") + 1;

  const form = new ModalFormData()
    .title("Style Options")
    .slider("Style Type", 1, 3, 1, currentStyle);

  form
    .show(player)
    .then((response) => {
      if (response.canceled) {
        return;
      } else {
        // Set properties based on the slider values
        sourceEntity.setProperty(
          "pb_plane:pattern",
          response.formValues[0] - 1
        );
      }
    })
    .catch((error) => {
      player.sendMessage(`An error occurred: ${error}`);
    });
}
/// Remove coloring and revert to original design
function openOptions1Remove(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Remove Colors")
    .body("Would you like to remove your custom colors from this vehicle?")
    .button("Confirm", "textures/PixelBiester/Airplane/form_icons/confirm_icon")
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        sourceEntity.setProperty("pb_plane:colored_1", false);
        break;
      case 1:
        opencolorOptions1(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}

/// FORMS FOR 2 color TYPE
function opencolorOptions2(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Color Options")
    .body(
      "Here you can customise the look of your vehicle, please select an option below to begin:"
    )
    .button(
      "Basic Colors",
      "textures/PixelBiester/Airplane/form_icons/coloriser/basic"
    )
    .button(
      "Complex Colors",
      "textures/PixelBiester/Airplane/form_icons/coloriser/complex"
    )
    .button(
      "Pattern Style",
      "textures/PixelBiester/Airplane/form_icons/coloriser/style"
    )
    .button(
      "Remove Colors",
      "textures/PixelBiester/Airplane/form_icons/coloriser/remove"
    )
    .button("Exit", "textures/PixelBiester/Airplane/form_icons/exit_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        openOptions2Basic(player, sourceEntity);
        break;
      case 1:
        openOptions2Complex(player, sourceEntity);
        break;
      case 2:
        openOptions2StyleMenu(player, sourceEntity);
        break;
      case 3:
        openOptions2Remove(player, sourceEntity);
        break;
      case 4:
        break;
    }
  });
}

/// Basic color picker - select layer
function openOptions2Basic(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Basic Color Chooser")
    .body("Select a layer to apply a preset color to your vehicle:")
    .button(
      "Layer 1",
      "textures/PixelBiester/Airplane/form_icons/coloriser/basic1"
    )
    .button(
      "Layer 2",
      "textures/PixelBiester/Airplane/form_icons/coloriser/basic2"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      // color 1
      case 0:
        openOptions2Basic1(player, sourceEntity);
        break;
      // color 2
      case 1:
        openOptions2Basic2(player, sourceEntity);
        break;
      // Exit
      case 2:
        opencolorOptions2(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Basic color picker - MAIN color
function openOptions2Basic1(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Basic Color Chooser - Layer 1")
    .body("Select a preset color from below to apply it to your vehicle:")
    .button("Red", "textures/PixelBiester/Airplane/form_icons/coloriser/red")
    .button(
      "Orange",
      "textures/PixelBiester/Airplane/form_icons/coloriser/orange"
    )
    .button(
      "Yellow",
      "textures/PixelBiester/Airplane/form_icons/coloriser/yellow"
    )
    .button("Lime", "textures/PixelBiester/Airplane/form_icons/coloriser/lime")
    .button(
      "Green",
      "textures/PixelBiester/Airplane/form_icons/coloriser/green"
    )
    .button("Cyan", "textures/PixelBiester/Airplane/form_icons/coloriser/cyan")
    .button(
      "Light Blue",
      "textures/PixelBiester/Airplane/form_icons/coloriser/light_blue"
    )
    .button("Blue", "textures/PixelBiester/Airplane/form_icons/coloriser/blue")
    .button(
      "Purple",
      "textures/PixelBiester/Airplane/form_icons/coloriser/purple"
    )
    .button(
      "Magenta",
      "textures/PixelBiester/Airplane/form_icons/coloriser/magenta"
    )
    .button("Pink", "textures/PixelBiester/Airplane/form_icons/coloriser/pink")
    .button(
      "Brown",
      "textures/PixelBiester/Airplane/form_icons/coloriser/brown"
    )
    .button(
      "Black",
      "textures/PixelBiester/Airplane/form_icons/coloriser/black"
    )
    .button("Gray", "textures/PixelBiester/Airplane/form_icons/coloriser/gray")
    .button(
      "Light Gray",
      "textures/PixelBiester/Airplane/form_icons/coloriser/light_gray"
    )
    .button(
      "White",
      "textures/PixelBiester/Airplane/form_icons/coloriser/white"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      // Red
      case 0:
        sourceEntity.setProperty("pb_plane:red_1", 0.7);
        sourceEntity.setProperty("pb_plane:green_1", 0.2);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Orange
      case 1:
        sourceEntity.setProperty("pb_plane:red_1", 1);
        sourceEntity.setProperty("pb_plane:green_1", 0.5);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Yellow
      case 2:
        sourceEntity.setProperty("pb_plane:red_1", 1);
        sourceEntity.setProperty("pb_plane:green_1", 0.8);
        sourceEntity.setProperty("pb_plane:blue_1", 0.2);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Lime
      case 3:
        sourceEntity.setProperty("pb_plane:red_1", 0.5);
        sourceEntity.setProperty("pb_plane:green_1", 0.8);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Green
      case 4:
        sourceEntity.setProperty("pb_plane:red_1", 0.4);
        sourceEntity.setProperty("pb_plane:green_1", 0.5);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Cyan
      case 5:
        sourceEntity.setProperty("pb_plane:red_1", 0.1);
        sourceEntity.setProperty("pb_plane:green_1", 0.6);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Light Blue
      case 6:
        sourceEntity.setProperty("pb_plane:red_1", 0.2);
        sourceEntity.setProperty("pb_plane:green_1", 0.7);
        sourceEntity.setProperty("pb_plane:blue_1", 0.9);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Blue
      case 7:
        sourceEntity.setProperty("pb_plane:red_1", 0.2);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Purple
      case 8:
        sourceEntity.setProperty("pb_plane:red_1", 0.5);
        sourceEntity.setProperty("pb_plane:green_1", 0.2);
        sourceEntity.setProperty("pb_plane:blue_1", 0.7);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Magenta
      case 9:
        sourceEntity.setProperty("pb_plane:red_1", 0.8);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.7);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Pink
      case 10:
        sourceEntity.setProperty("pb_plane:red_1", 1);
        sourceEntity.setProperty("pb_plane:green_1", 0.5);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Brown
      case 11:
        sourceEntity.setProperty("pb_plane:red_1", 0.5);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.2);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Black
      case 12:
        sourceEntity.setProperty("pb_plane:red_1", 0.1);
        sourceEntity.setProperty("pb_plane:green_1", 0.1);
        sourceEntity.setProperty("pb_plane:blue_1", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Gray
      case 13:
        sourceEntity.setProperty("pb_plane:red_1", 0.3);
        sourceEntity.setProperty("pb_plane:green_1", 0.3);
        sourceEntity.setProperty("pb_plane:blue_1", 0.3);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // Light Gray
      case 14:
        sourceEntity.setProperty("pb_plane:red_1", 0.6);
        sourceEntity.setProperty("pb_plane:green_1", 0.6);
        sourceEntity.setProperty("pb_plane:blue_1", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      // White
      case 15:
        sourceEntity.setProperty("pb_plane:red_1", 0.9);
        sourceEntity.setProperty("pb_plane:green_1", 0.9);
        sourceEntity.setProperty("pb_plane:blue_1", 0.9);
        sourceEntity.setProperty("pb_plane:alpha_1", 0.7);
        sourceEntity.setProperty("pb_plane:colored_1", true);
        break;
      case 16:
        openOptions2Basic(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Basic color picker - SECONDARY color
function openOptions2Basic2(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Basic Color Chooser - Layer 2")
    .body("Select a preset color from below to apply it to your vehicle:")
    .button("Red", "textures/PixelBiester/Airplane/form_icons/coloriser/red")
    .button(
      "Orange",
      "textures/PixelBiester/Airplane/form_icons/coloriser/orange"
    )
    .button(
      "Yellow",
      "textures/PixelBiester/Airplane/form_icons/coloriser/yellow"
    )
    .button("Lime", "textures/PixelBiester/Airplane/form_icons/coloriser/lime")
    .button(
      "Green",
      "textures/PixelBiester/Airplane/form_icons/coloriser/green"
    )
    .button("Cyan", "textures/PixelBiester/Airplane/form_icons/coloriser/cyan")
    .button(
      "Light Blue",
      "textures/PixelBiester/Airplane/form_icons/coloriser/light_blue"
    )
    .button("Blue", "textures/PixelBiester/Airplane/form_icons/coloriser/blue")
    .button(
      "Purple",
      "textures/PixelBiester/Airplane/form_icons/coloriser/purple"
    )
    .button(
      "Magenta",
      "textures/PixelBiester/Airplane/form_icons/coloriser/magenta"
    )
    .button("Pink", "textures/PixelBiester/Airplane/form_icons/coloriser/pink")
    .button(
      "Brown",
      "textures/PixelBiester/Airplane/form_icons/coloriser/brown"
    )
    .button(
      "Black",
      "textures/PixelBiester/Airplane/form_icons/coloriser/black"
    )
    .button("Gray", "textures/PixelBiester/Airplane/form_icons/coloriser/gray")
    .button(
      "Light Gray",
      "textures/PixelBiester/Airplane/form_icons/coloriser/light_gray"
    )
    .button(
      "White",
      "textures/PixelBiester/Airplane/form_icons/coloriser/white"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      // Red
      case 0:
        sourceEntity.setProperty("pb_plane:red_2", 0.7);
        sourceEntity.setProperty("pb_plane:green_2", 0.2);
        sourceEntity.setProperty("pb_plane:blue_2", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Orange
      case 1:
        sourceEntity.setProperty("pb_plane:red_2", 1);
        sourceEntity.setProperty("pb_plane:green_2", 0.5);
        sourceEntity.setProperty("pb_plane:blue_2", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Yellow
      case 2:
        sourceEntity.setProperty("pb_plane:red_2", 1);
        sourceEntity.setProperty("pb_plane:green_2", 0.8);
        sourceEntity.setProperty("pb_plane:blue_2", 0.2);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Lime
      case 3:
        sourceEntity.setProperty("pb_plane:red_2", 0.5);
        sourceEntity.setProperty("pb_plane:green_2", 0.8);
        sourceEntity.setProperty("pb_plane:blue_2", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Green
      case 4:
        sourceEntity.setProperty("pb_plane:red_2", 0.4);
        sourceEntity.setProperty("pb_plane:green_2", 0.5);
        sourceEntity.setProperty("pb_plane:blue_2", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Cyan
      case 5:
        sourceEntity.setProperty("pb_plane:red_2", 0.1);
        sourceEntity.setProperty("pb_plane:green_2", 0.6);
        sourceEntity.setProperty("pb_plane:blue_2", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Light Blue
      case 6:
        sourceEntity.setProperty("pb_plane:red_2", 0.2);
        sourceEntity.setProperty("pb_plane:green_2", 0.7);
        sourceEntity.setProperty("pb_plane:blue_2", 0.9);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Blue
      case 7:
        sourceEntity.setProperty("pb_plane:red_2", 0.2);
        sourceEntity.setProperty("pb_plane:green_2", 0.3);
        sourceEntity.setProperty("pb_plane:blue_2", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Purple
      case 8:
        sourceEntity.setProperty("pb_plane:red_2", 0.5);
        sourceEntity.setProperty("pb_plane:green_2", 0.2);
        sourceEntity.setProperty("pb_plane:blue_2", 0.7);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Magenta
      case 9:
        sourceEntity.setProperty("pb_plane:red_2", 0.8);
        sourceEntity.setProperty("pb_plane:green_2", 0.3);
        sourceEntity.setProperty("pb_plane:blue_2", 0.7);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Pink
      case 10:
        sourceEntity.setProperty("pb_plane:red_2", 1);
        sourceEntity.setProperty("pb_plane:green_2", 0.5);
        sourceEntity.setProperty("pb_plane:blue_2", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Brown
      case 11:
        sourceEntity.setProperty("pb_plane:red_2", 0.5);
        sourceEntity.setProperty("pb_plane:green_2", 0.3);
        sourceEntity.setProperty("pb_plane:blue_2", 0.2);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Black
      case 12:
        sourceEntity.setProperty("pb_plane:red_2", 0.1);
        sourceEntity.setProperty("pb_plane:green_2", 0.1);
        sourceEntity.setProperty("pb_plane:blue_2", 0.1);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Gray
      case 13:
        sourceEntity.setProperty("pb_plane:red_2", 0.3);
        sourceEntity.setProperty("pb_plane:green_2", 0.3);
        sourceEntity.setProperty("pb_plane:blue_2", 0.3);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // Light Gray
      case 14:
        sourceEntity.setProperty("pb_plane:red_2", 0.6);
        sourceEntity.setProperty("pb_plane:green_2", 0.6);
        sourceEntity.setProperty("pb_plane:blue_2", 0.6);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      // White
      case 15:
        sourceEntity.setProperty("pb_plane:red_2", 0.9);
        sourceEntity.setProperty("pb_plane:green_2", 0.9);
        sourceEntity.setProperty("pb_plane:blue_2", 0.9);
        sourceEntity.setProperty("pb_plane:alpha_2", 0.7);
        sourceEntity.setProperty("pb_plane:colored_2", true);
        break;
      case 16:
        openOptions2Basic(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}

/// Basic color picker - select layer
function openOptions2Complex(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Complex Color Chooser")
    .body(
      "Here you can define your chosen colour by using RGB sliders, or even type in a HEX value, select an option below to customise it:"
    )
    .button(
      "Layer 1",
      "textures/PixelBiester/Airplane/form_icons/coloriser/complex1"
    )
    .button(
      "Layer 2",
      "textures/PixelBiester/Airplane/form_icons/coloriser/complex2"
    )
    .button("Exit", "textures/PixelBiester/Airplane/form_icons/exit_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      // color 1
      case 0:
        openOptions2Complex1(player, sourceEntity);
        break;
      // color 2
      case 1:
        openOptions2Complex2(player, sourceEntity);
        break;
      // Exit
      case 2:
        break;
    }
  });
}
/// Complex color picker - MAIN color
function openOptions2Complex1(player, sourceEntity) {
  // Convert RGB float properties (0.0 to 1.0) to integers (0 to 255)
  const currentRed_1 = Math.floor(
    sourceEntity.getProperty("pb_plane:red_1") * 255
  );
  const currentGreen_1 = Math.floor(
    sourceEntity.getProperty("pb_plane:green_1") * 255
  );
  const currentBlue_1 = Math.floor(
    sourceEntity.getProperty("pb_plane:blue_1") * 255
  );
  const currentAlpha_1 = Math.floor(
    sourceEntity.getProperty("pb_plane:alpha_1") * 10
  );

  const form = new ModalFormData()
    .title("Complex Color Chooser - Layer 1")
    .slider("Red", 0, 255, 1, currentRed_1) // Sliders for RGB
    .slider("Green", 0, 255, 1, currentGreen_1)
    .slider("Blue", 0, 255, 1, currentBlue_1)
    .slider("Alpha", 0, 8, 1, currentAlpha_1) // Slider for Alpha (0.0 to 1.0 * 10)
    .textField("Hex Color", "#FFFFFF"); // Default hex color value

  form
    .show(player)
    .then((response) => {
      if (response.canceled) {
        return;
      } else {
        // Only apply the changes if the form is not canceled
        sourceEntity.setProperty("pb_plane:colored_1", true);

        // Get the new values from the sliders
        const newRed_1 = (response.formValues[0] / 255).toFixed(1); // Convert back to float and round to 1 decimal place
        const newGreen_1 = (response.formValues[1] / 255).toFixed(1);
        const newBlue_1 = (response.formValues[2] / 255).toFixed(1);
        const newAlpha_1 = (response.formValues[3] / 10).toFixed(1);

        // Get the hex value from the text field
        const hexColor1 = response.formValues[4];
        const rgbFromHex1 = hexToRgb(hexColor1);

        // Set properties based on the slider values
        sourceEntity.setProperty("pb_plane:red_1", parseFloat(newRed_1));
        sourceEntity.setProperty("pb_plane:green_1", parseFloat(newGreen_1));
        sourceEntity.setProperty("pb_plane:blue_1", parseFloat(newBlue_1));
        sourceEntity.setProperty("pb_plane:alpha_1", parseFloat(newAlpha_1));

        // If a valid hex color is provided, update RGB values accordingly
        if (rgbFromHex1) {
          player.sendMessage(
            `Hex: ${hexColor1} converted to: Red = ${rgbFromHex1.r}, Green = ${rgbFromHex1.g}, Blue = ${rgbFromHex1.b}`
          );
          // Optionally update the properties with RGB from hex as well
          sourceEntity.setProperty("pb_plane:red_1", rgbFromHex1.r / 255);
          sourceEntity.setProperty("pb_plane:green_1", rgbFromHex1.g / 255);
          sourceEntity.setProperty("pb_plane:blue_1", rgbFromHex1.b / 255);
        }
      }
    })
    .catch((error) => {
      player.sendMessage(`An error occurred TEST: ${error}`);
    });
}

/// Complex color picker - SECONDARY color
function openOptions2Complex2(player, sourceEntity) {
  // Convert RGB float properties (0.0 to 1.0) to integers (0 to 255)
  const currentRed_2 = Math.floor(
    sourceEntity.getProperty("pb_plane:red_2") * 255
  );
  const currentGreen_2 = Math.floor(
    sourceEntity.getProperty("pb_plane:green_2") * 255
  );
  const currentBlue_2 = Math.floor(
    sourceEntity.getProperty("pb_plane:blue_2") * 255
  );
  const currentAlpha_2 = Math.floor(
    sourceEntity.getProperty("pb_plane:alpha_2") * 10
  ); // Assuming alpha is scaled 0.0 to 1.0 and needs to be between 0 and 8.

  const form = new ModalFormData()
    .title("Complex Color Chooser - Layer 2")
    .slider("Red", 0, 255, 1, currentRed_2) // Sliders for RGB
    .slider("Green", 0, 255, 1, currentGreen_2)
    .slider("Blue", 0, 255, 1, currentBlue_2)
    .slider("Alpha", 0, 8, 1, currentAlpha_2) // Slider for Alpha (0.0 to 1.0 * 10)
    .textField("Hex Color", "#FFFFFF"); // Default hex color value

  form
    .show(player)
    .then((response) => {
      if (response.canceled) {
        return;
      } else {
        sourceEntity.setProperty("pb_plane:colored_2", true);
        // Get the new values from the sliders
        const newRed_2 = (response.formValues[0] / 255).toFixed(1); // Convert back to float and round to 1 decimal place
        const newGreen_2 = (response.formValues[1] / 255).toFixed(1);
        const newBlue_2 = (response.formValues[2] / 255).toFixed(1);
        const newAlpha_2 = (response.formValues[3] / 10).toFixed(1); // Convert back to float and round to 1 decimal place

        // Get the hex value from the text field
        const hexColor2 = response.formValues[4];
        const rgbFromHex2 = hexToRgb(hexColor2);

        // Set properties based on the slider values
        sourceEntity.setProperty("pb_plane:red_2", parseFloat(newRed_2));
        sourceEntity.setProperty("pb_plane:green_2", parseFloat(newGreen_2));
        sourceEntity.setProperty("pb_plane:blue_2", parseFloat(newBlue_2));
        sourceEntity.setProperty("pb_plane:alpha_2", parseFloat(newAlpha_2)); // Set float value for alpha
        // If a valid hex color is provided, update RGB values accordingly
        if (rgbFromHex2) {
          player.sendMessage(
            `Hex: ${hexColor2} converted to: Red = ${rgbFromHex2.r}, Green = ${rgbFromHex2.g}, Blue = ${rgbFromHex2.b}`
          );
          // Optionally update the properties with RGB from hex as well
          sourceEntity.setProperty("pb_plane:red_2", rgbFromHex2.r / 255);
          sourceEntity.setProperty("pb_plane:green_2", rgbFromHex2.g / 255);
          sourceEntity.setProperty("pb_plane:blue_2", rgbFromHex2.b / 255);
        }
      }
    })
    .catch((error) => {
      player.sendMessage(`An error occurred: ${error}`);
    });
}

function openOptions2StyleMenu(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Style Options")
    .body("Here you can choose the style of your vehicle")
    .button(
      "Open Style Chooser",
      "textures/PixelBiester/Airplane/form_icons/coloriser/style"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        openOptions2Style(player, sourceEntity);
        break;
      case 1:
        opencolorOptions2(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}

/// Style selector
function openOptions2Style(player, sourceEntity) {
  // Convert RGB float properties (0.0 to 1.0) to integers (0 to 255)
  const currentStyle = sourceEntity.getProperty("pb_plane:pattern") + 1;

  const form = new ModalFormData()
    .title("Style Options")
    .slider("Style Type", 1, 3, 1, currentStyle);

  form
    .show(player)
    .then((response) => {
      if (response.canceled) {
        return;
      } else {
        // Set properties based on the slider values
        sourceEntity.setProperty(
          "pb_plane:pattern",
          response.formValues[0] - 1
        );
      }
    })
    .catch((error) => {
      player.sendMessage(`An error occurred: ${error}`);
    });
}
/// Remove coloring and revert to original design - select layer
function openOptions2Remove(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Remove Colors")
    .body(
      "You can remove your custom colors from one of the options below:"
    )
    .button(
      "Layer 1",
      "textures/PixelBiester/Airplane/form_icons/coloriser/remove1"
    )
    .button(
      "Layer 2",
      "textures/PixelBiester/Airplane/form_icons/coloriser/remove2"
    )
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      // color 1
      case 0:
        openOptions2Remove1(player, sourceEntity);
        break;
      // color 2
      case 1:
        openOptions2Remove2(player, sourceEntity);
        break;
      // Exit
      case 2:
        opencolorOptions2(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Remove coloring and revert to original design - MAIN color
function openOptions2Remove1(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Remove Colors - Layer 1")
    .body("Would you like to remove your custom colors from this vehicle?")
    .button("Confirm", "textures/PixelBiester/Airplane/form_icons/confirm_icon")
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        sourceEntity.setProperty("pb_plane:colored_1", false);
        break;
      case 1:
        openOptions2Remove(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
/// Remove coloring and revert to original design - SECONDARY color
function openOptions2Remove2(player, sourceEntity) {
  const form = new ActionFormData()
    .title("Remove Colors - Layer 2")
    .body("Would you like to remove your custom colors from this vehicle?")
    .button("Confirm", "textures/PixelBiester/Airplane/form_icons/confirm_icon")
    .button("Return", "textures/PixelBiester/Airplane/form_icons/return_icon");

  form.show(player).then((response) => {
    if (response.canceled) {
      return;
    }

    switch (response.selection) {
      case 0:
        sourceEntity.setProperty("pb_plane:colored_2", false);
        break;
      case 1:
        openOptions2Remove(player, sourceEntity);
        break;
      default:
        break;
    }
  });
}
