package org.nusco.narjillos.core.chemistry;

import static org.nusco.narjillos.core.chemistry.Element.*;

import java.util.LinkedHashMap;
import java.util.Map;

import org.nusco.narjillos.core.utilities.Configuration;

public class Atmosphere {

	private final double saturationElementLevels;
	private final Map<Element, Double> levels = new LinkedHashMap<>();
	private final int catalystLevel;
	
	public Atmosphere() {
		this(Configuration.ECOSYSTEM_INITIAL_ELEMENT_LEVEL, Configuration.ECOSYSTEM_CATALYST_LEVEL);
	}
	
	public Atmosphere(double initialElementLevels, int catalystLevel) {
		this.saturationElementLevels = initialElementLevels * 3;
		levels.put(OXYGEN, initialElementLevels);
		levels.put(HYDROGEN, initialElementLevels);
		levels.put(NITROGEN, initialElementLevels);
		this.catalystLevel = catalystLevel;
	}

	public synchronized double getAmountOf(Element element) {
		if (element == ZERO)
			return 0;
		
		return levels.get(element);
	}

	public int getCatalystLevel() {
		return catalystLevel;
	}

	public synchronized void convert(Element fromElement, Element toElement) {
		if (fromElement == ZERO || toElement == ZERO)
			return;

		double fromLevel = levels.get(fromElement);
		if (fromLevel > 0) {
			Double toLevel = levels.get(toElement);
			levels.put(fromElement, fromLevel - 1);
			levels.put(toElement, toLevel + 1);
		}
	}

	public synchronized double getDensityOf(Element element) {
		if (element == ZERO)
			return 0.0;

		return ((double) levels.get(element)) / saturationElementLevels;
	}

	public Atmosphere duplicate() {
		Atmosphere result = new Atmosphere();
		result.levels.put(OXYGEN, levels.get(OXYGEN));
		result.levels.put(HYDROGEN, levels.get(HYDROGEN));
		result.levels.put(NITROGEN, levels.get(NITROGEN));
		return result;
	}
	
	@Override
	public synchronized String toString() {
		return "O: " + levels.get(OXYGEN) + ", H: " + levels.get(HYDROGEN) + ", N: " + levels.get(NITROGEN);
	}
}

@@ -1522,257 +1522,8 @@
 ;%0A%09%7D
-%0A%0A%09public Atmosphere duplicate() %7B%0A%09%09Atmosphere result = new Atmosphere();%0A%09%09result.levels.put(OXYGEN, levels.get(OXYGEN));%0A%09%09result.levels.put(HYDROGEN, levels.get(HYDROGEN));%0A%09%09result.levels.put(NITROGEN, levels.get(NITROGEN));%0A%09%09return result;%0A%09%7D
 %0A%09%0A%09
@@ -1674,15 +1674,46 @@
 ITROGEN)
+ + %22, X: %22 + getCatalystLevel()
 ;%0A%09%7D%0A%7D%0A

Fix Atmosphere#toString()