package at.medevit.ch.artikelstamm.elexis.common.preference;

import at.medevit.ch.artikelstamm.marge.Marge;
import ch.elexis.core.data.activator.CoreHub;
import ch.rgw.tools.Money;

public class MargePreference {
	
	private static Marge[] marges = null;
	
	public static Marge[] getMarges(){
		if (marges == null) {
			initMarges();
		}
		return marges;
	}
	
	public static void storeMargeConfiguration(){
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < marges.length; i++) {
			Marge m = marges[i];
			sb.append(m.getStartInterval() + "/" + m.getEndInterval() + "/" + m.getAddition());
			if (i != marges.length)
				sb.append("$");
		}
		CoreHub.globalCfg.set(PreferenceConstants.PREV_CSV_MARGE_STORAGE, sb.toString());
		CoreHub.globalCfg.flush();
	}
	
	private static void initMarges(){
		String margeStorageString = CoreHub.globalCfg.get(PreferenceConstants.PREV_CSV_MARGE_STORAGE, "0/0/0$0/0/0$0/0/0");
		String[] margeStorageEntry = margeStorageString.split("\\$");
		marges = new Marge[margeStorageEntry.length];
		for (int i = 0; i < margeStorageEntry.length; i++) {
			String entry = margeStorageEntry[i];
			String[] values = entry.split("/");
			if (values.length == 3) {
				marges[i] = new Marge();
				marges[i].setStartInterval(Double.parseDouble(values[0]));
				marges[i].setEndInterval(Double.parseDouble(values[1]));
				marges[i].setAddition(Double.parseDouble(values[2]));
			}
		}
	}
	
	public static Money calculateVKP(Money ekPreis){		
		double amount = ekPreis.getAmount();
		
		for (int i = 0; i < getMarges().length; i++) {
			Marge m = getMarges()[i];
			if (!m.isValid())
				continue;
			if (m.startInterval <= amount && m.endInterval >= amount) {
				double mult = 1 + (m.addition / 100);
				return new Money(amount * mult);
			}
		}
		
		return new Money();
	}
	
}

@@ -1252,32 +1252,38 @@
 tInterval(Double
+%0A%09%09%09%09%09
 .parseDouble(val
@@ -1288,16 +1288,44 @@
 alues%5B0%5D
+ == null ? %220.0%22 : values%5B0%5D
 ));%0A%09%09%09%09
@@ -1377,16 +1377,44 @@
 alues%5B1%5D
+ == null ? %220.0%22 : values%5B1%5D
 ));%0A%09%09%09%09
@@ -1463,16 +1463,44 @@
 alues%5B2%5D
+ == null ? %220.0%22 : values%5B2%5D
 ));%0A%09%09%09%7D

check if value is null before parsing