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.world.IWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.potion.Effects;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.Entity;

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

public class GettingEatenProcedure {
	@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("world") == null) {
			if (!dependencies.containsKey("world"))
				HemMod.LOGGER.warn("Failed to load dependency world for procedure GettingEaten!");
			return;
		}
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				HemMod.LOGGER.warn("Failed to load dependency entity for procedure GettingEaten!");
			return;
		}
		IWorld world = (IWorld) dependencies.get("world");
		Entity entity = (Entity) dependencies.get("entity");
		if ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new HemModVariables.PlayerVariables())).Eaten
				&& (entity.getMotion().getY() > 0.15 || entity.getMotion().getZ() != 0 || entity.getMotion().getX() != 0)) {
			entity.setMotion(0, 0.01, 0);
			{
				Entity _ent = entity;
				_ent.setPositionAndUpdate(
						((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapX + 0.5),
						((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapY),
						((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
								.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapZ + 0.5));
				if (_ent instanceof ServerPlayerEntity) {
					((ServerPlayerEntity) _ent).connection.setPlayerLocation(
							((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
									.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapX + 0.5),
							((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
									.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapY),
							((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
									.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapZ + 0.5),
							_ent.rotationYaw, _ent.rotationPitch, Collections.emptySet());
				}
			}
		}
		if ((entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new HemModVariables.PlayerVariables())).Eaten
				&& ((new Object() {
					public boolean getValue(IWorld world, BlockPos pos, String tag) {
						TileEntity tileEntity = world.getTileEntity(pos);
						if (tileEntity != null)
							return tileEntity.getTileData().getBoolean(tag);
						return false;
					}
				}.getValue(world,
						new BlockPos(
								(entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
										.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapX,
								(entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
										.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapY,
								(entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
										.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapZ),
						"eating")) == false || !(new Object() {
							public String getValue(IWorld world, BlockPos pos, String tag) {
								TileEntity tileEntity = world.getTileEntity(pos);
								if (tileEntity != null)
									return tileEntity.getTileData().getString(tag);
								return "";
							}
						}.getValue(world,
								new BlockPos(
										(entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
												.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapX,
										(entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
												.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapY,
										(entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null)
												.orElse(new HemModVariables.PlayerVariables())).LastPlantTrapZ),
								"victimuuid")).equals(entity.getUniqueID().toString()))) {
			{
				boolean _setval = (false);
				entity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.Eaten = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
			if (entity instanceof LivingEntity) {
				((LivingEntity) entity).removePotionEffect(Effects.SLOWNESS);
			}
			if (entity instanceof LivingEntity) {
				((LivingEntity) entity).removePotionEffect(Effects.MINING_FATIGUE);
			}
			if (entity instanceof LivingEntity) {
				((LivingEntity) entity).removePotionEffect(Effects.INVISIBILITY);
			}
		}
	}
}
