package me.vickychijwani.spectre.view.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.vickychijwani.spectre.R;
import me.vickychijwani.spectre.model.Post;
import me.vickychijwani.spectre.pref.UserPrefs;
import me.vickychijwani.spectre.util.AppUtils;
import me.vickychijwani.spectre.view.PostViewActivity;

public class PostViewFragment extends BaseFragment {

    @Bind(R.id.edit_post_btn)
    View mEditBtn;

    private OnEditClickListener mEditClickListener;
    private Post mPost;
    private int mMarkdownHashCode;
    private WebViewFragment mWebViewFragment;

    public interface OnEditClickListener {
        void onEditClicked();
    }


    @SuppressWarnings("unused")
    public static PostViewFragment newInstance() {
        return new PostViewFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_post_view, container, false);
        ButterKnife.bind(this, view);

        mPost = ((PostViewActivity) getActivity()).getPost();

        UserPrefs prefs = UserPrefs.getInstance(getActivity());
        String blogUrl = prefs.getString(UserPrefs.Key.BLOG_URL);

        mWebViewFragment = WebViewFragment.newInstance("file:///android_asset/post-preview.html");
        mWebViewFragment.setJSInterface(new Object() {
            @JavascriptInterface
            public String getMarkdown() {
                // FIXME dirty string-replacement hack!
                return mPost.getMarkdown().replaceAll("/content/images",
                        AppUtils.pathJoin(blogUrl, "/content/images"));
            }
        }, "POST");
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.web_view_container, mWebViewFragment)
                .commit();

        // set up edit button
        mEditBtn.setOnClickListener(v -> mEditClickListener.onEditClicked());

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        hide();
    }

    @Override
    public void onShow() {
        if (getActivity() != null) {
            getActivity().setTitle(mPost.getTitle());
        }
        int markdownHashCode = mPost.getMarkdown().hashCode();
        if (markdownHashCode != mMarkdownHashCode) {
            mWebViewFragment.evaluateJavascript("preview()");
            mMarkdownHashCode = markdownHashCode;
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mEditClickListener = (OnEditClickListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + "must implement OnEditClickListener");
        }
    }

    public void setPost(@NonNull Post post) {
        mPost = post;
        onShow();
    }

}

@@ -72,16 +72,71 @@
 tivity;%0A
+import android.content.Intent;%0Aimport android.net.Uri;%0A
 import a
@@ -375,16 +375,47 @@
 terface;
+%0Aimport android.webkit.WebView;
 %0A%0Aimport
@@ -2146,24 +2146,495 @@
 %7D, %22POST%22);%0A
+        mWebViewFragment.setWebViewClient(new WebViewFragment.DefaultWebViewClient() %7B%0A            @Override%0A            public boolean shouldOverrideUrlLoading(WebView view, String url) %7B%0A                // launch links in external browser%0A                url = AppUtils.pathJoin(blogUrl, url);%0A                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));%0A                startActivity(intent);%0A                return true;%0A            %7D%0A        %7D);%0A%0A
         getC

Fix #99: Always launch external browser on tapping a link in the preview.