package edu.iastate.music.marching.attendance.servlets;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.iastate.music.marching.attendance.model.interact.DataTrain;
import edu.iastate.music.marching.attendance.model.store.ImportData;
import edu.iastate.music.marching.attendance.tasks.Export;
import edu.iastate.music.marching.attendance.tasks.Import;

public class TaskServlet extends AbstractBaseServlet {

	private enum Page {
		export, import_, export_daily;
	}

	private static final long serialVersionUID = 2390747813204817960L;

	private static final String SERVLET_PATH = "task";

	public static final String IMPORT_PARAM_STOREID = "datastore_id";

	private static final Logger LOG = Logger.getLogger(TaskServlet.class
			.getName());

	public static final String EXPORT_DATA_URL = pageToUrl(Page.export,
			SERVLET_PATH);

	public static final String IMPORT_DATA_URL = pageToUrl(Page.import_,
			SERVLET_PATH);

	private void doDailyExport(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {

			LOG.info("Performing the daily full data export.");

			DataTrain train = DataTrain.depart();

			// Check if we should perform an export based on preferences
			if (train.appData().get().isCronExportEnabled()) {

				Export.performExport();

			} else {
				LOG.info("Daily full data export currently disabled, doing nothing.");
			}

		} catch (Exception e) {
			LOG.log(Level.SEVERE,
					"Encountered exception doing daily data export", e);
		} finally {
			// Never retry, avoids infinite loops
			resp.sendError(200);
		}
	}

	private void doExport(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		LOG.info("Doing full data export.");

		try {

			Export.performExport();

		} catch (Exception e) {
			LOG.log(Level.SEVERE,
					"Encountered exception doing on-demand data export", e);
		} finally {
			// Never retry, avoids infinite loops
			resp.sendError(200);
		}
	}

	private void doImport(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		LOG.info("Doing full data import.");

		DataTrain dt = DataTrain.depart();

		long id = Long.parseLong(req.getParameter(IMPORT_PARAM_STOREID));
		ImportData importData = dt.data().getImportData(id);

		try {
			Import.performImport(importData);
		} catch (Exception e) {
			LOG.log(Level.SEVERE, "Encountered exception doing data import", e);
		}

		try {
			// Always delete the import data afterwards
			dt.data().removeImportData(importData);
		} finally {
			// Never retry, avoids infinite loops
			resp.sendError(200);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Page page = parsePathInfo(req.getPathInfo(), Page.class);

		if (page == null)
			ErrorServlet.showError(req, resp, 404);
		else
			switch (page) {
			case export:
				doExport(req, resp);
				break;
			case export_daily:
				doDailyExport(req, resp);
				break;
			case import_:
				doImport(req, resp);
				break;
			default:
				ErrorServlet.showError(req, resp, 404);
			}
	}
}

@@ -3097,62 +3097,393 @@
 ase 
-ex
+im
 port_
-daily:%0A%09%09%09%09doDailyExport(req, resp
+:%0A%09%09%09%09doImport(req, resp);%0A%09%09%09%09break;%0A%09%09%09default:%0A%09%09%09%09ErrorServlet.showError(req, resp, 404);%0A%09%09%09%7D%0A%09%7D%0A%09%0A%09@Override%0A%09protected void doGet(HttpServletRequest req, HttpServletResponse resp)%0A%09%09%09throws ServletException, IOException %7B%0A%09%09Page page = parsePathInfo(req.getPathInfo(), Page.class
 );%0A
+%0A
 %09%09
-%09%09break;
+if (page == null)%0A%09%09%09ErrorServlet.showError(req, resp, 404);%0A%09%09else%0A%09%09%09switch (page) %7B
 %0A%09%09%09
@@ -3483,39 +3483,44 @@
 ) %7B%0A%09%09%09case 
-im
+ex
 port_
+daily
 :%0A%09%09%09%09do
 Import(req, 
@@ -3499,34 +3499,39 @@
 rt_daily:%0A%09%09%09%09do
-Im
+DailyEx
 port(req, resp);

Enable GET requests for daily cron since that's what app engine uses