import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * save or load a result
 * @author 413
 *
 */
public class ServiceResult
{

	/**
	 * save the job into a job.xml file
	 * @param result
	 */
	public void save(Result result)
	{
		//XML elements
		Element resultRoot = new Element("Result");
		Element EGeneralError = new Element("GeneralError");
		Element EPageProcessCount = new Element("PageProcessCount");
		Element EPageErrorCount = new Element("PageErrorCount");
		Element EErrorList = new Element("ErrorList");
		Element EStartJobDateTime = new Element("StartJobDateTime");
		Element EEndJobDateTime = new Element("EndJobDateTime");
		resultRoot.addContent(EGeneralError);
		resultRoot.addContent(EPageProcessCount);
		resultRoot.addContent(EPageErrorCount);
		resultRoot.addContent(EErrorList);
		resultRoot.addContent(EStartJobDateTime);
		resultRoot.addContent(EEndJobDateTime);
		
		org.jdom.Document saveDoc = new Document(resultRoot);
		
		DefaultListModel<Error> errors = result.getErrors();
		for(int i = 0; i < errors.getSize(); i++)
	    {
			Error error = errors.getElementAt(i);
			Element epn = new Element("ErrorPageNumber");
			Element ed = new Element("ErrorDescription");
			Element lvl = new Element("Level");
			Element screen = new Element("Screen");
			Element screen_Exist = new Element("Screen_Exist");
			epn.setText(error.getErrorPageNumber()+"");
			ed.setText(error.getErrorDescription());
			lvl.setText(error.getLevel()+"");
			screen_Exist.setText(error.isScreen_Exist()+"");
			if(error.isScreen_Exist())
			{
				screen.setText(error.getScreen().getName());
			}
			Element e = new Element("Error");
			e.addContent(epn);
			e.addContent(ed);
			e.addContent(lvl);
			EErrorList.addContent(e);
	    }
		
		EGeneralError.setText(result.getGeneralError());
		EPageProcessCount.setText(result.getPageProcessCount()+"");
		EPageErrorCount.setText(result.getPageErrorCount()+"");
		EStartJobDateTime.setText(result.getStartJobDateTime()+"");
		EEndJobDateTime.setText(result.getEndJobDateTime()+"");
		try
		{
			XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
			FileOutputStream out = new FileOutputStream(result.getResultFile());
			sortie.output(saveDoc, out);
			out.close();
		}
		catch (java.io.IOException e)
		{
			System.out.println("an error occurred while saving a result file : "+e.getMessage());
		}
		
	}

	/**
	 * load the Result from job.xml file
	 * @param source
	 * @return the Result loaded
	 */
	public Result load(File source)
	{
		Result ret = new Result();
		SAXBuilder sxb = new SAXBuilder();
		try
		{
			org.jdom.Document loadDoc = sxb.build(source);
			Element rootload = loadDoc.getRootElement();
			ret.setGeneralError(rootload.getChild("GeneralError").getText());
			ret.setPageProcessCount(Integer.parseInt(rootload.getChild("PageProcessCount").getText()));
			ret.setPageErrorCount(Integer.parseInt(rootload.getChild("PageErrorCount").getText()));
			//ret.setStartJobDateTime(new Date(rootload.getChild("StartJobDateTime").getText()));
			//ret.setEndJobDateTime(new Date(rootload.getChild("EndJobDateTime").getText()));
			@SuppressWarnings("unchecked")
			List<Element> error = rootload.getChild("ErrorList").getChildren("Error");
			Iterator<Element> i = error.iterator();
			while(i.hasNext())
			{
				Element courant = (Element)i.next();
				if(courant.getChild("Screen_Exist").getText()=="true")
				{
					ret.addError(Integer.parseInt(courant.getChild("ErrorPageNumber").getText()), courant.getChild("ErrorDescription").getText(), Integer.parseInt(courant.getChild("Level").getText()), new File(courant.getChild("Screen").getText()));
				}
				else
				{
					ret.addError(Integer.parseInt(courant.getChild("ErrorPageNumber").getText()), courant.getChild("ErrorDescription").getText(), Integer.parseInt(courant.getChild("Level").getText()));
				}
			}
		}
		catch(java.io.IOException | JDOMException e)
		{
			System.out.println("an error occurred while loading the file "+source.getPath()+" : "+e.getMessage());
		} 
		return ret;

	}
}
