package chat.rocket.android.widget.helper;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.widget.TextView;

import chat.rocket.android.widget.R;

public class InlineHightlighter {
  //TODO: not completely implemented...
  // original implementation is RocketChat:packages/rocketchat-markdown/markdown.coffee
  // regex pattern with '/(^|&gt;|[ >_*~])\`([^`\r\n]+)\`([<_*~]|\B|\b|$)/gm'

  public static void highlight(TextView textview) {
    final CharSequence text = textview.getText();
    textview.setText(highlightInner(textview.getContext(), text));
  }

  private static CharacterStyle[] createCharStyles(final Context context) {
    return new CharacterStyle[]{
        new ForegroundColorSpan(ContextCompat.getColor(context, R.color.highlight_text_color)),
        new BackgroundColorSpan(
            ContextCompat.getColor(context, R.color.highlight_text_background_color)),
        new StyleSpan(Typeface.BOLD), new TypefaceSpan("monospace")
    };
  }

  private static ForegroundColorSpan createTransparentSpan() {
    return new ForegroundColorSpan(Color.TRANSPARENT);
  }

  private static CharSequence highlightInner(final Context context, final CharSequence text) {
    final SpannableString s = new SpannableString(text);

    final int length = text.length();
    int highlightStart = length;
    for (int i = 0; i < length; i++) {
      char chr = text.charAt(i);
      if (chr == '`') {
        if (i > highlightStart) {
          final int highlightEnd = i;
          if (highlightStart + 1 < highlightEnd) {
            s.setSpan(createTransparentSpan(), highlightStart, highlightStart + 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            for (CharacterStyle span : createCharStyles(context)) {
              s.setSpan(span, highlightStart + 1, highlightEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            s.setSpan(createTransparentSpan(), highlightEnd, highlightEnd + 1,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          }
          highlightStart = length;
        } else {
          highlightStart = i;
        }
      }
    }

    return s;
  }
}

@@ -495,16 +495,80 @@
 tView;%0A%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A
 import c
@@ -1661,17 +1661,25 @@
 eString 
-s
+inputText
  = new S
@@ -1709,383 +1709,695 @@
     
-final int length = text.length();%0A    int highlightStart = length;%0A    for (int i = 0; i %3C length; i++) %7B%0A      char chr = text.charAt(i);%0A      if (chr == '%60') %7B%0A        if (i %3E highlightStart) %7B%0A          final int highlightEnd = i;%0A          if (highlightStart + 1 %3C highlightEnd) %7B%0A            s.setSpan(createTransparentSpan(), highlightStart, highlightStart + 1,%0A      
+Pattern highlightPattern = Pattern.compile(%0A        %22(%5E%7C&gt;%7C%5B %3E_*~%5D)%5C%5C%60(%5B%5E%60%5C%5Cr%5C%5Cn%5D+)%5C%5C%60(%5B%3C_*~%5D%7C%5C%5CB%7C%5C%5Cb%7C$)%22, Pattern.MULTILINE);%0A    Matcher matcher = highlightPattern.matcher(inputText);%0A%0A    while (matcher.find()) %7B%0A      setSpan(inputText, context,%0A          matcher.start() + matcher.group(1).length(),%0A          matcher.end() - matcher.group(3).length(),%0A          1, 1);%0A    %7D%0A%0A    return inputText;%0A  %7D%0A%0A  private static void setSpan(SpannableString inputText, Context context,%0A                              int start, int end, int markStartLen, int markEndLen) %7B%0A    if (markStartLen %3E 0) %7B%0A      inputText.setSpan(createTransparentSpan(),%0A          start, start + markStartLen,%0A
     
@@ -2437,28 +2437,226 @@
 USIVE);%0A    
+%7D%0A%0A
     
+CharacterStyle%5B%5D spans =%0A        inputText.getSpans(start + markStartLen, end - markEndLen, CharacterStyle.class);%0A    for (CharacterStyle span : spans) %7B%0A      inputText.removeSpan(span);%0A    %7D%0A
     for (Cha
@@ -2705,33 +2705,33 @@
 t)) %7B%0A      
-     
-   s
+inputText
 .setSpan(spa
@@ -2736,42 +2736,68 @@
 pan,
- highlightStart + 1, highlightEnd,
+%0A          start + markStartLen, end - markEndLen,%0A         
  Spa
@@ -2836,280 +2836,169 @@
     
+%7D%0A%0A
     
-    %7D%0A            s.setSpan(createTransparentSpan(), highlightEnd, highlightEnd + 1,%0A                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);%0A          %7D%0A          highlightStart = length;%0A        %7D else %7B%0A          highlightStart = i;%0A        %7D%0A      %7D%0A    %7D%0A%0A    return s;
+if (markEndLen %3E 0) %7B%0A      inputText.setSpan(createTransparentSpan(),%0A          end - markEndLen, end,%0A          Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);%0A    %7D
 %0A  %7D

fix InlineHighlighter to use the same regex as the web app.