package ch.elexis.core.ui.dbcheck.contributions;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.eclipse.core.runtime.IProgressMonitor;

import ch.elexis.core.l10n.Messages;
import ch.elexis.core.model.IDocumentLetter;
import ch.elexis.core.model.util.DocumentLetterUtil;
import ch.elexis.core.services.IQuery;
import ch.elexis.core.services.IQueryCursor;
import ch.elexis.core.services.IVirtualFilesystemService.IVirtualFilesystemHandle;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
import ch.elexis.core.ui.dbcheck.external.ExternalMaintenance;

public class MoveAllLettersToExternalStorage extends ExternalMaintenance {
	
	public MoveAllLettersToExternalStorage(){}
	
	@Override
	public String executeMaintenance(IProgressMonitor pm, String DBVersion){
		
		StringBuilder result = new StringBuilder();
		int okCount = 0;
		int failCount = 0;
		IQuery<IDocumentLetter> query =
			CoreModelServiceHolder.get().getQuery(IDocumentLetter.class);
		try (IQueryCursor<IDocumentLetter> letters = query.executeAsCursor()) {
			pm.beginTask(Messages.Texterstellung_save_all_letters_externally, letters.size());
			while (letters.hasNext()) {
				IDocumentLetter letter = letters.next();
				try {
					boolean ok = exportToExtern(letter, result);
					if (ok) {
						okCount++;
					} else {
						failCount++;
						result.append("[" + letter.getId() + "] could not export: see log\n");
					}
					pm.worked(1);
				} catch (IOException e) {
					failCount++;
					result.append(
						"[" + letter.getId() + "] could not export: " + e.getMessage() + "\n");
				}
			}
		}
		pm.done();
		return okCount + " OK / " + failCount + " FAIL\n" + result.toString();
	}
	
	@Override
	public String getMaintenanceDescription(){
		return "[EXPLETT] Export all letters from DB storage to external storage";
	}
	
	/**
	 * Try to save the {@link IDocumentLetter} on external storage. Extern file configuration has to
	 * be valid.
	 * @param result 
	 * 
	 * @param brief
	 * @return
	 * @throws IOException
	 */
	public boolean exportToExtern(IDocumentLetter letter, StringBuilder result) throws IOException{
		IVirtualFilesystemHandle externalHandle =
			DocumentLetterUtil.getExternalHandleIfApplicable(letter);
		if (externalHandle != null && externalHandle.exists() && externalHandle.canRead()) {
			return true;
		} else {
			byte[] content = null;
			try (InputStream letterInputStream = letter.getContent()) {
				if (letterInputStream != null) {
					content = IOUtils.toByteArray(letterInputStream);
				} else {
					result.append("[" + letter.getId() + "] content is null\n");
				}
			}
			if (content != null) {
				try (InputStream is = new ByteArrayInputStream(content)) {
					letter.setContent(is);
				}
			}
			
			return true;
		}
	}
	
}

@@ -224,16 +224,132 @@
 Monitor;
+%0Aimport org.eclipse.jface.dialogs.MessageDialog;%0Aimport org.eclipse.swt.SWT;%0Aimport org.eclipse.swt.widgets.Display;
 %0A%0Aimport
@@ -847,16 +847,45 @@
 nce %7B%0A%09%0A
+%09private int whichLetters;%0A%09%0A
 %09public 
@@ -1007,16 +1007,313 @@
 rsion)%7B%0A
+%09%09Display.getDefault().syncExec(() -%3E %7B%0A%09%09%09whichLetters = MessageDialog.open(MessageDialog.QUESTION,%0A%09%09%09%09Display.getDefault().getActiveShell(), %22Export letters%22,%0A%09%09%09%09%22Select which letters to export.%22, SWT.SHEET, %22All letters%22, %22Only templates%22);%0A%09%09%7D);%0A%09%09boolean onlyTemplates = whichLetters == 1;%0A
 %09%09%0A%09%09Str
@@ -1728,16 +1728,16 @@
 next();%0A
-
 %09%09%09%09try 
@@ -1738,16 +1738,128 @@
 %09%09try %7B%0A
+%09%09%09%09%09if (onlyTemplates && !letter.isTemplate()) %7B%0A%09%09%09%09%09%09// skip all non template letters%0A%09%09%09%09%09%09continue;%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%09boo

add option to export letter templates only dbcheck tool