package ch.elexis.core.ui.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.ui.util.SWTHelper;
import ch.elexis.core.ui.views.FallDetailView;
import ch.elexis.data.Fall;
import ch.elexis.data.Konsultation;
import ch.elexis.data.Patient;

public class FallCopyCommand extends AbstractHandler {
	private static Logger logger = LoggerFactory.getLogger(FallCopyCommand.class);
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException{
		IWorkbenchPage activePage =
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		FallDetailView fallDetailView = (FallDetailView) activePage.findView(FallDetailView.ID);
		Fall activeFall = fallDetailView.getActiveFall();
		Fall clone = createFallClone(activeFall);
		
		// only ask for consultation transfer if case is still open 
		if (activeFall.isOpen()) {
			Konsultation[] consultations = activeFall.getBehandlungen(true);
			if (consultations != null && consultations.length > 0) {
				boolean transferCons =
					SWTHelper.askYesNo(Messages.FallCopyCommand_RelatedConsultations,
						Messages.FallCopyCommand_TransferConsultations
							+ Messages.FallCopyCommand_AttentionTransferConsultations);
				if (transferCons) {
					logger.debug("trying to transfer consulations");
					for (Konsultation cons : consultations) {
						if (cons.isEditable(false)) {
							cons.setFall(clone);
						}
					}
				}
			}
		}
		return null;
	}
	
	private Fall createFallClone(Fall fall){
		Patient pat = fall.getPatient();
		Fall clone =
			pat.neuerFall(fall.getBezeichnung(), fall.getGrund(), fall.getAbrechnungsSystem());
		
		String[] fields =
			new String[] {
				Fall.FLD_GARANT_ID, Fall.FLD_FALL_NUMMER, Fall.FLD_RN_PLANUNG, Fall.FLD_RES,
				Fall.FLD_DATUM_VON, Fall.FLD_EXTINFO, Fall.FLD_XGESETZ
			};
		String[] values =
			new String[] {
				fall.getGarant().getId(), fall.getFallNummer(), fall.get(Fall.FLD_RN_PLANUNG),
				fall.get(Fall.FLD_RES), fall.getBeginnDatum(), fall.get(Fall.FLD_EXTINFO),
				fall.getAbrechnungsSystem()
			};
		clone.set(fields, values);
		clone = copyRequiredFields(fall, clone);
		
		return clone;
	}
	
	private Fall copyRequiredFields(Fall fall, Fall clone){
		String[] requiredFields = fall.getRequirements().split(";");
		for (String req : requiredFields) {
			String[] reqNameType = req.split(":");
			String reqName = reqNameType[0];
			String value = fall.getRequiredString(reqName);
			clone.setInfoString(reqName, value);
		}
		return clone;
	}
}

@@ -30,16 +30,68 @@
 mands;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import o
@@ -2381,32 +2381,24 @@
 clone = copy
-Required
 Fields(fall,
@@ -2448,24 +2448,16 @@
 all copy
-Required
 Fields(F
@@ -2481,24 +2481,23 @@
 one)%7B%0A%09%09
-String%5B%5D
+// copy
  require
@@ -2501,17 +2501,52 @@
 ired
-F
+ f
 ields
- = 
+%0A%09%09List%3CString%3E keys = loadFieldKeys(
 fall
@@ -2567,18 +2567,8 @@
 ts()
-.split(%22;%22
 );%0A%09
@@ -2584,198 +2584,509 @@
 ing 
-req : requiredFields) %7B%0A%09%09%09String%5B%5D reqNameType = req.split(%22:%22);%0A%09%09%09String reqName = reqNameType%5B0%5D;%0A%09%09%09String value = fall.getRequiredString(reqName);%0A%09%09%09clone.setInfoString(reqName, value
+key : keys) %7B%0A%09%09%09clone.setInfoString(key, fall.getRequiredString(key));%0A%09%09%7D%0A%09%09%0A%09%09// copy optional fields%0A%09%09keys = loadFieldKeys(fall.getOptionals());%0A%09%09for (String key : keys) %7B%0A%09%09%09clone.setInfoString(key, fall.getInfoString(key));%0A%09%09%7D%0A%09%09return clone;%0A%09%7D%0A%09%0A%09private List%3CString%3E loadFieldKeys(String fieldString)%7B%0A%09%09List%3CString%3E keys = new ArrayList%3CString%3E();%0A%09%09String%5B%5D fields = fieldString.split(%22;%22);%0A%09%09for (String field : fields) %7B%0A%09%09%09String%5B%5D nameType = field.split(%22:%22);%0A%09%09%09keys.add(nameType%5B0%5D
 );%0A%09
@@ -3093,24 +3093,23 @@
 %09%7D%0A%09%09return 
-clone
+keys
 ;%0A%09%7D%0A%7D%0A

copy optional fields as well when Fall is copied