package cl.monsoon.s1next.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.webkit.URLUtil;

import org.xml.sax.XMLReader;

import cl.monsoon.s1next.activity.GalleryActivity;

/**
 * Make ImageSpan clickable and handle `strike` tag.
 */
public final class MyTagHandler implements Html.TagHandler {

    private final Context mContext;

    public MyTagHandler(Context context) {
        this.mContext = context;
    }

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (!opening && tag.equalsIgnoreCase("img")) {
            handleStartImg(output);
        } else if (tag.equalsIgnoreCase("strike")) {
            handleStrike(opening, output);
        }
    }

    /**
     * See android.text.HtmlToSpannedConverter#startImg(android.text.SpannableStringBuilder, org.xml.sax.Attributes, android.text.Html.ImageGetter)
     */
    private void handleStartImg(Editable output) {
        int end = output.length();

        int len = "\uFFFC".length();
        ImageSpan imageSpan = output.getSpans(end - len, end, ImageSpan.class)[0];

        String url = imageSpan.getSource();
        // replace \uFFFC (OBJECT REPLACEMENT CHARACTER) to its ImageSpan's source
        // in order to support url copy (when selected)
        output.replace(end - len, end, url);

        // Emoji url don't have domain
        // skip Emoji url because we don't want Emoji clickable
        if (URLUtil.isNetworkUrl(url)) {

            output.removeSpan(imageSpan);
            // make this ImageSpan clickable
            output.setSpan(
                    new ImageClickableSpan(mContext, imageSpan.getDrawable(), url),
                    end - len,
                    output.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }

    /**
     * See android.text.HtmlToSpannedConverter#handleStartTag(java.lang.String, org.xml.sax.Attributes)
     * See android.text.HtmlToSpannedConverter#handleEndTag(java.lang.String)
     */
    private void handleStrike(boolean opening, Editable output) {
        int len = output.length();
        if (opening) {
            output.setSpan(new Strike(), len, len, Spannable.SPAN_MARK_MARK);
        } else {
            Object obj = getLast(output, Strike.class);
            int where = output.getSpanStart(obj);

            output.removeSpan(obj);

            if (where != len) {
                output.setSpan(new StrikethroughSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }

    /**
     * Same to android.text.HtmlToSpannedConverter#getLast(android.text.Spanned, java.lang.Class)
     */
    private static Object getLast(Spanned text, Class kind) {
        Object[] objs = text.getSpans(0, text.length(), kind);

        if (objs.length == 0) {
            return null;
        } else {
            return objs[objs.length - 1];
        }
    }

    public static class ImageClickableSpan extends ImageSpan implements View.OnClickListener {

        private final Context mContext;

        ImageClickableSpan(Context context, Drawable d, String source) {
            super(d, source);

            this.mContext = context;
        }

        public Context getContext() {
            return mContext;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), GalleryActivity.class);
            intent.putExtra(GalleryActivity.ARG_IMAGE_URL, getSource());

            getContext().startActivity(intent);
        }
    }

    private static class Strike {

    }
}

@@ -841,20 +841,8 @@
 if (
-!opening && 
 tag.
@@ -886,25 +886,29 @@
   handle
-StartImg(
+Img(opening, 
 output);
@@ -1212,17 +1212,29 @@
 ndle
-StartImg(
+Img(boolean opening, 
 Edit
@@ -1240,32 +1240,60 @@
 table output) %7B%0A
+        if (!opening) %7B%0A    
         int end 
@@ -1308,24 +1308,28 @@
 .length();%0A%0A
+    
         int 
@@ -1357,24 +1357,28 @@
 ();%0A        
+  
+  
 ImageSpan im
@@ -1449,16 +1449,20 @@
         
+    
 String u
@@ -1497,16 +1497,20 @@
         
+  
+  
 // repla
@@ -1584,16 +1584,20 @@
         
+    
 // in or
@@ -1636,32 +1636,36 @@
 lected)%0A        
+  
+  
 output.replace(e
@@ -1694,16 +1694,20 @@
         
+    
 // Emoji
@@ -1737,16 +1737,20 @@
         
+  
+  
 // skip 
@@ -1801,24 +1801,28 @@
 ble%0A        
+    
 if (URLUtil.
@@ -1847,32 +1847,36 @@
  %7B%0A%0A            
+  
+  
 output.removeSpa
@@ -1889,16 +1889,20 @@
 eSpan);%0A
+    
         
@@ -1942,32 +1942,36 @@
 ble%0A            
+  
+  
 output.setSpan(%0A
@@ -1970,16 +1970,20 @@
 etSpan(%0A
+    
         
@@ -2078,16 +2078,20 @@
         
+    
 end - le
@@ -2093,16 +2093,20 @@
  - len,%0A
+    
         
@@ -2154,16 +2154,20 @@
         
+  
+  
 Spanned.
@@ -2185,32 +2185,46 @@
 IVE_EXCLUSIVE);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix the wrong method name