import { world, GameMode } from "@minecraft/server";
import growTreeFruit from "./growTreefruit";
import growTreeSmall from "./growTreesmall";
import growTreeMedium from "./growTreemedium";
import growTreeLarge from "./growTreelarge";
import growTreeRedwood from "./growTreeredwood";
import growTree9_11_13 from "./growTree9_11_13";
import growTree9_9_11_13 from "./growTree9_11_13";
import growTreeBone from "./growTreeBone";
import growTreeMushroom from "./growTreeMushroom";
import growTreePine from "./growTreePine";
import growTree15_17_19 from "./growTree15_17_19";
import growTreeGiantCherry from "./growGiantCherry";
import growTreeCoconut from "./growTreeCoconut";
import growTreeAvocado from "./growTreeAvocado";
import checkTreeGrowSpace from "./checkTreeGrowSpace";
import {
  fruitSaplings,
  mediumSaplings,
  largeSaplings,
  redwooodSaplings,
  smallSaplings,
  sapling_9_11_13,
  sapling_9_9_11_13,
  bonesapling,
  mushroomsapling,
  pinesapling,
  sapling_15_17_19,
  vanilla_cherry,
  coconutsapling,
  avocadoSaplings,
  pinkOleanderSaplings,
} from "../globals";
import growTreePinkOleander from "./growTreePinkOleander";

// Helper function to get player's game mode
function getGamemode(player) {
  return Object.values(GameMode).find((g) => [...world.getPlayers({ name: player.name, gameMode: g })].length);
}

export default function bonemealSapling(block, itemStack, player) {
  const { x, y, z } = block.location;

  try {
    if (block.location != undefined) {
      const dimension = block.dimension; // Dynamically get the dimension

      // Get the player's game mode using the helper function
      const playerGameMode = getGamemode(player);
      let isCreativeMode = playerGameMode === GameMode.creative;

      if (Math.floor(Math.random() * 3) < 2) {
        dimension.runCommand(`particle minecraft:crop_growth_emitter ${x} ${y} ${z}`);
        // Only clear bone meal if player is not in creative mode
        if (!isCreativeMode) {
          dimension.runCommand(`clear "${player.name}" minecraft:bone_meal -1 1`);
        }
      } else {
        // Only clear bone meal if player is not in creative mode
        if (!isCreativeMode) {
          dimension.runCommand(`clear "${player.name}" minecraft:bone_meal -1 1`);
        }
        dimension.runCommand(`particle minecraft:crop_growth_area_emitter ${x} ${y} ${z}`);

        // Check if there's space above the sapling for the tree to grow
        if (!checkTreeGrowSpace(block.location, dimension)) {
          // No space for the tree to grow
          return null;
        }

        if (smallSaplings.has(block.typeId)) {
          return growTreeSmall(block.location, block.typeId, true, dimension);
        } else if (fruitSaplings.has(block.typeId)) {
          return growTreeFruit(block.location, block.typeId, true, dimension);
        } else if (mediumSaplings.has(block.typeId)) {
          return growTreeMedium(block.location, block.typeId, true, dimension);
        } else if (largeSaplings.has(block.typeId)) {
          return growTreeLarge(block.location, block.typeId, true, dimension);
        } else if (redwooodSaplings.has(block.typeId)) {
          return growTreeRedwood(block.location, block.typeId, true, dimension);
        } else if (sapling_9_11_13.has(block.typeId)) {
          return growTree9_11_13(block.location, block.typeId, true, dimension);
        } else if (bonesapling.has(block.typeId)) {
          return growTreeBone(block.location, block.typeId, true, dimension);
        } else if (mushroomsapling.has(block.typeId)) {
          return growTreeMushroom(block.location, block.typeId, true, dimension);
        } else if (pinesapling.has(block.typeId)) {
          return growTreePine(block.location, block.typeId, true, dimension);
        } else if (sapling_15_17_19.has(block.typeId)) {
          return growTree15_17_19(block.location, block.typeId, true, dimension);
        } else if (vanilla_cherry.has(block.typeId)) {
          return growTreeGiantCherry(block.location, block.typeId, true, dimension);
        } else if (coconutsapling.has(block.typeId)) {
          return growTreeCoconut(block.location, block.typeId, true, dimension);
        } else if (avocadoSaplings.has(block.typeId)) {
          return growTreeAvocado(block.location, block.typeId, true, dimension);
        } else if (pinkOleanderSaplings.has(block.typeId)) {
          return growTreePinkOleander(block.location, block.typeId, true, dimension);
        } else if (sapling_9_9_11_13.has(block.typeId)) {
          return growTree9_9_11_13(block.location, block.typeId, true, dimension);
        }
      }
    }
  } catch (e) {
    console.error(e); // Log the error for debugging
  }
}
