package ca.concordia.cssanalyser.migration.topreprocessors.sass;

import ca.concordia.cssanalyser.cssmodel.StyleSheet;
import ca.concordia.cssanalyser.io.IOHelper;
import ca.concordia.cssanalyser.parser.CSSParserFactory;
import io.bit3.jsass.Compiler;
import io.bit3.jsass.Options;
import io.bit3.jsass.Output;
import io.bit3.jsass.importer.Import;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;

public class SassHelper {

    public static StyleSheet compileSassFile(String filePath) throws Exception {

        String cssFileContents = IOHelper.readFileToString(filePath);

        Compiler compiler = new Compiler();
        Options options = new Options();
        options.setImporters(Collections.singleton((path, previousImport) -> resolveImport(path, previousImport, filePath)));

        Output output = compiler.compileString(cssFileContents, options);
        String cssString = output.getCss();
        return CSSParserFactory.getCSSParser(CSSParserFactory.CSSParserType.LESS).parseCSSString(cssString);

    }

    /**
     * Implements a {@link io.bit3.jsass.importer.Importer}
     *
     * @param importPath
     * @param previousImport
     * @param rootSassFilePath
     * @return
     */
    private static Collection<Import> resolveImport(String importPath, Import previousImport, String rootSassFilePath) {
        Path previousImportPath;
        String previousImportPathString = previousImport.getAbsoluteUri().getPath();
        if ("stdin".equals(previousImportPathString)) {
            previousImportPath = Paths.get(rootSassFilePath).getParent();
        } else {
            previousImportPath = Paths.get(previousImportPathString).getParent();
        }

        try {
            Path path = previousImportPath.resolve(importPath);
            File resource = resolveSassFile(path);

            if (null == resource) {
                throw new FileNotFoundException(importPath);
            }

            final URI uri = new URI(resource.getAbsolutePath());
            final String source = IOHelper.readFileToString(resource.getPath());

            final Import scssImport = new Import(uri, uri, source);
            return Collections.singleton(scssImport);

        } catch (URISyntaxException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static File resolveSassFile(Path path) {
        final Path dir = path.getParent();
        final String basename = path.getFileName().toString();

        for (String prefix: new String[]{"_", ""}) {
            for (String suffix: new String[]{".scss", ".css", ""}) {
                Path target = dir.resolve(prefix + basename + suffix);
                File resource = new File(target.toString());
                if (resource.exists() && !resource.isDirectory()) {
                    return resource;
                }
            }
        }

        return null;
    }

}

@@ -329,170 +329,32 @@
 ass.
-importer.Import;%0A%0Aimport java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException
+context.StringContext
 ;%0A
+%0A
 impo
@@ -361,29 +361,23 @@
 rt java.
-n
 io.
-f
+F
 ile
-.Path
 ;%0Aimport
@@ -387,80 +387,14 @@
 va.n
-io.file.Paths;%0Aimport java.util.Collection;%0Aimport java.util.Collections
+et.URI
 ;%0A%0Ap
@@ -471,17 +471,22 @@
 (String 
-f
+inputF
 ilePath)
@@ -518,60 +518,40 @@
     
-String cssFileContents = IOHelper.readFileToString(f
+File inputFile = new File(inputF
 ileP
@@ -556,25 +556,24 @@
 ePath);%0A
-%0A
         
 Compiler
@@ -568,1629 +568,505 @@
     
-Compiler compiler = new Compiler();%0A        Options options = new Options();%0A        options.setImporters(Collections.singleton((path, previousImport) -%3E resolveImport(path, previousImport, filePath)));%0A%0A        Output output = compiler.compileString(cssFileContents, options);%0A        String cssString = output.getCss();%0A        return CSSParserFactory.getCSSParser(CSSParserFactory.CSSParserType.LESS).parseCSSString(cssString);%0A%0A    %7D%0A%0A    /**%0A     * Implements a %7B@link io.bit3.jsass.importer.Importer%7D%0A     *%0A     * @param importPath%0A     * @param previousImport%0A     * @param rootSassFilePath%0A     * @return%0A     */%0A    private static Collection%3CImport%3E resolveImport(String importPath, Import previousImport, String rootSassFilePath) %7B%0A        Path previousImportPath;%0A        String previousImportPathString = previousImport.getAbsoluteUri().getPath();%0A        if (%22stdin%22.equals(previousImportPathString)) %7B%0A            previousImportPath = Paths.get(rootSassFilePath).getParent();%0A        %7D else %7B%0A            previousImportPath = Paths.get(previousImportPathString).getParent();%0A        %7D%0A%0A        try %7B%0A            Path path = previousImportPath.resolve(importPath);%0A            File resource = resolveSassFile(path);%0A%0A            if (null == resource) %7B%0A                throw new FileNotFoundException(importPath);%0A            %7D%0A%0A            final URI uri = new URI(resource.getAbsolutePath());%0A            final String source = IOHelper.readFileToString(resource.getPath());%0A%0A            final Import scssImport = new Import(uri, uri, source);%0A            return Collections.singleton(scssImport
+URI inputFileURL = inputFile.toURI();%0A        URI outputFileURL = inputFile.getParentFile().toPath().resolve(inputFile.getName() + %22.css%22).toUri();%0A        String cssFileContents = IOHelper.readFileToString(inputFilePath);%0A%0A        Compiler compiler = new Compiler();%0A        Options options = new Options();%0A        options.setIsIndentedSyntaxSrc(inputFilePath.endsWith(%22.sass%22));%0A%0A        StringContext context = new StringContext(cssFileContents, inputFileURL, outputFileURL, options
 );%0A
-%0A
         
 %7D ca
@@ -1065,208 +1065,48 @@
     
-%7D catch (URISyntaxException %7C IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A%0A    private static File resolveSassFile(Path path) %7B%0A        final Path dir = path.getParent(
+Output output = compiler.compile(context
 );%0A 
@@ -1116,72 +1116,52 @@
     
-final 
 String 
-basename = path.getFileName().toString
+cssString = output.getCss
 ();%0A
-%0A
         
 for 
@@ -1160,422 +1160,109 @@
     
-for (String prefix: new String%5B%5D%7B%22_%22, %22%22%7D) %7B%0A            for (String suffix: new String%5B%5D%7B%22.scss%22, %22.css%22, %22%22%7D) %7B%0A                Path target = dir.resolve(prefix + basename + suffix);%0A                File resource = new File(target.toString());%0A                if (resource.exists() && !resource.isDirectory()) %7B%0A                    return resource;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return null;
+return CSSParserFactory.getCSSParser(CSSParserFactory.CSSParserType.LESS).parseCSSString(cssString);%0A
 %0A   

Fix libsass call for compiling .sass files