package it.micegroup.voila2.cartridge.library.formatters;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public class PrettierFormatter {
	private String[] cmd;

	public PrettierFormatter() {
		cmd = new String[] {
			"cmd.exe" , "/c", "prettier",
				"--write",
				"--print-width", "180",
				"--tab-width", "4",
				"--trailing-comma", "none",
				"--single-quote",
				"--arrow-parens", "avoid",
				"--end-of-line", "crlf",
				"<PATH>"
		};
	}

	public String format(String input, String fileName) {
		String formattedOutput = input;
		File temp = null;
		try {
			temp = File.createTempFile("acceleo-"+ System.currentTimeMillis(), "." + FilenameUtils.getExtension(fileName));
			FileUtils.writeStringToFile(temp, input, "utf-8");

			cmd[cmd.length - 1] = temp.getAbsolutePath();

			ProcessBuilder builder = new ProcessBuilder(cmd);
		    Process p = builder.start();
			int result = p.waitFor();
			if (result != 0) {
				Scanner scanner = new Scanner(p.getErrorStream());
				while (scanner.hasNext()) {
				    System.err.println(scanner.nextLine());
				}
				scanner.close();
			} else {
				formattedOutput = FileUtils.readFileToString(temp, "utf-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally  {
			if (temp != null)
				FileUtils.deleteQuietly(temp);
		}
		return formattedOutput;
	}
	
	public void formatAll(File remoteDir) {
		try {
//			String[] paths = {
//					"\"**/src/app/entity/**/list*.+(html)\"", 
//					"\"**/src/app/entity/**/list*.+(ts)\"", 
//					"\"**/src/app/entity/**/detail*.+(html)\"", 
//					"\"**/src/app/entity/**/detail*.+(ts)\"", 
//					"\"**/src/app/entity/**/search*.+(html)\"", 
//					"\"**/src/app/entity/**/search*.+(ts)\"", 
//					"\"**/src/app/entity/**/tabs*.+(html)\"", 
//					"\"**/src/app/entity/**/tabs*.+(ts)\"", 
//					"\"**/src/*.+(html|ts)\"", 
//					"\"**/src/app/*.+(html|ts)\"", 
//					"\"**/src/app/criteria/*.+(html|ts)\"", 
//					"\"**/src/app/model/*.+(html|ts)\"", 
//					"\"**/src/app/entity/base_*/base*.+(html|ts)\"", 
//					"\"**/src/app/form/*.+(html|ts)\"", 
//					"\"**/src/app/home-component/**/*.+(html|ts)\"", 
//					"\"**/src/app/layout-component/**/*.+(html|ts)\"", 
//					"\"**/src/app/login-component/**/*.+(html|ts)\"", 
//					"\"**/src/app/security/**/*.+(html|ts)\"", 
//					"\"**/src/app/service/**/*.+(html|ts)\"", 
//					"\"**/src/**/*.+(js|xml)\""
//			};
			String[] paths = {
					"\"**-fe/**/*.+(html)\"", 
					"\"**-fe/**/*.+(ts)\"",
					"\"**-fe/**/*.+(json)\"",
					"\"**-fe/**/*.+(js|xml)\""
			};
			doFormat(paths, remoteDir);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doFormat(String[] paths, File remoteDir) throws Exception {
		Process[] processes = new Process[paths.length];
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < paths.length; i++) {
			cmd[cmd.length - 1] = paths[i];
			ProcessBuilder builder = new ProcessBuilder(cmd);
			builder.inheritIO();
			builder.directory(remoteDir);
		    processes[i] = builder.start();
		}
	    int result = 0;
		for (int i = 0; i < paths.length; i++) {
		    result += processes[i].waitFor();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("doformat Millisecondi per formattare: " + (endTime - startTime));

		if (result != 0) {
			for (int i = 0; i < paths.length; i++) {
				Scanner scanner = new Scanner(processes[i].getErrorStream());
				while (scanner.hasNext()) {
				    System.err.println(scanner.nextLine());
				}
				scanner.close();
			}
		}
	}
}
