package studio.halbear.hem.procedures;

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

import net.minecraft.world.IWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.Entity;
import net.minecraft.block.Blocks;

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

public class TestPlaneOnEntityTickUpdateProcedure {

	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 TestPlaneOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				HemMod.LOGGER.warn("Failed to load dependency x for procedure TestPlaneOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				HemMod.LOGGER.warn("Failed to load dependency y for procedure TestPlaneOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				HemMod.LOGGER.warn("Failed to load dependency z for procedure TestPlaneOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				HemMod.LOGGER.warn("Failed to load dependency entity for procedure TestPlaneOnEntityTickUpdate!");
			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");
		entity.rotationYaw = (float) (0);
		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) (0);
		entity.getPersistentData().putDouble("PropSpeed", ((entity.getPersistentData().getDouble("PropSpeed")
				+ Math.min(20 * (Math.abs(entity.getMotion().getZ()) + Math.abs(entity.getMotion().getX())), 20)) % 360));
		if (!((world.getBlockState(new BlockPos(x, y - 2, z))).getBlock() == Blocks.AIR)) {
			entity.getPersistentData().putDouble("Pitch",
					((-20) * (1 - Math.min(2 * (Math.abs(entity.getMotion().getZ()) + Math.abs(entity.getMotion().getX())), 1))));
		}
		if (0.3 > Math.abs(entity.getMotion().getZ()) + Math.abs(entity.getMotion().getX())) {
			entity.setMotion(
					(Math.sin(Math.toRadians(1 * entity.getPersistentData().getDouble("Yaw")))
							* (entity.getPersistentData().getDouble("Throttle") / 100)),
					(entity.getMotion().getY()), (Math.cos(Math.toRadians(1 * entity.getPersistentData().getDouble("Yaw")))
							* (entity.getPersistentData().getDouble("Throttle") / 100)));
		} else {
			entity.setMotion(
					(Math.cos(Math.toRadians((-1) * entity.getPersistentData().getDouble("Roll")))
							* Math.sin(Math.toRadians((-1) * entity.getPersistentData().getDouble("Yaw")))
							* (entity.getPersistentData().getDouble("Throttle") / 100)
							+ Math.sin(Math.toRadians((-1) * entity.getPersistentData().getDouble("Roll")))
									* Math.sin(Math.toRadians(1 * entity.getPersistentData().getDouble("Pitch")))
									* (entity.getPersistentData().getDouble("Throttle") / 100)),
					(Math.cos(Math.toRadians((-1) * entity.getPersistentData().getDouble("Roll")))
							* Math.sin(Math.toRadians((-1) * entity.getPersistentData().getDouble("Pitch")))
							* (entity.getPersistentData().getDouble("Throttle") / 100)
							+ Math.sin(Math.toRadians((-1) * entity.getPersistentData().getDouble("Roll")))
									* Math.sin(Math.toRadians((-1) * entity.getPersistentData().getDouble("Yaw")))
									* (entity.getPersistentData().getDouble("Throttle") / 100)),
					(Math.cos(Math.toRadians((-1) * entity.getPersistentData().getDouble("Roll")))
							* Math.cos(Math.toRadians((-1) * entity.getPersistentData().getDouble("Yaw")))
							* (entity.getPersistentData().getDouble("Throttle") / 100)
							+ Math.sin(Math.toRadians((-1) * entity.getPersistentData().getDouble("Roll")))
									* Math.sin(Math.toRadians(1 * entity.getPersistentData().getDouble("Pitch")))
									* (entity.getPersistentData().getDouble("Throttle") / 100)));
		}
		if (entity.isBeingRidden()) {
			{
				List<Entity> _entfound = world
						.getEntitiesWithinAABB(Entity.class,
								new AxisAlignedBB(x - (5 / 2d), y - (5 / 2d), z - (5 / 2d), x + (5 / 2d), y + (5 / 2d), z + (5 / 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.getRidingEntity()) instanceof TestPlaneEntity.CustomEntity) {
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleIncreasePressure) {
							entity.getPersistentData().putDouble("Throttle", Math.min(entity.getPersistentData().getDouble("Throttle") + 1, 100));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleReleasePressure) {
							entity.getPersistentData().putDouble("Throttle", Math.max(entity.getPersistentData().getDouble("Throttle") - 1, 0));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleAccelerate) {
							entity.getPersistentData().putDouble("Pitch", ((entity.getPersistentData().getDouble("Pitch") + 1) % 360));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleDecelerate) {
							entity.getPersistentData().putDouble("Pitch", ((entity.getPersistentData().getDouble("Pitch") - 1) % 360));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleTurnLeft) {
							entity.getPersistentData().putDouble("Roll", ((entity.getPersistentData().getDouble("Roll") - 1) % 360));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleTurnRight) {
							entity.getPersistentData().putDouble("Roll", ((entity.getPersistentData().getDouble("Roll") + 1) % 360));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleStrafeLeft) {
							entity.getPersistentData().putDouble("Yaw", ((entity.getPersistentData().getDouble("Yaw") - 1) % 360));
						}
						if ((entityiterator.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).VehicleStrafeRight) {
							entity.getPersistentData().putDouble("Yaw", ((entity.getPersistentData().getDouble("Yaw") + 1) % 360));
						}
					}
				}
			}
		}
	}
}
