package ch.elexis.core.ui.medication.handlers;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import ch.elexis.core.model.IEncounter;
import ch.elexis.core.model.IPatient;
import ch.elexis.core.model.IPrescription;
import ch.elexis.core.model.prescription.EntryType;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.services.holder.EncounterServiceHolder;
import ch.elexis.core.text.model.Samdas;
import ch.elexis.core.text.model.Samdas.Record;
import ch.elexis.core.ui.medication.views.MedicationTableViewerItem;

public class MentionInConsultationHandler extends AbstractHandler {
	@SuppressWarnings("unchecked")
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException{
		ISelection selection =
			HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		if (selection != null && !selection.isEmpty()) {
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			List<IPrescription> prescriptions = new ArrayList<IPrescription>();
			List<MedicationTableViewerItem> mtvItems = strucSelection.toList();
			for (MedicationTableViewerItem mtvItem : mtvItems) {
				IPrescription p = mtvItem.getPrescription();
				if (p != null) {
					prescriptions.add(p);
				}
			}
			if (!prescriptions.isEmpty()) {
				IPatient patient = prescriptions.get(0).getPatient();
				Optional<IEncounter> encounter =
					EncounterServiceHolder.get().getLatestEncounter(patient);
				encounter.ifPresent(enc -> {
					StringBuilder sb = new StringBuilder();
					for (IPrescription presc : prescriptions) {
						String articleLabel = "";
						if (presc.getArticle() != null) {
							articleLabel = presc.getArticle().getLabel();
						}
						sb.append("\n");
						sb.append("Medikation: " + articleLabel + ", "
							+ presc.getDosageInstruction() + " " + getType(presc.getEntryType()));
					}
					
					Samdas samdas = new Samdas(enc.getVersionedEntry().getHead());
					Record rec = samdas.getRecord();
					String recText = rec.getText();
					recText += sb.toString();
					rec.setText(recText);
					EncounterServiceHolder.get().updateVersionedEntry(enc, samdas);
					CoreModelServiceHolder.get().save(enc);
				});
			}
		}
		
		return null;
	}
	
	private String getType(EntryType entryType){
		switch (entryType) {
		case FIXED_MEDICATION:
			return "(Fixmedikation)";
		case RESERVE_MEDICATION:
			return "(Reservemedikation)";
		case SELF_DISPENSED:
			return "(Dispensiert)";
		case RECIPE:
			return "(Rezeptiert)";
		default:
			return "";
		}
	}
}

@@ -413,24 +413,72 @@
 ndlerUtil;%0A%0A
+import ch.elexis.core.common.ElexisEventTopics;%0A
 import ch.el
@@ -638,16 +638,76 @@
 ryType;%0A
+import ch.elexis.core.services.holder.ContextServiceHolder;%0A
 import c
@@ -2635,16 +2635,96 @@
 e(enc);%0A
+%09%09%09%09%09ContextServiceHolder.get().postEvent(ElexisEventTopics.EVENT_UPDATE, enc);%0A
 %09%09%09%09%7D);%0A

add update event to medication MentionInConsultationHandler