package fr.adrienbrault.idea.symfony2plugin.translation.parser;

import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Daniel Espendiller <daniel@espendiller.net>
 */
public class TranslationStringParser {

    public TranslationStringMap parse(Path path) {

        try {

            DirectoryStream<Path> ds = Files.newDirectoryStream(path, "*.php");

            // @TODO: which language is default?
            for(Path p: ds) {
                return this.parse(p.toString());
            }

        } catch (IOException e) {
            return new TranslationStringMap();
        }

        return new TranslationStringMap();
    }

    public TranslationStringMap parse(String file) {
        return this.parse(new File(file));
    }

    public TranslationStringMap parsePathMatcher(String path) {
        return this.parse(Paths.get(path));
    }

    @Nullable
    public TranslationStringMap parse(File file) {

        String string = "";
        try {
            string = this.fileToString(file.getPath());
        } catch (IOException e) {
            return null;
        }

        TranslationStringMap string_map = new TranslationStringMap();

        Matcher matcher = Pattern.compile("'([\\w]+)'\\s=>.*?array\\s\\((.*?)\\s\\),", Pattern.MULTILINE).matcher(string);
        while(matcher.find()){
            String domain = matcher.group(1);
            String array_strings = matcher.group(2);

            Matcher match_strings = Pattern.compile("'(.*?)'\\s=>\\s'.*?'", Pattern.MULTILINE).matcher(array_strings);
            while(match_strings.find()){
                string_map.addString(domain, match_strings.group(1));
            }

        }

        return string_map;
    }

    private String fileToString(String filename) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        StringBuilder builder = new StringBuilder();
        String line;

        while((line = reader.readLine()) != null){
            builder.append(line);
        }

        return builder.toString();
    }

}

@@ -124,129 +124,8 @@
 .*;%0A
-import java.nio.file.DirectoryStream;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A
 impo
@@ -330,30 +330,31 @@
 rse(
-Path path
+String file
 ) %7B%0A
-%0A
         
 try 
@@ -353,105 +353,159 @@
     
-try %7B%0A%0A            DirectoryStream%3CPath%3E ds = Files.newDirectoryStream(path, %22*.php%22
+return this.parse(new File(file));%0A    %7D%0A%0A    public TranslationStringMap parsePathMatcher(String path) %7B%0A%0A        File file = new File(path
 );%0A
-%0A 
         
    /
@@ -504,82 +504,70 @@
     
-   // @TODO: which language is default?%0A            for(Path p: d
+File%5B%5D files = file.listFiles();%0A%0A        if(null == file
 s) %7B%0A
-    
     
@@ -585,31 +585,33 @@
 urn 
-this.parse(p.to
+new Translation
 String
+Map
 (
-)
 );%0A 
@@ -609,36 +609,32 @@
 gMap();%0A        
-    
 %7D%0A%0A        %7D cat
@@ -632,80 +632,88 @@
     
-%7D catch (IOException e) %7B%0A            return new TranslationStringMap();
+for (final File fileEntry : files) %7B%0A            if (!fileEntry.isDirectory()) %7B
 %0A   
@@ -713,35 +713,32 @@
 ) %7B%0A        
-%7D%0A%0A
         
 return new T
@@ -729,47 +729,51 @@
     
-return new TranslationStringMap
+String fileName = fileEntry.getName
 ();%0A    
 %7D%0A%0A 
@@ -772,219 +772,205 @@
     
-%7D%0A%0A
     
-public TranslationStringMap parse(String file) %7B%0A        return this.parse(new File(file));%0A    %7D%0A%0A    public TranslationStringMap parsePathMatcher(String path) %7B%0A        return this.parse(Paths.get(path)
+        if( null != fileName && fileName.endsWith(%22php%22)) %7B%0A                    return this.parse(fileEntry);%0A                %7D%0A            %7D%0A        %7D%0A%0A        return new TranslationStringMap(
 );%0A 

make translation compatible with jdk1.6