package org.josast.AmsatList.Commande;

import java.io.File;
import java.util.Hashtable;
import java.util.List;
import java.util.logging.Logger;

import org.josast.AmsatList.generated.AmsatList;
import org.josast.AmsatList.generated.Satellites;
import org.josast.ModuleSatelliteDatabase.AmsatListFile;

public class executeCommande {

	private Logger log = Logger.getLogger(getClass().getName());

	public void execute(String inputRepository, String outputRepository, CommandeList cmd) {
		File inputRepertoire = new File(inputRepository);
		File outputRepertoire = new File(outputRepository);

		int i = 0;
		if (!inputRepertoire.isDirectory()) {
			log.severe(inputRepository + "is not a repository");
		}
		if (!outputRepertoire.exists()) {
			log.info("create " + inputRepository);
			if (!outputRepertoire.mkdir()) {
				log.severe(outputRepertoire + "is not created");
			}
		} else if (!outputRepertoire.isDirectory()) {
			log.severe(outputRepertoire + "is not a repository");
		}

		File[] inputsfiles = inputRepertoire.listFiles();
		if (inputsfiles == null) {
			log.severe(inputRepository + "Repository empty");
		} else {

			for (i = 0; i < inputsfiles.length; i++) {
				AmsatListFile amsatListfile = new AmsatListFile();
				AmsatList amsatList = amsatListfile.openAmsatList(inputsfiles[i].getAbsolutePath());
				List<Satellites> satellites = amsatList.getSatellites();
				if (satellites.size() != 1) {
					log.severe("Wrong number of satellite" + satellites.size());
				}

				Satellites sat = satellites.get(0);

				Hashtable<Integer, Commande> cmdhas = cmd.getHashtableCommande();
				Commande cmdex = cmdhas.get(sat.getNasaID());
				if (cmdex != null) {
					sat = cmdex.execute(sat);
					log.info("commande à exécuter");
					String fichOutSring = outputRepertoire + "\\AL_" + sat.getName()+".xml";
					amsatListfile.save(fichOutSring, amsatList);
				} else {
					log.info("pas de commande à exécuter");
				}

			}
		}

	}
	
	
	public void copie(String inputRepository, String outputRepository) {
		File inputRepertoire = new File(inputRepository);
		File outputRepertoire = new File(outputRepository);

		int i = 0;
		if (!inputRepertoire.isDirectory()) {
			log.severe(inputRepository + "is not a repository");
		}
		if (!outputRepertoire.exists()) {
			log.info("create " + inputRepository);
			if (!outputRepertoire.mkdir()) {
				log.severe(outputRepertoire + "is not created");
			}
		} else if (!outputRepertoire.isDirectory()) {
			log.severe(outputRepertoire + "is not a repository");
		}

		File[] inputsfiles = inputRepertoire.listFiles();
		if (inputsfiles == null) {
			log.severe(inputRepository + "Repository empty");
		} else {

			for (i = 0; i < inputsfiles.length; i++) {
				AmsatListFile amsatListfile = new AmsatListFile();
				AmsatList amsatList = amsatListfile.openAmsatList(inputsfiles[i].getAbsolutePath());
				List<Satellites> satellites = amsatList.getSatellites();
				if (satellites.size() != 1) {
					log.severe("Wrong number of satellite" + satellites.size());
				}
				Satellites sat = satellites.get(0);
				String fichOutSring = outputRepertoire + "\\AL_" + sat.getName()+".xml";			
				amsatListfile.save(fichOutSring, amsatList);
				

			}
		}

	}

}
