package com.mapzen.tangram;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Xml;
import android.util.Log;

class FontFileParser {

    private Map<String, String> fontDict = new HashMap<String, String>();

    private void processDocument(XmlPullParser parser) throws XmlPullParserException, IOException {

        parser.nextTag();
        // Parse Families
        parser.require(XmlPullParser.START_TAG, null, "familyset");
        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            if ("family".equals(parser.getName())) {
                // Parse this family:
                String name = parser.getAttributeValue(null, "name");
                while (parser.next() != XmlPullParser.END_TAG) {
                    if (parser.getEventType() != XmlPullParser.START_TAG) {
                        continue;
                    }
                    String tag = parser.getName();
                    if ("font".equals(tag)) {
                        String weightStr = parser.getAttributeValue(null, "weight");
                        weightStr = (weightStr == null) ? "400" : weightStr;

                        String styleStr = parser.getAttributeValue(null, "style");
                        styleStr = (styleStr == null) ? "normal" : styleStr;

                        String filename = parser.nextText();
                        String fullFilename = "/system/fonts/" + filename;

                        String key = name + "_" + weightStr + "_" + styleStr;
                        fontDict.put(key, fullFilename);
                    } else {
                        skip(parser);
                    }
                }
            } else if ("alias".equals(parser.getName())) {
                // Parse this alias to font to fileName
                String aliasName = parser.getAttributeValue(null, "name");
                String toName = parser.getAttributeValue(null, "to");
                String weightStr = parser.getAttributeValue(null, "weight");
                weightStr = (weightStr == null) ? "400" : weightStr;
                String fontFilename = fontDict.get(toName + "_" + weightStr + "_normal"); // alias style is default: normal (always)
                fontDict.put(aliasName + "_" + weightStr, fontFilename);
            } else {
                skip(parser);
            }
        }
    }

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        int depth = 1;
        while (depth > 0) {
            switch (parser.next()) {
            case XmlPullParser.START_TAG:
                depth++;
                break;
            case XmlPullParser.END_TAG:
                depth--;
                break;
            }
        }
    }

    public FontFileParser() {

        InputStream in = null;
        final File fontFile = new File("/system/etc/fonts.xml");
        final File systemFontFile = new File("/system/etc/system_fonts.xml");
        final File fallbackFontFile = new File("/system/etc/fallback_fonts.xml");

        String fileXml = "";

        if (fontFile.exists()) {
            fileXml = fontFile.getAbsolutePath();
        } else if (systemFontFile.exists()) {
            fileXml = systemFontFile.getAbsolutePath();
        } else if (fallbackFontFile.exists()) {
            fileXml = systemFontFile.getAbsolutePath();
        }

        if("".equals(fileXml)) {
            return;
        }

        try {
            in = new FileInputStream(fileXml);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }

        XmlPullParser parser = Xml.newPullParser();

        try {
            parser.setInput(in, null);
            processDocument(parser);
        } catch(XmlPullParserException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getFontFile( String _key ) {
        if (fontDict.containsKey(_key)) {
            return fontDict.get(_key);
        } else {
            return "";
        }
    }

}


@@ -304,52 +304,8 @@
 ion;
-%0Aimport org.xmlpull.v1.XmlPullParserFactory;
 %0A%0Aim
@@ -3204,32 +3204,33 @@
 onts.xml%22);%0A
+%0A
         
 final File s
@@ -3217,24 +3217,29 @@
         
-final Fi
+// TODO: Hand
 le syste
@@ -3243,48 +3243,71 @@
 stem
-FontFile = new File(%22/system/etc/system_
+_fonts parsing which has a different xml layout as compared to 
 font
@@ -3311,19 +3311,16 @@
 onts.xml
-%22);
 %0A       
@@ -3324,153 +3324,128 @@
     
-final File fallbackFontFile = new File(%22/system/etc/fallback_fonts.xml%22);%0A%0A        String fileXml = %22%22;%0A%0A        if (fontFile.exists()) %7B
+//       system_fonts.xml also does not seem to have a good css style font parameter mapping as is the case with
 %0A       
     
@@ -3444,107 +3444,85 @@
     
+ //
      
-fileXml = 
+  fonts.xml (
 font
-File.getAbsolutePath();%0A        %7D else if (systemFontFile.exists()) %7B%0A  
+s.xml is available in android L and above)%0A%0A
         
   fi
@@ -3509,33 +3509,38 @@
 above)%0A%0A        
- 
+String
  fileXml = syste
@@ -3538,70 +3538,26 @@
 l = 
-systemFontFile.getAbsolutePath();%0A        %7D else if (fallbackF
+%22%22;%0A%0A        if (f
 ontF
@@ -3598,15 +3598,9 @@
 l = 
-systemF
+f
 ontF

Remove parsing system_fonts and fallback_fonts