package fr.lteconsulting.hexacssmaven;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BinaryOperator;

import org.apache.maven.plugin.logging.Log;

/**
 * Loads a CSS file and a mapping file. Every selector in the css file are
 * changed to their mapped name. Selectors in the css file that are not used in
 * the mapping file can be pruned (optional).
 */
public class CssMapper
{
	/**
	 * Process an input file
	 * 
	 * @param inputFile
	 *            The input file
	 * @param mappingFile
	 *            The file containing mapping information (lines in the form of
	 *            <newName>:<oldName>)
	 */
	public static void processFile( String inputFile, String mappingFile, String outputFile, boolean doPrune, Log log ) throws IOException
	{
		Path mappingPath = Paths.get( mappingFile );

		Path inputPath = Paths.get( inputFile );
		String input = Files.lines( inputPath ).reduce( "", new BinaryOperator<String>()
		{
			@Override
			public String apply( String arg0, String arg1 )
			{
				return arg0 + arg1;
			}
		} );

		// list of used class names
		Set<String> usedClassNames = new HashSet<>();

		// replace class names
		input = replaceClassNames( input, mappingPath, usedClassNames, log );

		log.debug( usedClassNames.size() + " used css classes in the mapping file" );
		log.debug( "used css classes : " + usedClassNames );

		CssRewriter cssRewriter = new CssRewriter( usedClassNames, doPrune, log );
		input = cssRewriter.process( input );
		
		input += "\r\n// generated by HexaCss maven plugin, see http://www.lteconsulting.fr/hexacss";

		// write output
		Path outputPath = Paths.get( outputFile );
		BufferedWriter writer = Files.newBufferedWriter( outputPath, StandardOpenOption.CREATE );
		writer.append( input );
		writer.close();

		log.debug( "Wrote to file " + outputPath );
		log.debug( input );
	}

	private static String replaceClassNames( String input, Path mappingPath, Set<String> usedClassNames, Log log ) throws IOException
	{
		List<String> lines = Files.readAllLines( mappingPath );

		List<String[]> renames = new ArrayList<>();
		for( String line : lines )
			renames.add( line.split( "=" ) );

		Collections.sort( renames, new Comparator<String[]>()
		{
			@Override
			public int compare( String[] a, String[] b )
			{
				return ((Integer) b[1].length()).compareTo( a[1].length() );
			}
		} );

		for( String[] line : renames )
		{
			log.info( line[1] + " => " + line[0] );
			input = input.replaceAll( line[1], line[0] );
			usedClassNames.add( line[0] );
		}

		return input;
	}
}

@@ -1940,16 +1940,80 @@
 File );%0A
+%09%09if(Files.exists( outputPath ))%0A%09%09%09Files.delete( outputPath );%0A
 %09%09Buffer
@@ -2092,16 +2092,20 @@
 n.CREATE
+_NEW
  );%0A%09%09wr
@@ -2835,16 +2835,24 @@
 laceAll(
+ %22%5C%5C.%22 +
  line%5B1%5D
@@ -2852,16 +2852,22 @@
 line%5B1%5D,
+ %22.%22 +
  line%5B0%5D

fix - file wrongly written