package org.versebyverseministry.vbvmi.fragments.studies;


import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import org.versebyverseministry.vbvmi.R;
import org.versebyverseministry.vbvmi.fragments.shared.AbstractFragment;
import org.versebyverseministry.vbvmi.model.Study;
import org.versebyverseministry.vbvmi.model.Study_Table;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link StudyInfoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class StudyInfoFragment extends AbstractFragment {
    private static final String ARG_STUDY_ID = "ARG_STUDY_ID";

    private Study study;

    @BindView(R.id.webView)
    WebView webView;

    @BindView(R.id.imageView)
    ImageView imageView;

    public StudyInfoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment StudyInfoFragment.
     */
    public static StudyInfoFragment newInstance(String studyID) {
        StudyInfoFragment fragment = new StudyInfoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_STUDY_ID, studyID);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            String studyID = getArguments().getString(ARG_STUDY_ID);
            study = SQLite.select().from(Study.class).where(Study_Table.id.eq(studyID)).querySingle();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_study_info, container, false);
        unbinder = ButterKnife.bind(this, view);


        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);

        int imageWidth = displayMetrics.widthPixels;
        String studyImageURL = study.imageForWidth(imageWidth);
        if (studyImageURL != null) {
            Glide.with(getContext()).load(studyImageURL).into(imageView);
        } else {
            Glide.with(getContext()).load(study.thumbnailSource).into(imageView);
        }


        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.d("WEB", "Should url load this: " + url);
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.d("WEB", "Should overrideURLLoading");

                return false;
            }

            @Override
            public void onLoadResource(WebView view, String url) {
                Log.d("WEB", "load " + url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.d("WEB", "page started: " + url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                Log.d("WEB", "ERROR");
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                Log.d("WEB", "INTERCEPT");
                return null;
            }
        });

        StringBuilder sb = new StringBuilder();
        sb.append("<head>\n");
        sb.append("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script>\n");
        sb.append("<script type=\"text/javascript\">\n");
        sb.append("function codeAddress() {\n");
        sb.append("$(\"body a\").removeAttr(\"href\");\n");
        sb.append("}\n");
        sb.append("window.onload = codeAddress;\n");
        sb.append("</script>\n");
        sb.append("</head>\n");
        sb.append("<body>\n");
        sb.append(study.description);
        sb.append("</body>\n");

        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadData(sb.toString(), "text/html", "UTF-8");


        return view;
    }

}

@@ -3209,1213 +3209,8 @@
  %7D%0A%0A
-%0A        webView.setWebViewClient(new WebViewClient() %7B%0A            @Override%0A            public boolean shouldOverrideUrlLoading(WebView view, String url) %7B%0A                Log.d(%22WEB%22, %22Should url load this: %22 + url);%0A                return false;%0A            %7D%0A%0A            @Override%0A            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) %7B%0A                Log.d(%22WEB%22, %22Should overrideURLLoading%22);%0A%0A                return false;%0A            %7D%0A%0A            @Override%0A            public void onLoadResource(WebView view, String url) %7B%0A                Log.d(%22WEB%22, %22load %22 + url);%0A            %7D%0A%0A            @Override%0A            public void onPageStarted(WebView view, String url, Bitmap favicon) %7B%0A                Log.d(%22WEB%22, %22page started: %22 + url);%0A            %7D%0A%0A            @Override%0A            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) %7B%0A                Log.d(%22WEB%22, %22ERROR%22);%0A            %7D%0A%0A            @Override%0A            public WebResourceResponse shouldInterceptRequest(WebView view, String url) %7B%0A                Log.d(%22WEB%22, %22INTERCEPT%22);%0A                return null;%0A            %7D%0A        %7D);%0A%0A
     
@@ -3817,66 +3817,8 @@
 );%0A%0A
-        webView.getSettings().setDomStorageEnabled(true);%0A
     

remove unneeded web component