package com.erpdevelopment.vbvm.fragments.studies.study;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatTextView;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.erpdevelopment.vbvm.model.Study;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import com.erpdevelopment.vbvm.R;
import com.erpdevelopment.vbvm.fragments.shared.AbstractFragment;
import com.erpdevelopment.vbvm.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)
    AppCompatTextView 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();
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    @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);
        }

        StringBuilder sb = new StringBuilder();

        sb.append("<body>\n");
        sb.append(study.description);
        sb.append("</body>\n");

        webView.setText(Html.fromHtml(sb.toString()));

        return view;
    }

}

@@ -124,16 +124,41 @@
 ontext;%0A
+import android.os.Build;%0A
 import a
@@ -3235,24 +3235,194 @@
 body%3E%5Cn%22);%0A%0A
+        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.N) %7B%0A            webView.setText(Html.fromHtml(sb.toString(), Html.FROM_HTML_MODE_LEGACY));%0A        %7D else %7B%0A    
         webV
@@ -3464,16 +3464,27 @@
 ng()));%0A
+        %7D%0A%0A
 %0A       

Remove deprecation