package in.uncod.android.bypass;

import in.uncod.android.bypass.Element.Type;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.BulletSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.QuoteSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;

public class Bypass {
	static {
		System.loadLibrary("bypass");
	}

	private static final float[] HEADER_SIZES = { 
		1.5f, // h1
		1.4f, // h2
		1.3f, // h3
		1.2f, // h4
		1.1f, // h5
		1.0f, // h6
	};

	public CharSequence markdownToSpannable(String markdown) {
		Document document = processMarkdown(markdown);

		CharSequence[] spans = new CharSequence[document.getElementCount()];
		for (int i = 0; i < document.getElementCount(); i++) {
			spans[i] = recurseElement(document.getElement(i));
		}

		return TextUtils.concat(spans);
	}

	private native Document processMarkdown(String markdown);

	private CharSequence recurseElement(Element element) {

		CharSequence[] spans = new CharSequence[element.size()];
		for (int i = 0; i < element.size(); i++) {
			spans[i] = recurseElement(element.children[i]);
		}

		CharSequence concat = TextUtils.concat(spans);

		SpannableStringBuilder builder = new SpannableStringBuilder();
		String text = element.getText();
		if (element.size() == 0
				&& element.getParent() != null
                && element.getParent().getType() != Type.BLOCK_CODE) {
			text = text.replace('\n', ' ');
		}
		switch (element.getType()) {
			case LIST:
				if (element.getParent() != null
					&& element.getParent().getType() == Type.LIST_ITEM) {
					builder.append("\n");
				}
				break;
			case LINEBREAK:
				builder.append("\n");
				break;
			case LIST_ITEM:
				builder.append("\u2022");
				break;
		}
		builder.append(text);
		builder.append(concat);
		if (element.getType() == Type.LIST && element.getParent() != null) {

		} else if (element.getType() == Type.LIST_ITEM) {
			if (element.size() > 0 && element.children[element.size()-1].isBlockElement()) {
				
			}
			else {
				builder.append("\n");
			}
		} else if (element.isBlockElement()) {
			builder.append("\n\n");
		}

		if (element.getType() == Type.HEADER) {
			String levelStr = element.getAttribute("level");
			int level = Integer.parseInt(levelStr);
			setSpan(builder, new RelativeSizeSpan(HEADER_SIZES[level - 1]));
			setSpan(builder, new StyleSpan(Typeface.BOLD));
		} else if (element.getType() == Type.LIST_ITEM
				&& element.getParent().getParent() != null) {
			setSpan(builder, new LeadingMarginSpan.Standard(20));
		} else if (element.getType() == Type.EMPHASIS) {
			setSpan(builder, new StyleSpan(Typeface.ITALIC));
		} else if (element.getType() == Type.DOUBLE_EMPHASIS) {
			setSpan(builder, new StyleSpan(Typeface.BOLD));
		} else if (element.getType() == Type.TRIPLE_EMPHASIS) {
			setSpan(builder, new StyleSpan(Typeface.BOLD_ITALIC));
		} else if (element.getType() == Type.CODE_SPAN) {
			setSpan(builder, new TypefaceSpan("monospace"));
		} else if (element.getType() == Type.LINK) {
			setSpan(builder, new URLSpan(element.getAttribute("link")));
		} else if (element.getType() == Type.BLOCK_QUOTE) {
			setSpan(builder, new QuoteSpan());
			setSpan(builder, new StyleSpan(Typeface.ITALIC));
		} else if (element.getType() == Type.STRIKETHROUGH) {
			setSpan(builder, new StrikethroughSpan());
		}

		return builder;
	}

	private static void setSpan(SpannableStringBuilder builder, Object what) {
		builder.setSpan(what, 0, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	}
}

@@ -72,16 +72,48 @@
 t.Type;%0A
+import android.content.Context;%0A
 import a
@@ -800,16 +800,368 @@
 h6%0A%09%7D;%0A%0A
+%09private final int mListItemIndent;%0A%0A%09/**%0A%09 * @deprecated Use %7B@link #Bypass(android.content.Context)%7D instead.%0A%09 */%0A%09@Deprecated%0A%09public Bypass() %7B%0A%09%09// Default constructor for backwards-compatibility%0A%09%09mListItemIndent = 20;%0A%09%7D%0A%0A%09public Bypass(Context context) %7B%0A%09%09mListItemIndent = (int) mContext.getResources().getDisplayMetrics().density * 10;%0A%09%7D%0A%0A
 %09public 
@@ -3177,18 +3177,31 @@
 tandard(
-20
+mListItemIndent
 ));%0A%09%09%7D 

Scale indentation based on device density