console.info("[SOCIETY] mayonnaiseMachine.js loaded");

global.mayonnaiseMachineRecipes = [
  { input: "minecraft:egg", output: ["1x society:mayonnaise"] },
  { input: "untitledduckmod:duck_egg", output: ["1x society:duck_mayonnaise"] },
  { input: "untitledduckmod:goose_egg", output: ["1x society:goose_mayonnaise"] },
  { input: "quark:egg_parrot_red_blue", output: ["1x society:parrot_mayonnaise"] },
  { input: "quark:egg_parrot_blue", output: ["1x society:parrot_mayonnaise"] },
  { input: "quark:egg_parrot_green", output: ["1x society:parrot_mayonnaise"] },
  { input: "quark:egg_parrot_yellow_blue", output: ["1x society:parrot_mayonnaise"] },
  { input: "quark:egg_parrot_gray", output: ["1x society:parrot_mayonnaise"] },
  { input: "minecraft:turtle_egg", output: ["1x society:turtle_mayonnaise"] },
  { input: "minecraft:sniffer_egg", output: ["1x society:sniffer_mayonnaise"] },
  { input: "minecraft:dragon_egg", output: ["1x society:dragon_mayonnaise"] },
  { input: "vintagedelight:golden_egg",  output: ["1x society:golden_mayonnaise"] },
  { input: "autumnity:turkey_egg", output: ["1x society:turkey_mayonnaise"] },
  { input: "society:large_egg", output: ["1x society:large_mayonnaise"] },
  { input: "society:large_duck_egg", output: ["1x society:large_duck_mayonnaise"] },
  { input: "society:large_goose_egg", output: ["1x society:large_goose_mayonnaise"] },
  { input: "society:large_turkey_egg", output: ["1x society:large_turkey_mayonnaise"] },
];

StartupEvents.registry("block", (event) => {
  event
    .create("society:mayonnaise_machine", "cardinal")
    .property(booleanProperty.create("working"))
    .property(booleanProperty.create("mature"))
    .property(booleanProperty.create("upgraded"))
    .property(integerProperty.create("stage", 0, 3))
    .soundType("copper")
    .property(
      integerProperty.create("type", 0, global.mayonnaiseMachineRecipes.length)
    )
    .box(2, 0, 2, 14, 19, 14)
    .defaultCutout()
    .tagBlock("minecraft:mineable/pickaxe")
    .tagBlock("minecraft:needs_stone_tool")
    .item((item) => {
      item.tooltip(Text.gray("Turns an egg into mayonnaise"));
      item.modelJson({
        parent: "society:block/mayonnaise_machine_off",
      });
    })

    .defaultState((state) => {
      state
        .set(booleanProperty.create("working"), false)
        .set(booleanProperty.create("mature"), false)
        .set(booleanProperty.create("upgraded"), false)
        .set(integerProperty.create("stage", 0, 3), 0)
        .set(
          integerProperty.create(
            "type",
            0,
            global.mayonnaiseMachineRecipes.length
          ),
          0
        );
    })
    .placementState((state) => {
      state
        .set(booleanProperty.create("working"), false)
        .set(booleanProperty.create("mature"), false)
        .set(booleanProperty.create("upgraded"), false)
        .set(integerProperty.create("stage", 0, 3), 0)
        .set(
          integerProperty.create(
            "type",
            0,
            global.mayonnaiseMachineRecipes.length
          ),
          0
        );
    })
    .rightClick((click) => {
      const { player, block } = click;
      const facing = block.properties.get("facing").toLowerCase();

      if (
        player.stages.has("rancher") &&
        block.properties.get("mature") === "true"
      ) {
        global.mayonnaiseMachineRecipes[
          Number(block.properties.get("type").toLowerCase()) - 1
        ].output.forEach((e) => {
          block.popItemFromFace(e, facing);
        });
      }

      handleBERightClick("minecraft:block.sniffer_egg.plop", click, global.mayonnaiseMachineRecipes, 3, false);
    })
    .randomTick((tick) => {
      handleBETick(tick, rnd50(), 3);
    }).blockstateJson = {
    multipart: [
      {
        apply: { model: "society:block/mayonnaise_machine_particle" },
      },
      {
        when: { mature: true },
        apply: { model: "society:block/machine_done" },
      },
      {
        when: { working: false, facing: "north" },
        apply: {
          model: "society:block/mayonnaise_machine_off",
          y: 0,
          uvlock: false,
        },
      },
      {
        when: { working: false, facing: "east" },
        apply: {
          model: "society:block/mayonnaise_machine_off",
          y: 90,
          uvlock: false,
        },
      },
      {
        when: { working: false, facing: "south" },
        apply: {
          model: "society:block/mayonnaise_machine_off",
          y: 180,
          uvlock: false,
        },
      },
      {
        when: { working: false, facing: "west" },
        apply: {
          model: "society:block/mayonnaise_machine_off",
          y: -90,
          uvlock: false,
        },
      },
      {
        when: { working: true, facing: "north" },
        apply: {
          model: "society:block/mayonnaise_machine",
          y: 0,
          uvlock: false,
        },
      },
      {
        when: { working: true, facing: "east" },
        apply: {
          model: "society:block/mayonnaise_machine",
          y: 90,
          uvlock: false,
        },
      },
      {
        when: { working: true, facing: "south" },
        apply: {
          model: "society:block/mayonnaise_machine",
          y: 180,
          uvlock: false,
        },
      },
      {
        when: { working: true, facing: "west" },
        apply: {
          model: "society:block/mayonnaise_machine",
          y: -90,
          uvlock: false,
        },
      },
    ],
  };
});
