package studio.halbear.hem.procedures;

import studio.halbear.hem.particle.RobotDamageParticle;
import studio.halbear.hem.HemMod;

import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.World;
import net.minecraft.world.IWorld;
import net.minecraft.world.Explosion;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.entity.Entity;

import java.util.Map;

public class EmberleafMilitaryRobotLowerHalfOnEntityTickUpdateProcedure {

	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 EmberleafMilitaryRobotLowerHalfOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("x") == null) {
			if (!dependencies.containsKey("x"))
				HemMod.LOGGER.warn("Failed to load dependency x for procedure EmberleafMilitaryRobotLowerHalfOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("y") == null) {
			if (!dependencies.containsKey("y"))
				HemMod.LOGGER.warn("Failed to load dependency y for procedure EmberleafMilitaryRobotLowerHalfOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("z") == null) {
			if (!dependencies.containsKey("z"))
				HemMod.LOGGER.warn("Failed to load dependency z for procedure EmberleafMilitaryRobotLowerHalfOnEntityTickUpdate!");
			return;
		}
		if (dependencies.get("entity") == null) {
			if (!dependencies.containsKey("entity"))
				HemMod.LOGGER.warn("Failed to load dependency entity for procedure EmberleafMilitaryRobotLowerHalfOnEntityTickUpdate!");
			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");
		if (world instanceof ServerWorld) {
			((ServerWorld) world).spawnParticle(RobotDamageParticle.particle, x, y, z, (int) 1, 0, 1, 0, 0.3);
		}
		if (entity.getPersistentData().getDouble("expirationtimer") <= 0) {
			if (world instanceof ServerWorld) {
				((ServerWorld) world).spawnParticle(RobotDamageParticle.particle, x, y, z, (int) 25, 0, 1, 0, 0.3);
			}
			if (world instanceof ServerWorld) {
				((ServerWorld) world).spawnParticle(ParticleTypes.LARGE_SMOKE, x, y, z, (int) 25, 0, 1, 0, 0.3);
			}
			if (!entity.world.isRemote())
				entity.remove();
			if (world instanceof World && !((World) world).isRemote) {
				((World) world).createExplosion(null, (int) x, (int) y, (int) z, (float) 2, Explosion.Mode.BREAK);
			}
		} else {
			entity.getPersistentData().putDouble("expirationtimer", (entity.getPersistentData().getDouble("expirationtimer") - 1));
		}
		if (entity.getPersistentData().getDouble("expirationtimer") == 0 && !entity.getPersistentData().getBoolean("ticking")) {
			entity.getPersistentData().putBoolean("ticking", (true));
			entity.getPersistentData().putDouble("expirationtimer", (60 + 200 * Math.random()));
		}
	}
}
