package common.potion;

import java.util.Map;
import common.collect.Maps;
import common.color.TextColor;
import common.entity.DamageSource;
import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityPotion;
import common.entity.types.EntityLiving;
import common.vars.Vars;

public enum Potion {
    SPEED("speed", "Schnelligkeit", "Trank der Schnelligkeit", false, 8171462) {
    	public String getTooltip(int amp) {
    		return String.format(TextColor.BLUE + "+%d%% Geschwindigkeit", 20 * (amp + 1));
    	}
    },
    SLOWNESS("slowness", "Langsamkeit", "Trank der Langsamkeit", true, 5926017) {
    	public String getTooltip(int amp) {
    		return String.format(TextColor.RED + "-%d%% Geschwindigkeit", 15 * (amp + 1));
    	}
    },
    HASTE("haste", "Eile", "Trank der Eile", false, 14270531) {
		public double getEffectiveness() {
			return 1.5;
		}
    },
    FATIGUE("mining_fatigue", "Abbaulähmung", "Trank der Trägheit", true, 4866583),
    STRENGTH("strength", "Stärke", "Trank der Stärke", false, 9643043) {
    	public String getTooltip(int amp) {
    		return String.format(TextColor.BLUE + "+%d%% Angriffsschaden", 50 * (amp + 1));
    	}
    },
    HEAL("health", "Direktheilung", "Trank der Heilung", false, 16262179) {
		public boolean isInstant() {
			return true;
		}
		
		public void onImpact(EntityPotion potion, EntityLiving thrower, EntityLiving entity, int amp, double effect) {
			if(entity.arePotionsInverted()) {
				if(Vars.damagePotion) {
					int dmg = (int)(effect * (double)(6 << amp) + 0.5D);
					if(potion == null)
						entity.attackEntityFrom(DamageSource.magic, dmg);
					else
						entity.attackEntityFrom(DamageSource.causeIndirectMagicDamage(potion, thrower), dmg);
				}
			}
			else {
				entity.heal(Math.max((int)(effect * (double)(4 << amp) + 0.5D), 0));
			}
		}
    },
    DAMAGE("damage", "Direktschaden", "Trank des Schadens", true, 4393481) {
		public boolean isInstant() {
			return true;
		}
		
		public void onImpact(EntityPotion potion, EntityLiving thrower, EntityLiving entity, int amp, double effect) {
			if(!entity.arePotionsInverted()) {
				if(Vars.damagePotion) {
					int dmg = (int)(effect * (double)(6 << amp) + 0.5D);
					if(potion == null)
						entity.attackEntityFrom(DamageSource.magic, dmg);
					else
						entity.attackEntityFrom(DamageSource.causeIndirectMagicDamage(potion, thrower), dmg);
				}
			}
			else {
				entity.heal(Math.max((int)(effect * (double)(4 << amp) + 0.5D), 0));
			}
		}
    },
    JUMP("jump_boost", "Sprungkraft", "Trank der Sprungkraft", false, 2293580),
    NAUSEA("nausea", "Übelkeit", "Trank der Übelkeit", true, 5578058) {
		public double getEffectiveness() {
			return 0.25;
		}
    },
    REGENERATION("regeneration", "Regeneration", "Trank der Regeneration", false, 13458603) {
		public void onUpdate(EntityLiving entity, int duration, int amp) {
			int k = 50 >> amp;
			if((k <= 0 || duration % k == 0) && entity.getHealth() < entity.getMaxHealth())
				entity.heal(1);
		}

		public double getEffectiveness() {
			return 0.25;
		}
    },
    RESISTANCE("resistance", "Resistenz", "Trank des Widerstandes", false, 10044730),
    FIRE_RESISTANCE("fire_resistance", "Feuerschutz", "Trank der Feuerresistenz", false, 14981690),
    MANA_GENERATION("mana_generation", "Manaschub", "Trank des Manaschubes", false, 3035801) {
		public void onUpdate(EntityLiving entity, int duration, int amp) {
			if(!(entity instanceof EntityNPC npc))
				return;
			int k = 40 >> amp;
			if((k <= 0 || duration % k == 0) && npc.getManaPoints() < npc.getMaxMana())
				npc.healMana(1);
		}
    },
    FLYING("flying", null, null, false, 8356754) {
    	public String getDisplay(int amplifier) {
    		return amplifier <= 0 ? "Schweben" : "Flugkraft";
    	}

    	public String getPotionDisplay(int amplifier) {
    		return amplifier <= 0 ? "Trank des Schwebens" : "Trank der Flugkraft";
    	}
    },
    BLINDNESS("blindness", "Blindheit", "Trank der Blindheit", true, 2039587) {
		public double getEffectiveness() {
			return 0.25;
		}
    },
    NIGHT_VISION("night_vision", "Nachtsicht", "Trank der Nachtsicht", false, 2039713),
    STABILITY("stability", "Stabilität", "Trank der Standfestigkeit", false, 5797459),
    WEAKNESS("weakness", "Schwäche", "Trank der Schwäche", true, 4738376) {
    	public String getTooltip(int amp) {
    		return String.format(TextColor.RED + "-%d%% Angriffsschaden", 20 * (amp + 1));
    	}
    },
    POISON("poison", "Vergiftung", "Trank der Vergiftung", true, 5149489) {
		public void onUpdate(EntityLiving entity, int duration, int amp) {
			int j = 25 >> amp;
			if((j <= 0 || duration % j == 0) && (entity.worldObj.client || Vars.damagePoison) && entity.getHealth() > 1)
				entity.attackEntityFrom(DamageSource.magic, 1);
		}

		public double getEffectiveness() {
			return 0.25;
		}
    },
    ABSORPTION("absorption", "Absorption", "Trank der Absorption", false, 2445989) {
		public void removeModifiers(EntityLiving entity, int amp) {
			entity.setAbsorptionAmount(entity.getAbsorptionAmount() - (4 * (amp + 1)));
		}

		public void addModifiers(EntityLiving entity, int amp) {
			entity.setAbsorptionAmount(entity.getAbsorptionAmount() + (4 * (amp + 1)));
		}
    },
    RADIATION("radiation", "Strahlung", "Radioaktiver Trank", true, 0x00ff00) {
		public void onUpdate(EntityLiving entity, int duration, int amp) {
			if(entity.ticksExisted % 20 == 0 && (entity.worldObj.client || Vars.damageRadiation)) // && entityLivingBaseIn.getHealth() > 1.0F)
				entity.attackEntityFrom(DamageSource.radiation, 1 + amp);
		}
    },
    MANA("mana", "Mana", "Manatrank", false, 0x0000ff) {
		public boolean isInstant() {
			return true;
		}
		
		public void onImpact(EntityPotion potion, EntityLiving thrower, EntityLiving entity, int amp, double effect) {
			if(entity instanceof EntityNPC npc)
				npc.healMana(Math.max((int)(effect * (double)(4 << amp) + 0.5D), 0));
		}
    };

