package org.lantern;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Ignore
public class ResourceBundleTest {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Test
    public void convertBasePo() throws Exception {
        final File po = new File("po/en.po");
        final File rb =
            new File("src/main/resources/LanternResourceBundle.properties");

        final BufferedWriter bw = new BufferedWriter(new FileWriter(rb));
        final BufferedReader br = new BufferedReader(new FileReader(po));
        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            if (line.startsWith("#")) {
            }
            if (StringUtils.isBlank(line)) {
            }
            if (line.startsWith("msgid")) {
                final String startLine = StringUtils.substringBetween(line, "\"", "\"");
                line = startLine;
                String valLine = br.readLine();
                while (!valLine.startsWith("msgstr")) {
                    //line += "\n";
                    line += StringUtils.substringBetween(valLine, "\"", "\"");
                    valLine = br.readLine();
                }
                final String key = line.replaceAll(" ", "_");
                String trans = StringUtils.substringBetween(valLine, "\"", "\"");
                valLine = br.readLine();
                while (valLine != null && valLine.trim().startsWith("\"")) {
                    //line += "\n";
                    trans += StringUtils.substringBetween(valLine, "\"", "\"");
                    valLine = br.readLine();
                }
                //final String value = StringUtils.substringBetween(valLine, "\"", "\"");
                final int length = Math.min(key.length(), LanternConstants.I18N_KEY_LENGTH);
                final String normalizedKey = key.substring(0, length);
                final String full = normalizedKey + "=" + line+"\n";

                // Ignore it if it's the initial configuration line.
                if (!normalizedKey.isEmpty()) {
                    bw.write(full);
                }
            }
            line = br.readLine();
        }
        bw.close();
        br.close();
        final String text = IOUtils.toString(new FileReader(rb));
        assertTrue(text.contains("You_appear_to_be_r"));
    }

    @Test
    public void convertPos() throws Exception {
        final File[] pos = new File("po").listFiles(new FileFilter() {

            @Override
            public boolean accept(final File pathname) {
                final String name = pathname.getName();
                return name.endsWith("po") && !name.equals("en.po") && !name.equals("zh.po");
            }
        });
        for (final File po : pos) {
            final String name = po.getName();
            final String localName =
                StringUtils.substringBeforeLast(name, ".po");
            final File dir = new File("src/main/resources");
            final File rb = new File(dir, "LanternResourceBundle_"+localName+".properties");
            convertPo(po, rb);
            final String text = IOUtils.toString(new FileReader(rb));
            assertTrue("Expected text not in: "+rb, text.contains("You_appear_to_be_r"));
        }
    }

    private void convertPo(final File po, final File rb) throws Exception {

        final BufferedWriter bw = new BufferedWriter(new FileWriter(rb));
        final BufferedReader br = new BufferedReader(new FileReader(po));
        String line = br.readLine();
        while (line != null) {
            System.out.println(line);
            if (line.startsWith("#")) {
            }
            if (StringUtils.isBlank(line)) {
            }
            if (line.startsWith("msgid")) {
                final String startLine = StringUtils.substringBetween(line, "\"", "\"");
                line = startLine;
                String valLine = br.readLine();
                while (!valLine.startsWith("msgstr")) {
                    //line += "\n";
                    line += StringUtils.substringBetween(valLine, "\"", "\"");
                    valLine = br.readLine();
                }
                //final String prelimKey = StringUtils.substringBetween(line, "\"", "\"");//StringUtils.substringAfter(line, "msgid ");
                final String key = line.replaceAll(" ", "_");
                String trans = StringUtils.substringBetween(valLine, "\"", "\"");
                valLine = br.readLine();
                while (valLine != null && valLine.trim().startsWith("\"")) {
                    //line += "\n";
                    trans += StringUtils.substringBetween(valLine, "\"", "\"");
                    valLine = br.readLine();
                }
                //final String value = StringUtils.substringBetween(valLine, "\"", "\"");
                final int length = Math.min(key.length(), LanternConstants.I18N_KEY_LENGTH);
                final String normalizedKey = key.substring(0, length);
                log.info("KEY: "+normalizedKey);
                final String full = normalizedKey + "=" + trans+"\n";

                // Ignore it if it's the initial configuration line.
                if (!normalizedKey.isEmpty()) {
                    bw.write(full);
                }
            }
            line = br.readLine();
        }
        bw.close();
        br.close();
    }
}

@@ -991,121 +991,8 @@
 e);%0A
-            if (line.startsWith(%22#%22)) %7B%0A            %7D%0A            if (StringUtils.isBlank(line)) %7B%0A            %7D%0A
     
@@ -1023,32 +1023,32 @@
 ith(%22msgid%22)) %7B%0A
+
                 
@@ -1194,32 +1194,221 @@
  br.readLine();%0A
+                if (valLine == null) %7B%0A                    log.warn(%22no msgstr for msgid %22 + line);%0A                    //end of file, so break%0A                    break;%0A                %7D%0A
                 
@@ -1760,32 +1760,33 @@
 ne, %22%5C%22%22, %22%5C%22%22);
+;
 %0A               
@@ -1892,44 +1892,8 @@
 ) %7B%0A
-                    //line += %22%5Cn%22;%0A
     
@@ -2023,32 +2023,134 @@
               %7D%0A
+                if (trans.trim().length() == 0) %7B%0A                    trans = line;%0A                %7D%0A
                 
@@ -2445,20 +2445,21 @@
 + %22=%22 + 
-line
+trans
 +%22%5Cn%22;%0A%0A
@@ -2843,24 +2843,63 @@
 ar_to_be_r%22)
+ && text.contains(%22You appear to be r%22)
 );%0A    %7D%0A%0A  
@@ -4109,32 +4109,32 @@
 line != null) %7B%0A
+
             Syst
@@ -4159,121 +4159,8 @@
 e);%0A
-            if (line.startsWith(%22#%22)) %7B%0A            %7D%0A            if (StringUtils.isBlank(line)) %7B%0A            %7D%0A
     

fix bugs in test