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;
    private enum ListType {
        Ordered, Unordered
    }
    private static int listIndex = 0;
    private static ListType listType;

    @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();
        } else if (tag.equalsIgnoreCase("ol")) {
            handleOl(opening);
        } else if (tag.equalsIgnoreCase("li")) {
            handleLi(opening, output);
        }
    }

    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;
        }
    }

    // Ordered lists are handled in a simple manner. They are rendered as Arabic numbers starting at 1
    // with no handling for alpha or Roman numbers or arbitrary numbering.
    private static void handleOl(boolean opening) {
        if (opening) {
            listIndex = 1;
            listType = ListType.Ordered;
        } else {
            listType = ListType.Unordered;
        }
    }

    private static void handleLi(boolean opening, Editable output) {
        if (opening) {
            if (listType == ListType.Ordered) {
                output.append("\n  " + (listIndex++) + ". ");
            } else {
                output.append("\n  • ");
            }
        }
    }

}

@@ -255,16 +255,78 @@
 dler %7B%0A%0A
+    private enum ListType %7B%0A        Ordered, Unordered%0A    %7D%0A%0A
     priv
@@ -371,16 +371,17 @@
 N = -1;%0A
+%0A
     priv
@@ -380,31 +380,24 @@
     private 
-static 
 int countCel
@@ -404,24 +404,32 @@
 ls = 0;%0A    
+private 
 int strikePo
@@ -517,76 +517,8 @@
 ate 
-enum ListType %7B%0A        Ordered, Unordered%0A    %7D%0A    private static 
 int 
@@ -548,24 +548,26 @@
 ate 
-static L
+ListType l
 istType 
 list
@@ -562,24 +562,36 @@
 istType 
-l
+= L
 istType
+.Unordered
 ;%0A%0A    @
@@ -1417,16 +1417,22 @@
         
+final 
 int leng
@@ -1938,39 +1938,32 @@
  %7D%0A%0A    private 
-static 
 void handleTd(bo
@@ -2228,39 +2228,32 @@
  %7D%0A%0A    private 
-static 
 void handleTr(bo
@@ -2615,39 +2615,32 @@
 ng.%0A    private 
-static 
 void handleOl(bo
@@ -2836,23 +2836,16 @@
 private 
-static 
 void han

use non static members in UnknownTagsHandler