package org.pfaa.chemica.model;

import java.awt.Color;

import org.pfaa.chemica.model.ChemicalStateProperties.Aqueous;
import org.pfaa.chemica.model.ChemicalStateProperties.Gas;
import org.pfaa.chemica.model.ChemicalStateProperties.Liquid;
import org.pfaa.chemica.model.ChemicalStateProperties.Solid;
import org.pfaa.chemica.model.Compound.Compounds;


public class SimpleChemical implements Chemical {

	private Formula formula;
	private String oreDictKey;
	private Fusion fusion;
	private Vaporization vaporization;
	
	private ChemicalStateProperties solid;
	private ChemicalStateProperties liquid;
	private ChemicalStateProperties gas;
	private ChemicalStateProperties aqueous;
	
	public SimpleChemical(Formula formula, String oreDictKey, Solid solid) {
		this(formula, oreDictKey, solid, null, null, null, null, null);
	}
	public SimpleChemical(Formula formula, Aqueous aqueous) {
		this(formula, null, null, null, null, null, null, aqueous);
	}
	public SimpleChemical(Formula formula, String oreDictKey, Solid solid, Fusion fusion, 
			Liquid liquid,	Vaporization vaporization, Gas gas) {
		this(formula, oreDictKey, solid, fusion, liquid, vaporization, gas, null);
	}
	
	public SimpleChemical(Formula formula, String oreDictKey, Solid solid, Fusion fusion, 
			Liquid liquid,	Vaporization vaporization, Gas gas, Aqueous aqueous) 
	{
		this.formula = formula;
		this.oreDictKey = oreDictKey == null ? formula.toString() : oreDictKey;
		this.solid = solid;
		this.fusion = fusion;
		this.liquid = liquid;
		this.vaporization = vaporization;
		this.gas = gas == null ? null : new Gas(gas, formula.getMolarMass());
		this.aqueous = aqueous == null ? this.inferAqueous() : aqueous;
	}
	
	@Override
	public String getOreDictKey() {
		return oreDictKey;
	}

	@Override
	public Fusion getFusion() {
		return fusion;
	}

	@Override
	public Vaporization getVaporization() {
		return vaporization;
	}

	@Override
	public Formula getFormula() {
		return formula;
	}
	
	public ChemicalStateProperties getStateProperties(State state) {
		switch(state) {
		case SOLID:
			return solid;
		case LIQUID:
			return liquid;
		case GAS:
			return gas;
		case AQUEOUS:
			return aqueous;
		default:
			throw new IllegalArgumentException("Unknown state: " + state);
		}
	}
	
	@Override
	public String name() {
		return formula.toString();
	}
	
	@Override
	public Mixture mix(IndustrialMaterial material, double weight) {
		return new SimpleMixture(this).mix(material, weight);
	}
	
	private boolean isWaterSolubleSalt() {
		Reaction dissolution = this.getDissociation();
		if (dissolution == null) {
			return false;
		}
		int et = dissolution.getEquilibriumTemperature();
		return et > Compounds.H2O.getFusion().getTemperature() && et < Compounds.H2O.getVaporization().getTemperature();
	}
	
	private Aqueous inferAqueous() {
		if (!isWaterSolubleSalt()) {
			return null;
		}
		Ion cation = this.getFormula().getCation();
		Ion anion = this.getFormula().getAnion();
		ConditionProperties cationProps = cation.getProperties(Condition.STP);
		ConditionProperties anionProps = anion.getProperties(Condition.STP);
		Color color = cationProps.color;
		if (color == null) 
			color = anionProps.color;
		double enthalpy = cationProps.thermo.enthalpy + anionProps.thermo.enthalpy;
		double entropy = cationProps.thermo.entropy + anionProps.thermo.entropy;
		Thermo thermo = new Thermo(enthalpy, entropy);
		Hazard hazard = this.getStateProperties(State.SOLID).getHazard();
		return new Aqueous(color, thermo, hazard);
	}
}

@@ -299,58 +299,8 @@
 id;%0A
-import org.pfaa.chemica.model.Compound.Compounds;%0A
 %0A%0Apu
@@ -2411,320 +2411,8 @@
 %0A%09%7D%0A
-%09%0A%09private boolean isWaterSolubleSalt() %7B%0A%09%09Reaction dissolution = this.getDissociation();%0A%09%09if (dissolution == null) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09int et = dissolution.getEquilibriumTemperature();%0A%09%09return et %3E Compounds.H2O.getFusion().getTemperature() && et %3C Compounds.H2O.getVaporization().getTemperature();%0A%09%7D%0A%09
 %0A%09pr
@@ -2453,27 +2453,36 @@
 f (!
-isWaterSolubleSalt(
+this.isSoluble(Condition.STP
 )) %7B
@@ -2592,16 +2592,77 @@
 nion();%0A
+%09%09if (cation == null %7C%7C anion == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%09Condit

use standard isSoluble() check