package me.devsaki.hentoid.activities.sources;

import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebView;

import me.devsaki.hentoid.database.domains.Content;
import me.devsaki.hentoid.enums.Site;
import me.devsaki.hentoid.listener.ResultListener;

/**
 * Created by Shiro on 1/22/2016.
 * Implements tsumino source
 */
public class TsuminoActivity extends BaseWebActivity {

    private static final String DOMAIN_FILTER = "tsumino.com";
    private static final String GALLERY_FILTER = "//www.tsumino.com/entry/";
    private static final String[] blockedContent = {"/static/"};
    private boolean downloadFabPressed = false;
    private int historyIndex;

    Site getStartSite() {
        return Site.TSUMINO;
    }

    @Override
    protected CustomWebViewClient getWebClient() {
        addContentBlockFilter(blockedContent);
        CustomWebViewClient client = new TsuminoWebViewClient(GALLERY_FILTER, this);
        client.restrictTo(DOMAIN_FILTER);

        return client;
    }

    @Override
    public void onActionFabClick(View view) {
        if (MODE_DL == fabActionMode) {
            downloadFabPressed = true;
            historyIndex = webView.copyBackForwardList().getCurrentIndex();

            // Hack to reach the first gallery page to initiate download, and go back to the book page
            String newUrl = webView.getUrl().replace("entry", "Read/Index");
            webView.loadUrl(newUrl);
        } else {
            super.onActionFabClick(view);
        }
    }

    private class TsuminoWebViewClient extends CustomWebViewClient {

        TsuminoWebViewClient(String galleryFilter, WebContentListener listener) {
            super(galleryFilter, listener);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            // Hack to reach the first gallery page to initiate download, and go back to the book page
            if (downloadFabPressed &&
                    !(url.contains("//www.tsumino.com/Read/Index/") ||
                            url.contains("//www.tsumino.com/Read/Auth/") ||
                            url.contains("//www.tsumino.com/Read/AuthProcess"))) {
                downloadFabPressed = false;
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            // Hack to reach the first gallery page to initiate download, and go back to the book page
            if (downloadFabPressed && url.contains("//www.tsumino.com/Read/Index/")) {
                downloadFabPressed = false;
                int currentIndex = webView.copyBackForwardList().getCurrentIndex();
                webView.goBackOrForward(historyIndex - currentIndex);
                processDownload();
            }
        }
    }
}

@@ -605,16 +605,82 @@
 tic/%22%7D;%0A
+    private static final String%5B%5D DIRTY_ELEMENTS = %7B%22.ads-area%22%7D;%0A
     priv
@@ -873,24 +873,66 @@
 bClient() %7B%0A
+        addDirtyElements(DIRTY_ELEMENTS);%0A
         addC

Remove the huge ad blocks from Tsumino pages