package studio.halbear.hem.procedures;

import studio.halbear.hem.block.BlueleafWheatBlock;
import studio.halbear.hem.block.BlueleafStoneGravelBlock;
import studio.halbear.hem.block.BlueleafLushLeavesBlock;
import studio.halbear.hem.block.BlueleafLavenderBlock;
import studio.halbear.hem.block.BlueleafFluorescentLeavesBlock;
import studio.halbear.hem.block.BlueleafDirtGrassBlockBlock;
import studio.halbear.hem.block.BlueleafDirtBlockBlock;
import studio.halbear.hem.block.BlueleafCobblestoneWallBlock;
import studio.halbear.hem.block.BlueleafCobblestoneStairsBlock;
import studio.halbear.hem.block.BlueleafCobblestoneSlabBlock;
import studio.halbear.hem.block.BlueleafCobblestoneBlock;
import studio.halbear.hem.HemMod;

import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.IWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.block.Blocks;

import java.util.Random;
import java.util.Map;
import java.util.Iterator;

public class Wall1OnStructureInstanceGeneratedProcedure {

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("world") == null) {
			if (!dependencies.containsKey("world"))
				HemMod.LOGGER.warn("Failed to load dependency world for procedure Wall1OnStructureInstanceGenerated!");
			return;
		}
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				HemMod.LOGGER.warn("Failed to load dependency x for procedure Wall1OnStructureInstanceGenerated!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				HemMod.LOGGER.warn("Failed to load dependency y for procedure Wall1OnStructureInstanceGenerated!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				HemMod.LOGGER.warn("Failed to load dependency z for procedure Wall1OnStructureInstanceGenerated!");
			return;
		}
		IWorld world = (IWorld) dependencies.get("world");
		double x = dependencies.get("x") instanceof Integer ? (int) dependencies.get("x") : (double) dependencies.get("x");
		double y = dependencies.get("y") instanceof Integer ? (int) dependencies.get("y") : (double) dependencies.get("y");
		double z = dependencies.get("z") instanceof Integer ? (int) dependencies.get("z") : (double) dependencies.get("z");
		double Iterator = 0;
		double Random = 0;
		double IteratorY = 0;
		double IteratorX = 0;
		double IteratorZ = 0;
		double Random2 = 0;
		double posY = 0;
		double PosX = 0;
		double PosZ = 0;
		double PosYSource = 0;
		double length = 0;
		double PreviousType = 0;
		if (z < -16) {
			world.setBlockState(new BlockPos(x, y + 1, z), Blocks.AIR.getDefaultState(), 3);
			world.setBlockState(new BlockPos(x, y + 1, z), BlueleafLushLeavesBlock.block.getDefaultState(), 3);
			world.setBlockState(new BlockPos(x, y, z), Blocks.AIR.getDefaultState(), 3);
			world.setBlockState(new BlockPos(x, y, z), BlueleafLushLeavesBlock.block.getDefaultState(), 3);
		}
		Iterator = 0;
		for (int index0 = 0; index0 < (int) (3); index0++) {
			PreviousType = Random;
			IteratorX = 0;
			IteratorZ = 0;
			IteratorY = 0;
			Random = Math.round(2 * Math.random());
			length = (4 + Math.round(6 * Math.random()));
			if (Random == 1) {
				length = 16;
			}
			if (z < -16) {
				length = 8;
				if (Random != 1) {
					Random = Math.round(2 * Math.random());
				}
			}
			while (length > 0) {
				length = (length - 1);
				if (Iterator == 0) {
					IteratorX = (IteratorX - 1);
				}
				if (Iterator == 1) {
					IteratorX = (IteratorX + 1);
					if ((x - 8) % 16 != 0 && PreviousType == 1) {
						Random = 0;
					}
				}
				if (Iterator == 2) {
					IteratorZ = (IteratorZ + 1);
				}
				if (Iterator == 3) {
					IteratorZ = (IteratorZ - 1);
					if ((z - 8) % 16 != 0 && PreviousType == 1) {
						Random = 0;
					}
				}
				if ((Iterator > 1 && (x - 8) % 16 == 0 || Iterator <= 1 && (z - 8) % 16 == 0)
						&& (!BlockTags.getCollection().getTagByID(new ResourceLocation("forge:solid_block"))
								.contains((world.getBlockState(new BlockPos(x + IteratorX,
										world.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, (int) (x + IteratorX), (int) (z + IteratorZ))
												+ IteratorY,
										z + IteratorZ))).getBlock())
								|| (Random == 1 || z + IteratorZ < 0) && !((world.getBlockState(new BlockPos(x + IteratorX,
										world.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, (int) (x + IteratorX), (int) (z + IteratorZ))
												+ IteratorY,
										z + IteratorZ))).getBlock() == BlueleafWheatBlock.block
										&& !((world.getBlockState(new BlockPos(x + IteratorX,
												world.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, (int) (x + IteratorX),
														(int) (z + IteratorZ)) + IteratorY,
												z + IteratorZ))).getBlock() == BlueleafLavenderBlock.block)))) {
					posY = (world.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, (int) (x + IteratorX), (int) (z + IteratorZ)) + IteratorY);
					PosYSource = (world.getHeight(Heightmap.Type.MOTION_BLOCKING_NO_LEAVES, (int) (x + IteratorX), (int) (z + IteratorZ)));
					PosX = (x + IteratorX);
					PosZ = (z + IteratorZ);
					Random2 = Math.random();
					if (!((world.getBlockState(new BlockPos(PosX, PosYSource, PosZ))).getBlock() == Blocks.WATER)) {
						if (Random == 1 && ((x - 8) % 16 != 0 || (z - 8) % 16 != 0)) {
							if ((world.getBlockState(new BlockPos(PosX, PosYSource - 1, PosZ))).getBlock() == BlueleafDirtBlockBlock.block) {
								world.setBlockState(new BlockPos(PosX, PosYSource + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
								world.setBlockState(new BlockPos(PosX, PosYSource + 2, PosZ), Blocks.AIR.getDefaultState(), 3);
								world.setBlockState(new BlockPos(PosX, PosYSource, PosZ), Blocks.AIR.getDefaultState(), 3);
								if (PosZ < 8) {
									world.setBlockState(new BlockPos(PosX, PosYSource, PosZ), BlueleafLavenderBlock.block.getDefaultState(), 3);
								} else {
									world.setBlockState(new BlockPos(PosX, PosYSource, PosZ), BlueleafWheatBlock.block.getDefaultState(), 3);
								}
							} else if ((world.getBlockState(new BlockPos(PosX, PosYSource - 2, PosZ))).getBlock() == BlueleafStoneGravelBlock.block) {
								world.setBlockState(new BlockPos(PosX, PosYSource + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
								world.setBlockState(new BlockPos(PosX, PosYSource + 2, PosZ), Blocks.AIR.getDefaultState(), 3);
								world.setBlockState(new BlockPos(PosX, PosYSource - 2, PosZ), Blocks.AIR.getDefaultState(), 3);
								world.setBlockState(new BlockPos(PosX, PosYSource - 2, PosZ), BlueleafDirtBlockBlock.block.getDefaultState(), 3);
								world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ), Blocks.AIR.getDefaultState(), 3);
								if (PosZ < 8) {
									world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ), BlueleafLavenderBlock.block.getDefaultState(), 3);
								} else {
									world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ), BlueleafWheatBlock.block.getDefaultState(), 3);
								}
							}
						} else if (Random == 1 && (x - 8) % 16 == 0 && (z - 8) % 16 == 0) {
							length = 0;
						}
						if (!((world.getBlockState(new BlockPos(PosX, PosYSource, PosZ))).getBlock() == BlueleafWheatBlock.block)
								&& !((world.getBlockState(new BlockPos(PosX, PosYSource, PosZ))).getBlock() == BlueleafLavenderBlock.block)
								&& (world.getBlockState(new BlockPos(PosX, PosYSource - 1, PosZ))).getBlock() == BlueleafDirtBlockBlock.block) {
							if (Random == 0) {
								if (PosZ < -16) {
									world.setBlockState(new BlockPos(PosX, posY, PosZ), Blocks.AIR.getDefaultState(), 3);
									world.setBlockState(new BlockPos(PosX, posY, PosZ), BlueleafLushLeavesBlock.block.getDefaultState(), 3);
									if (length > 1 && Math.random() > 0.3) {
										world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), BlueleafLushLeavesBlock.block.getDefaultState(), 3);
									}
								} else {
									world.setBlockState(new BlockPos(PosX, posY, PosZ), Blocks.AIR.getDefaultState(), 3);
									world.setBlockState(new BlockPos(PosX, posY, PosZ), BlueleafCobblestoneBlock.block.getDefaultState(), 3);
									if (Math.random() > 0.7) {
										world.setBlockState(new BlockPos(PosX, posY, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, posY, PosZ), BlueleafCobblestoneStairsBlock.block.getDefaultState(),
												3);
									}
									if (length > 1) {
										world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), BlueleafCobblestoneSlabBlock.block.getDefaultState(),
												3);
										if (Math.random() > 0.7) {
											world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
											world.setBlockState(new BlockPos(PosX, posY + 1, PosZ),
													BlueleafCobblestoneWallBlock.block.getDefaultState(), 3);
										}
									}
								}
								if (Random == 2) {
									if (PosZ < -16) {
										world.setBlockState(new BlockPos(PosX, posY, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, posY, PosZ), BlueleafFluorescentLeavesBlock.block.getDefaultState(),
												3);
										if (length > 1 && Math.random() > 0.3) {
											world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
											world.setBlockState(new BlockPos(PosX, posY + 1, PosZ),
													BlueleafFluorescentLeavesBlock.block.getDefaultState(), 3);
										}
									} else {
										world.setBlockState(new BlockPos(PosX, posY, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, posY, PosZ), BlueleafCobblestoneBlock.block.getDefaultState(), 3);
										if (length > 1) {
											world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
											world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), BlueleafCobblestoneBlock.block.getDefaultState(),
													3);
											if (Math.random() > 0.5) {
												world.setBlockState(new BlockPos(PosX, posY + 1, PosZ), Blocks.AIR.getDefaultState(), 3);
												world.setBlockState(new BlockPos(PosX, posY + 1, PosZ),
														BlueleafStoneGravelBlock.block.getDefaultState(), 3);
											}
										}
									}
								}
								if (Random != 1) {
									if (PosZ < 0) {
										world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ),
												BlueleafDirtGrassBlockBlock.block.getDefaultState(), 3);
									} else {
										world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ), Blocks.AIR.getDefaultState(), 3);
										world.setBlockState(new BlockPos(PosX, PosYSource - 1, PosZ),
												BlueleafCobblestoneBlock.block.getDefaultState(), 3);
									}
								}
							}
						}
					}
				}
			}
			Iterator = (Iterator + 1);
		}
	}
}
