package cgeo.geocaching.utils;

import org.xml.sax.XMLReader;

import android.text.Editable;
import android.text.Html.TagHandler;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;

public class UnknownTagsHandler implements TagHandler {

    private static final int UNDEFINED_POSITION = -1;
    private static int countCells = 0;
    int strikePos = UNDEFINED_POSITION;
    private boolean problematicDetected = false;

    @Override
    public void handleTag(boolean opening, String tag, Editable output,
            XMLReader xmlReader) {
        if (tag.equalsIgnoreCase("strike") || tag.equals("s")) {
            handleStrike(opening, output);
        } else if (tag.equalsIgnoreCase("table")) {
            handleProblematic();
        } else if (tag.equalsIgnoreCase("td")) {
            handleTd(opening, output);
        } else if (tag.equalsIgnoreCase("tr")) {
            handleTr(opening, output);
        } else if (tag.equalsIgnoreCase("pre")) {
            handleProblematic();
        }
    }

    private void handleStrike(boolean opening, Editable output) {
        int length = output.length();
        if (opening) {
            strikePos = length;
        } else {
            if (strikePos > UNDEFINED_POSITION) {
                output.setSpan(new StrikethroughSpan(), strikePos, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                strikePos = UNDEFINED_POSITION;
            }
        }
    }

    public boolean isProblematicDetected() {
        return problematicDetected;
    }

    private void handleProblematic() {
        problematicDetected = true;
    }

    private static void handleTd(boolean opening, Editable output) {
        // insert bar for each table column, see https://en.wikipedia.org/wiki/Box-drawing_characters
        if (opening) {
            if (countCells++ > 0) {
                output.append('┆');
            }
        }
    }

    private static void handleTr(boolean opening, Editable output) {
        // insert new line for each table row
        if (opening) {
            output.append('\n');
            countCells = 0;
        }
    }

}

@@ -436,16 +436,153 @@
 = false;
+%0A    private enum ListType %7B%0A        Ordered, Unordered%0A    %7D%0A    private static int listIndex = 0;%0A    private static ListType listType;
 %0A%0A    @O
@@ -1140,32 +1140,200 @@
 eProblematic();%0A
+        %7D else if (tag.equalsIgnoreCase(%22ol%22)) %7B%0A            handleOl(opening);%0A        %7D else if (tag.equalsIgnoreCase(%22li%22)) %7B%0A            handleLi(opening, output);%0A
         %7D%0A    %7D%0A
@@ -2382,24 +2382,24 @@
 pend('%5Cn');%0A
-
             
@@ -2431,10 +2431,703 @@
 %0A    %7D%0A%0A
+    // Ordered lists are handled in a simple manner. They are rendered as Arabic numbers starting at 1%0A    // with no handling for alpha or Roman numbers or arbitrary numbering.%0A    private static void handleOl(boolean opening) %7B%0A        if (opening) %7B%0A            listIndex = 1;%0A            listType = ListType.Ordered;%0A        %7D else %7B%0A            listType = ListType.Unordered;%0A        %7D%0A    %7D%0A%0A    private static void handleLi(boolean opening, Editable output) %7B%0A        if (opening) %7B%0A            if (listType == ListType.Ordered) %7B%0A                output.append(%22%5Cn  %22 + (listIndex++) + %22. %22);%0A            %7D else %7B%0A                output.append(%22%5Cn  %E2%80%A2 %22);%0A            %7D%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add handling for HTML lists