package cgeo.geocaching.connector.trackable;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.R;
import cgeo.geocaching.Trackable;
import cgeo.geocaching.utils.Log;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.Element;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Xml;

public class GeokretyParser {

    public static Trackable parse(final String page) {
        final Trackable trackable = new Trackable();

        final RootElement root = new RootElement("gkxml");
        final Element geokret = root.getChild("geokrety").getChild("geokret");

        geokret.setEndTextElementListener(new EndTextElementListener() {

            @Override
            public void end(String name) {
                trackable.setName(name);
            }
        });

        geokret.setStartElementListener(new StartElementListener() {

            @Override
            public void start(Attributes attributes) {
                try {
                    final int indexId = attributes.getIndex("id");
                    if (indexId > -1) {
                        trackable.setGeocode(geocode(Integer.parseInt(attributes.getValue("id"))));
                    }
                    final int indexDist = attributes.getIndex("dist");
                    if (indexDist > -1) {
                        trackable.setDistance(Float.parseFloat(attributes.getValue("dist")));
                    }
                    final int indexType = attributes.getIndex("type");
                    if (indexType > -1) {
                        trackable.setType(getType(Integer.parseInt(attributes.getValue("type"))));
                    }
                    final int indexWaypoint = attributes.getIndex("waypoint");
                    if (indexWaypoint > -1) {
                        trackable.setSpottedName(attributes.getValue(indexWaypoint));
                        trackable.setSpottedType(Trackable.SPOTTED_CACHE);
                    }
                } catch (final NumberFormatException e) {
                    Log.e("Parsing geokret", e);
                }
            }
        });

        try {
            Xml.parse(page, root.getContentHandler());
            return trackable;
        } catch (final SAXException e) {
            Log.w("Cannot parse geokrety", e);
        }

        return null;
    }

    protected static String getType(int type) {
        switch (type) {
            case 0:
                return CgeoApplication.getInstance().getString(R.string.geokret_type_traditional);
            case 1:
                return CgeoApplication.getInstance().getString(R.string.geokret_type_book_or_media);
            case 2:
                return CgeoApplication.getInstance().getString(R.string.geokret_type_human);
            case 3:
                return CgeoApplication.getInstance().getString(R.string.geokret_type_coin);
            case 4:
                return CgeoApplication.getInstance().getString(R.string.geokret_type_post);
        }
        return null;
    }

    protected static String geocode(final int id) {
        return String.format("GK%04X", id);
    }
}

@@ -174,16 +174,61 @@
 s.Log;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -1135,25 +1135,28 @@
   final 
-int index
+String krety
 Id = att
@@ -1166,21 +1166,21 @@
 utes.get
-Index
+Value
 (%22id%22);%0A
@@ -1207,20 +1207,39 @@
 if (
-indexId %3E -1
+StringUtils.isNotBlank(kretyId)
 ) %7B%0A
@@ -1312,33 +1312,15 @@
 Int(
-attributes.getValue(%22id%22)
+kretyId
 )));
@@ -1372,21 +1372,23 @@
 nal 
-int indexDist
+String distance
  = a
@@ -1400,21 +1400,21 @@
 utes.get
-Index
+Value
 (%22dist%22)
@@ -1443,22 +1443,40 @@
 if (
-indexDist %3E -1
+StringUtils.isNotBlank(distance)
 ) %7B%0A
@@ -1542,35 +1542,16 @@
 oat(
-attributes.getValue(%22dist%22)
+distance
 ));%0A
@@ -1598,25 +1598,28 @@
   final 
-int index
+String krety
 Type = a
@@ -1631,21 +1631,21 @@
 utes.get
-Index
+Value
 (%22type%22)
@@ -1674,22 +1674,41 @@
 if (
-indexType %3E -1
+StringUtils.isNotBlank(kretyType)
 ) %7B%0A
@@ -1778,35 +1778,17 @@
 Int(
-attributes.getValue(%22t
+kretyT
 ype
-%22)
 )));
@@ -1840,25 +1840,27 @@
 nal 
-int indexW
+String w
 aypoint
+Code
  = a
@@ -1876,13 +1876,13 @@
 .get
-Index
+Value
 (%22wa
@@ -1919,26 +1919,44 @@
 if (
-indexW
+StringUtils.isNotBlank(w
 aypoint
- %3E -1
+Code)
 ) %7B%0A
@@ -2008,42 +2008,20 @@
 ame(
-attributes.getValue(indexW
+w
 aypoint
-)
+Code
 );%0A 
@@ -2108,32 +2108,278 @@
               %7D%0A
+                    final String imageName = attributes.getValue(%22image%22);%0A                    if (StringUtils.isNotBlank(imageName)) %7B%0A                        trackable.setImage(%22http://geokrety.org/obrazki/%22 + imageName);%0A                    %7D%0A
                 

support geokrety images