package org.jboss.as.console.server;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.PropertyResourceBundle;
import java.util.Set;

/**
 * @author Heiko Braun
 * @date 2/22/12
 */
public class BootstrapI18N {

    static boolean dryRun = true;

    public static void main(String[] args) throws Exception{


        String env = System.getProperty("dryRun", "true");
        dryRun = Boolean.valueOf(env);

        System.out.println("DryRun: "+dryRun);

        File currentDir = new File("");
        File baseDir = new File(currentDir.getAbsolutePath()+"/gui/src/main/java/org/jboss/as/console/client/core");

        assert baseDir.exists();

        processFiles(baseDir, new Filter("UIConstants"));

    }

    private static void processFiles(File baseDir, Filter filter) throws Exception {
        File[] propertyFiles = baseDir.listFiles(filter);

        File source = new File(baseDir.getAbsolutePath()+"/"+filter.getPrefix()+".properties");
        System.out.println("Source: "+source);
        PropertyResourceBundle sourceBundle = new PropertyResourceBundle(
                new FileInputStream(source)
        );

        for(File target : propertyFiles)
        {
            PropertyResourceBundle targetBundle = new PropertyResourceBundle(
                    new FileInputStream(target)
            );

            System.out.println("Processing: "+target);
            Set<String> remaining = match(sourceBundle, targetBundle);

            // write it back

            boolean success = target.delete();

            if(!dryRun)
            {
                BufferedWriter writer = new BufferedWriter(new FileWriter(target, false));
                for(String remain : remaining)
                {
                    writer.write(remain+"="+targetBundle.getString(remain));
                    writer.newLine();
                }
                writer.flush();
                writer.close();
            }
            System.out.println("\n\n");

        }
    }

    private static Set<String> match(PropertyResourceBundle sourceBundle, PropertyResourceBundle targetBundle) {

        Set<String> remaining = new HashSet<String>();

        Enumeration<String> targetKeys = targetBundle.getKeys();
        while(targetKeys.hasMoreElements())
        {
            String targetKey = targetKeys.nextElement();
            Enumeration<String> sourceKeys = sourceBundle.getKeys();
            boolean matched = false;
            while(sourceKeys.hasMoreElements())
            {
                if(sourceKeys.nextElement().equals(targetKey))
                {
                    matched = true;
                    break;
                }
            }

            if(!matched)
                remaining.add(targetKey);

        }

        System.out.println("Source keys: "+sourceBundle.keySet().size());
        System.out.println("Target keys: "+targetBundle.keySet().size());
        System.out.println("Remaining: "+remaining.size());

        return remaining;



    }

    static class Filter implements FilenameFilter {

        String prefix;

        Filter(String prefix) {
            this.prefix = prefix;
        }

        public String getPrefix() {
            return prefix;
        }

        @Override
        public boolean accept(File file, String s) {
            return s.startsWith(prefix) && s.endsWith("_en.properties") && s.contains("_");
        }
    }
}


@@ -169,24 +169,82 @@
 nameFilter;%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 import java.
@@ -278,22 +278,19 @@
 va.util.
-HashSe
+Lis
 t;%0Aimpor
@@ -328,30 +328,8 @@
 dle;
-%0Aimport java.util.Set;
 %0A%0A/*
@@ -1576,34 +1576,35 @@
 t);%0A            
-Se
+Lis
 t%3CString%3E remain
@@ -1691,44 +1691,39 @@
     
-boolean success = target.delete(
+Collections.sort(remaining
 );%0A%0A
+%0A
     
@@ -1741,35 +1741,35 @@
 !dryRun)
-%0A            %7B%0A    
+ target.delete();%0A%0A
         
@@ -1795,16 +1795,32 @@
 writer =
+ dryRun ? null :
  new Buf
@@ -1859,25 +1859,25 @@
 t, false));%0A
- 
+%0A
             
@@ -1872,27 +1872,24 @@
             
-   
 for(String r
@@ -1907,16 +1907,197 @@
 aining)%0A
+            %7B%0A                if(dryRun)%0A                %7B%0A                    System.out.println(remain+%22=%22+targetBundle.getString(remain));%0A                %7D%0A                else%0A
         
@@ -2230,32 +2230,92 @@
                %7D
+%0A%0A            %7D%0A%0A            if (writer!=null)%0A            %7B
 %0A               
@@ -2369,32 +2369,33 @@
 ;%0A            %7D%0A
+%0A
             Syst
@@ -2455,18 +2455,19 @@
  static 
-Se
+Lis
 t%3CString
@@ -2559,18 +2559,19 @@
         
-Se
+Lis
 t%3CString
@@ -2592,14 +2592,16 @@
 new 
-HashSe
+ArrayLis
 t%3CSt
@@ -3154,16 +3154,50 @@
 !matched
+ && !remaining.contains(targetKey)
 )%0A      

make sure i18n until writes sorted properties