package studio.halbear.hem.procedures;

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

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.TickEvent;

import net.minecraft.world.World;
import net.minecraft.entity.Entity;

import java.util.Map;
import java.util.HashMap;

public class VehicleYawProcedure {
	@Mod.EventBusSubscriber
	private static class GlobalTrigger {
		@SubscribeEvent
		public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
			if (event.phase == TickEvent.Phase.END) {
				Entity entity = event.player;
				World world = entity.world;
				double i = entity.getPosX();
				double j = entity.getPosY();
				double k = entity.getPosZ();
				Map<String, Object> dependencies = new HashMap<>();
				dependencies.put("x", i);
				dependencies.put("y", j);
				dependencies.put("z", k);
				dependencies.put("world", world);
				dependencies.put("entity", entity);
				dependencies.put("event", event);
				executeProcedure(dependencies);
			}
		}
	}

	public static void executeProcedure(Map<String, Object> dependencies) {
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				HemMod.LOGGER.warn("Failed to load dependency entity for procedure VehicleYaw!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		if ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new HemModVariables.PlayerVariables())).VehicleTurnLeft) {
			{
				double _setval = ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new HemModVariables.PlayerVariables())).VehicleYaw - 1);
				entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.VehicleYaw = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
		}
		if ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
				.orElse(new HemModVariables.PlayerVariables())).VehicleTurnRight) {
			{
				double _setval = ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
						.orElse(new HemModVariables.PlayerVariables())).VehicleYaw + 1);
				entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.VehicleYaw = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
		}
	}
}
