package me.devsaki.hentoid.activities.websites;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.NonNull;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;

import me.devsaki.hentoid.HentoidApp;
import me.devsaki.hentoid.R;
import me.devsaki.hentoid.database.domains.Content;
import me.devsaki.hentoid.enums.Site;
import me.devsaki.hentoid.parsers.ContentParser;
import me.devsaki.hentoid.parsers.ContentParserFactory;
import me.devsaki.hentoid.util.Helper;
import me.devsaki.hentoid.util.HttpClientHelper;
import me.devsaki.hentoid.views.ObservableWebView;
import timber.log.Timber;

import static me.devsaki.hentoid.util.Helper.TYPE;
import static me.devsaki.hentoid.util.Helper.executeAsyncTask;
import static me.devsaki.hentoid.util.Helper.getWebResourceResponseFromAsset;

/**
 * Created by Shiro on 1/20/2016.
 * Implements nhentai source
 */
public class NhentaiActivity extends BaseWebActivity {

    Site getStartSite() {
        return Site.NHENTAI;
    }

    @Override
    void setWebView(ObservableWebView webView) {
        NhentaiWebViewClient client = new NhentaiWebViewClient(this);
        client.restrictTo("nhentai.net");

        webView.setWebViewClient(client);
        super.setWebView(webView);
    }

    private class NhentaiWebViewClient extends CustomWebViewClient {
        NhentaiWebViewClient(BaseWebActivity activity) {
            super(activity);
        }

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

            if (url.contains("nhentai.net/g/")) {
                String newURL = url.replace("/g", "/api/gallery");
                newURL = newURL.substring(0, newURL.length() - 1);
                executeAsyncTask(new JsonLoader(), newURL);
            }
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(@NonNull WebView view,
                                                          @NonNull String url) {
            if (url.contains("//static.nhentai.net/js/")) {
                return getWebResourceResponseFromAsset(getStartSite(), "main_js.js", TYPE.JS);
            } else if (url.contains("//static.nhentai.net/css/")) {
                return getWebResourceResponseFromAsset(getStartSite(), "main_style.css", TYPE.CSS);
            } else if (isUrlForbidden(url)) {
                return new WebResourceResponse("text/plain", "utf-8", nothing);
            } else {
                return super.shouldInterceptRequest(view, url);
            }
        }

        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public WebResourceResponse shouldInterceptRequest(@NonNull WebView view,
                                                          @NonNull WebResourceRequest request) {
            String url = request.getUrl().toString();
            if (url.contains("//static.nhentai.net/js/")) {
                return getWebResourceResponseFromAsset(getStartSite(), "main_js.js", TYPE.JS);
            } else if (url.contains("//static.nhentai.net/css/")) {
                return getWebResourceResponseFromAsset(getStartSite(), "main_style.css", TYPE.CSS);
            } else if (isUrlForbidden(url)) {
                return new WebResourceResponse("text/plain", "utf-8", nothing);
            } else {
                return super.shouldInterceptRequest(view, request);
            }
        }
    }

    private class JsonLoader extends AsyncTask<String, Integer, Content> {
        @Override
        protected Content doInBackground(String... params) {
            String url = params[0];
            try {
                ContentParser parser = ContentParserFactory.getInstance().getParser(Site.NHENTAI);
                processContent(parser.parseContent(HttpClientHelper.call(url)));
            } catch (Exception e) {
                Timber.e(e, "Error parsing content.");
                runOnUiThread(() -> Helper.toast(HentoidApp.getAppContext(), R.string.web_unparsable));
            }

            return null;
        }
    }
}

@@ -1387,32 +1387,363 @@
 ebView);%0A    %7D%0A%0A
+    private static String getGalleryId(String url)%0A    %7B%0A        String%5B%5D parts = url.split(%22/%22);%0A        boolean gFound = false;%0A        for (String s : parts)%0A        %7B%0A            if (gFound)%0A            %7B%0A                return s;%0A            %7D%0A            if (s.equals(%22g%22)) gFound = true;%0A        %7D%0A        return %22%22;%0A    %7D%0A%0A
     private clas
@@ -1795,16 +1795,16 @@
 lient %7B%0A
-
         
@@ -2112,183 +2112,95 @@
     
-String newURL = url.replace(%22/g%22, %22/api/gallery%22);%0A                newURL = newURL.substring(0, newURL.length() - 1);%0A                executeAsyncTask(new JsonLoader(), newURL
+executeAsyncTask(new JsonLoader(), %22https://nhentai.net/api/gallery/%22+getGalleryId(url)
 );%0A 

Allow download button to appears on book pages when browsing nHentai (detected by Firebase)