package org.josast.ModuleSatelliteDatabase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.ListIterator;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.josast.AmsatList.generated.AmsatList;
import org.josast.AmsatList.generated.ObjectFactory;
import org.josast.AmsatList.generated.Satellites;

/**
 * Classe Java for mergin AMSAT-LIST Xml file
 * 
 * Concept : read all files from a directory add this file data in memory save
 * all data in one file
 * 
 * @author chris
 *
 */

public class AppCli {

	private static String version = "0.7";
	AmsatList amsatList = null;
	List<Satellites> liste = null;



	private static Options configParameters() {

		final Option versionOption = Option.builder("V")

				.longOpt("Version") //

				.desc("Return application version")

				.hasArg(false)

				.required(false)

				.build();

		final Option directoryOption = Option.builder("D")

				.longOpt("Directory") //

				.desc("give directory with all files to merge")

				.hasArg(true)

				.argName("Repository")

				.required(false)

				.build();

		final Option outputFileOption = Option.builder("O")

				.longOpt("OutputFile") //

				.desc("output file wherre all files are merged")

				.hasArg(true)

				.argName("OutputFile")

				.required(false)

				.build();

		final Option helpFileOption = Option.builder("h")

				.longOpt("help")

				.desc("display help message")

				.build();

		final Options options = new Options();

		options.addOption(versionOption);

		options.addOption(directoryOption);
		options.addOption(outputFileOption);
		options.addOption(helpFileOption);

		return options;

	}



	public static void main(String[] args) throws ParseException {

		final Options options = configParameters();

		final CommandLineParser parser = new DefaultParser();

		final CommandLine line = parser.parse(options, args);

		// Si mode aide

		boolean helpMode = line.hasOption("help");

		if (helpMode) {

			final HelpFormatter formatter = new HelpFormatter();

			formatter.printHelp(args[0], options, true);

			System.exit(0);

		}
		boolean versionpMode = line.hasOption("Version");

		if (versionpMode) {

			System.out.println(args[0] + version);

			System.exit(0);

		}

		boolean configMode = line.hasOption("Directory");

		if (configMode) {

			String OutputFile = line.getOptionValue("OutputFile", "");
			String Repository = line.getOptionValue("Directory", "");
			System.out.println("Repository " + Repository + " Output File" + OutputFile);
			
			
//			AppCli merge = new AppCli();
//			merge.createAmsatList();
//			merge.merge(Repository, OutputFile);
			
			
			MergeAmsatListFile merge = new MergeAmsatListFile();
			merge.merge(Repository, OutputFile);
			merge.save(OutputFile);
		
		}
		

	}

}
