package cz.vutbr.feec.libraryClasses;

import java.io.File;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;

import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
import com.sun.xml.internal.ws.util.Pool.Marshaller;
import com.thoughtworks.xstream.XStream;

import cz.vutbr.feec.project.FileUtil;
import cz.vutbr.feec.stockElements.RackCell;

public class XMLmethods {
	

	/**
	 * Loads person data from the specified file. The current person data will
	 * be replaced.
	 * 
	 * @param file
	 */
	@SuppressWarnings("unchecked")
	public static void loadPersonDataFromFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("reckCell", RackCell.class);

		try {
			String xml = FileUtil.readFile(file);

			ArrayList<RackCell> reckList = (ArrayList<RackCell>) xstream
					.fromXML(xml);

			ArrayList<RackCell> reckData = null;
			reckData.clear();
			reckData.addAll(reckList);

		} catch (Exception e) { // catches ANY exception
			/*
			 * Dialogs.showErrorDialog(primaryStage,
			 * "Could not load data from file:\n" + file.getPath(),
			 * "Could not load data", "Error", e);
			 */
		}
	}

	/**
	 * Saves the current person data to the specified file.
	 * 
	 * @param file
	 */
	public static void savePersonDataToFile(File file) {
		XStream xstream = new XStream();
		xstream.alias("reckCell", RackCell.class);

		// Convert ObservableList to a normal ArrayList
		RackCell r1 = new RackCell(4, 4);
		RackCell r2 = new RackCell(4, 5);

		// ArrayList<ReckCell> personList = new ArrayList<>(personData);
		ArrayList<RackCell> reckList = new ArrayList<RackCell>();
		reckList.add(r1);
		reckList.add(r2);
		String xml = xstream.toXML(reckList);
		try {
			FileUtil.saveFile(xml, file);

		} catch (Exception e) { // catches ANY exception
			/*
			 * Dialogs.showErrorDialog(primaryStage,
			 * "Could not save data to file:\n" + file.getPath(),
			 * "Could not save data", "Error", e);
			 */
		}

	}

}
