package studio.halbear.hem.procedures;

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

import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.Entity;

import java.util.Map;

public class AirboatRightClickedOnEntityProcedure {

	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 AirboatRightClickedOnEntity!");
			return;
		}
		if (dependencies.get("sourceentity") == null) {
			if (!dependencies.containsKey("sourceentity"))
				HemMod.LOGGER.warn("Failed to load dependency sourceentity for procedure AirboatRightClickedOnEntity!");
			return;
		}
		Entity entity = (Entity) dependencies.get("entity");
		Entity sourceentity = (Entity) dependencies.get("sourceentity");
		if (ItemTags.getCollection().getTagByID(new ResourceLocation("forge:hem_fossilfuel")).contains(
				((sourceentity instanceof LivingEntity) ? ((LivingEntity) sourceentity).getHeldItemMainhand() : ItemStack.EMPTY).getItem())) {
			(((sourceentity instanceof LivingEntity) ? ((LivingEntity) sourceentity).getHeldItemMainhand() : ItemStack.EMPTY)).shrink((int) 1);
			entity.getPersistentData().putDouble("Fuel", (entity.getPersistentData().getDouble("Fuel") + 5));
			if (sourceentity instanceof PlayerEntity && !sourceentity.world.isRemote()) {
				((PlayerEntity) sourceentity).sendStatusMessage(new StringTextComponent("Vehicle Fueled"), (true));
			}
		}
		if (!sourceentity.isSneaking() && !entity.isBeingRidden() || sourceentity.isPassenger() && (sourceentity.getRidingEntity()) == entity) {
			{
				double _setval = (entity.getPersistentData().getDouble("ID"));
				sourceentity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.PilotingVehicleID = _setval;
					capability.syncPlayerVariables(sourceentity);
				});
			}
			{
				double _setval = (entity.getPersistentData().getDouble("MetresTravelled"));
				sourceentity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.VehicleOdometerMetres = _setval;
					capability.syncPlayerVariables(sourceentity);
				});
			}
			{
				double _setval = (entity.getPersistentData().getDouble("Fuel"));
				sourceentity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.Fuel = _setval;
					capability.syncPlayerVariables(sourceentity);
				});
			}
			{
				double _setval = (entity.getPersistentData().getDouble("Pressure"));
				sourceentity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.PSI = _setval;
					capability.syncPlayerVariables(sourceentity);
				});
			}
			{
				double _setval = (entity.getPersistentData().getDouble("Heat"));
				sourceentity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.Heat = _setval;
					capability.syncPlayerVariables(sourceentity);
				});
			}
			{
				double _setval = (entity.getPersistentData().getDouble("Yaw"));
				sourceentity.getCapability(HemModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.VehicleYaw = _setval;
					capability.syncPlayerVariables(sourceentity);
				});
			}
		}
	}
}