	private static final Map<String, Potion> LOOKUP = Maps.newHashMap();
    
	private final String name;
	private final String effectDisplay;
	private final String potionDisplay;
	private final boolean bad;
	private final int color;

	static {
		for(Potion potion : values()) {
			LOOKUP.put(potion.name, potion);
		}
	}

	public static Potion getByName(String name) {
		return LOOKUP.get(name);
	}

	private Potion(String name, String effectDisplay, String potionDisplay, boolean bad, int color) {
		this.name = name;
		this.bad = bad;
		this.color = color;
		this.effectDisplay = effectDisplay;
		this.potionDisplay = potionDisplay;
	}

	public String getName() {
		return this.name;
	}

	public String toString() {
		return this.name;
	}

	public String getDisplay(int amplifier) {
		return this.effectDisplay + PotionHelper.getPotionPotency(amplifier);
	}

	public String getPotionDisplay(int amplifier) {
		return this.potionDisplay;
	}

	public boolean isBadEffect() {
		return this.bad;
	}

	public int getColor() {
		return this.color;
	}

	public void onUpdate(EntityLiving entity, int duration, int amp) {
	}

	public void onImpact(EntityPotion potion, EntityLiving thrower, EntityLiving entity, int amp, double effect) {
	}

	public boolean isInstant() {
		return false;
	}
	
	public String getTooltip(int amp) {
		return null;
	}

	public double getEffectiveness() {
		return this.bad ? 0.5 : 1.0;
	}

	public void removeModifiers(EntityLiving entity, int amp) {
	}

	public void addModifiers(EntityLiving entity, int amp) {
	}
}
