package org.josast.app.amicalsat.cli.option;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.josast.app.amicalsat.cli.AmicalsatCliConf;

public class InitialiseTextualFileCmd implements Cmd {
	private Logger log = Logger.getLogger(getClass().getName());
	private Path filepath = null;

	@Override
	public void excute() {
		AmicalsatCliConf conf = AmicalsatCliConf.getInstance();

		if (conf.isGenerateTextualOutputFile()) {

			Path path = Paths.get(conf.getOutputTextualFileRepository());
			// if directory exists?
			if (!Files.exists(path)) {
				try {
					Files.createDirectories(path);
					log.info("directory created : " + path);
				} catch (IOException e) {
					log.log(Level.SEVERE, "cannot create outpout directorie " + e.toString());
				}
			}

			String creationdate = "";
			if (conf.isTextualFilePrefixDate()) {
				SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
				Date aujourdhui = new Date();
				creationdate = formater.format(aujourdhui) + "-";
			}

			filepath = Paths.get(conf.getOutputTextualFileRepository() + "\\" + creationdate + conf.getTextualFile());
			try {
				Files.writeString(filepath, "Output file \r\n");
			} catch (IOException e) {
				log.severe("cannot write to outpout file " + e.toString());
			}
			conf.setRealTextualFilePath(filepath);

		}
	}

}
