package studio.halbear.hem.procedures;

import studio.halbear.hem.HemModVariables;
import studio.halbear.hem.HemMod;

import net.minecraft.world.IWorld;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.Entity;

import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.Map;
import java.util.List;
import java.util.Comparator;

public class HotAirBalloonOnEntityTickUpdateProcedure {

	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 HotAirBalloonOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				HemMod.LOGGER.warn("Failed to load dependency x for procedure HotAirBalloonOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				HemMod.LOGGER.warn("Failed to load dependency y for procedure HotAirBalloonOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				HemMod.LOGGER.warn("Failed to load dependency z for procedure HotAirBalloonOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				HemMod.LOGGER.warn("Failed to load dependency entity for procedure HotAirBalloonOnEntityTickUpdate!");
			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");
		Entity entity = (Entity) dependencies.get("entity");
		boolean W = false;
		boolean S = false;
		boolean A = false;
		boolean D = false;
		Entity Player = null;
		{
			List<Entity> _entfound = world
					.getEntitiesWithinAABB(Entity.class,
							new AxisAlignedBB(x - (9 / 2d), y - (9 / 2d), z - (9 / 2d), x + (9 / 2d), y + (9 / 2d), z + (9 / 2d)), null)
					.stream().sorted(new Object() {
						Comparator<Entity> compareDistOf(double _x, double _y, double _z) {
							return Comparator.comparing((Function<Entity, Double>) (_entcnd -> _entcnd.getDistanceSq(_x, _y, _z)));
						}
					}.compareDistOf(x, y, z)).collect(Collectors.toList());
			for (Entity entityiterator : _entfound) {
				if (entityiterator.isPassenger() && entityiterator instanceof ServerPlayerEntity) {
					if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
							.orElse(new HemModVariables.PlayerVariables())).PilotingVehicleID == entity.getPersistentData().getDouble("ID")) {
						{
							double _setval = (entity.getPersistentData().getDouble("Pressure"));
							entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
								capability.PSI = _setval;
								capability.syncPlayerVariables(entityiterator);
							});
						}
						{
							double _setval = (entity.getPersistentData().getDouble("Fuel"));
							entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
								capability.Fuel = _setval;
								capability.syncPlayerVariables(entityiterator);
							});
						}
						{
							double _setval = (entity.getPersistentData().getDouble("Heat"));
							entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
								capability.Heat = _setval;
								capability.syncPlayerVariables(entityiterator);
							});
						}
						entity.rotationYaw = (float) (((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleYaw));
						entity.setRenderYawOffset(entity.rotationYaw);
						entity.prevRotationYaw = entity.rotationYaw;
						if (entity instanceof LivingEntity) {
							((LivingEntity) entity).prevRenderYawOffset = entity.rotationYaw;
							((LivingEntity) entity).rotationYawHead = entity.rotationYaw;
							((LivingEntity) entity).prevRotationYawHead = entity.rotationYaw;
						}
						entity.rotationPitch = (float) ((entity.rotationPitch));
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleIncreasePressure) {
							entity.getPersistentData().putBoolean("OpenFlame", (true));
						} else {
							entity.getPersistentData().putBoolean("OpenFlame", (false));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleForward) {
							entity.getPersistentData().putDouble("XVel", (0.025 * entity.getLookVec().x));
							entity.getPersistentData().putDouble("YVel", (0.025 * entity.getLookVec().z));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleBackward) {
							entity.getPersistentData().putDouble("YVel", ((-0.025) * entity.getLookVec().z));
							entity.getPersistentData().putDouble("XVel", ((-0.025) * entity.getLookVec().x));
						}
					}
				}
			}
		}
		entity.getPersistentData().putDouble("Tick", (entity.getPersistentData().getDouble("Tick") - 1));
		if (entity.getMotion().getX() > 0.35) {
			entity.getPersistentData().putDouble("XVel", (0.35 - entity.getMotion().getX()));
		}
		if (entity.getMotion().getX() < -0.35) {
			entity.getPersistentData().putDouble("XVel", ((-0.35) - entity.getMotion().getX()));
		}
		if (entity.getMotion().getZ() > 0.35) {
			entity.getPersistentData().putDouble("YVel", (0.35 - entity.getMotion().getZ()));
		}
		if (entity.getMotion().getZ() < -0.35) {
			entity.getPersistentData().putDouble("YVel", ((-0.35) - entity.getMotion().getZ()));
		}
		if (entity.getPersistentData().getDouble("Heat") <= 10 && entity.getPersistentData().getDouble("Pressure") <= 0
				&& entity.getMotion().getY() >= -0.5) {
			entity.setMotion((entity.getMotion().getX() + entity.getPersistentData().getDouble("XVel")), (entity.getMotion().getY() - 0.05),
					(entity.getMotion().getZ() + entity.getPersistentData().getDouble("YVel")));
		} else if (entity.getPersistentData().getDouble("Pressure") > 0) {
			entity.setMotion((entity.getMotion().getX() + entity.getPersistentData().getDouble("XVel")),
					(entity.getPersistentData().getDouble("Pressure") / 75),
					(entity.getMotion().getZ() + entity.getPersistentData().getDouble("YVel")));
		} else if (entity.getMotion().getY() >= -0.05) {
			entity.setMotion((entity.getMotion().getX() + entity.getPersistentData().getDouble("XVel")), (entity.getMotion().getY() - 0.05),
					(entity.getMotion().getZ() + entity.getPersistentData().getDouble("YVel")));
		} else {
			entity.setMotion((entity.getMotion().getX() + entity.getPersistentData().getDouble("XVel")), (entity.getMotion().getY()),
					(entity.getMotion().getZ() + entity.getPersistentData().getDouble("YVel")));
		}
		entity.getPersistentData().putDouble("YVel", 0);
		entity.getPersistentData().putDouble("XVel", 0);
		if (entity.getPersistentData().getDouble("Pressure") <= 35 && entity.getPersistentData().getBoolean("OpenFlame")) {
			entity.getPersistentData().putDouble("Pressure",
					(entity.getPersistentData().getDouble("Pressure") + entity.getPersistentData().getDouble("Heat") / 150));
		}
		if (entity.getPersistentData().getBoolean("OpenFlame")) {
			world.addParticle(ParticleTypes.LARGE_SMOKE, x, y, z, 0, (entity.getMotion().getY() + 0.2), 0);
		}
		if (entity.getPersistentData().getDouble("Tick") <= 0) {
			entity.getPersistentData().putDouble("Tick", 20);
			if (entity.getPersistentData().getDouble("Pressure") > 0 && entity.getPersistentData().getDouble("Heat") >= 10
					&& !entity.getPersistentData().getBoolean("OpenFlame")) {
				entity.getPersistentData().putDouble("Pressure", Math.round(entity.getPersistentData().getDouble("Pressure") - 1));
			} else if (entity.getPersistentData().getDouble("Pressure") > 0 && entity.getPersistentData().getDouble("Heat") < 10) {
				entity.getPersistentData().putDouble("Pressure", Math.round(entity.getPersistentData().getDouble("Pressure") - 5));
			}
			if (entity.getPersistentData().getDouble("Fuel") > 0 && entity.getPersistentData().getDouble("Heat") < 150) {
				entity.getPersistentData().putDouble("Heat", (entity.getPersistentData().getDouble("Heat") + 5));
				entity.getPersistentData().putDouble("Fuel", (entity.getPersistentData().getDouble("Fuel") - 1));
			} else if (entity.getPersistentData().getDouble("Fuel") > 0 && entity.getPersistentData().getDouble("Heat") > 0) {
				entity.getPersistentData().putDouble("Fuel", (entity.getPersistentData().getDouble("Fuel") - 1));
			} else if (entity.getPersistentData().getDouble("Fuel") <= 0 && entity.getPersistentData().getDouble("Heat") > 0) {
				entity.getPersistentData().putDouble("Heat", (entity.getPersistentData().getDouble("Heat") - 1));
			}
		}
	}
}
