package com.aesthetikx.hubski.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.*;
import android.widget.TextView;
import com.aesthetikx.hubski.R;
import com.aesthetikx.hubski.model.Post;

public class CommentsFragment extends Fragment {

    private Post post;

    public static CommentsFragment newInstance(Post post) {
        CommentsFragment fragment = new CommentsFragment();
        Bundle args = new Bundle();
        args.putSerializable("post", post);
        fragment.setArguments(args);
        return fragment;
    }

    public CommentsFragment() { }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText("Comments for " + post.getTitle());
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        if (post.hasArticle()) {
            inflater.inflate(R.menu.comments, menu);
        } else {
            inflater.inflate(R.menu.comments_no_article, menu);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.post = (Post) getArguments().getSerializable("post");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_article:
                FragmentManager manager = getActivity().getSupportFragmentManager();
                manager.beginTransaction().replace(R.id.container, ArticleFragment.newInstance(post)).commit();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}


@@ -219,22 +219,126 @@
 id.w
-idget.TextView
+ebkit.WebChromeClient;%0Aimport android.webkit.WebView;%0Aimport android.webkit.WebViewClient;%0Aimport android.widget.Toast
 ;%0Aim
@@ -1049,32 +1049,43 @@
 nstanceState) %7B%0A
+        /*%0A
         View roo
@@ -1320,16 +1320,154 @@
 otView;%0A
+        */%0A        WebView webview = prepareWebView();%0A        webview.loadUrl(post.getCommentsUrl().toString());%0A        return webview;%0A
     %7D%0A%0A 
@@ -2198,16 +2198,41 @@
 action()
+%0A                        
 .replace
@@ -2282,16 +2282,89 @@
 e(post))
+%0A                        //.addToBackStack(null)%0A                        
 .commit(
@@ -2489,12 +2489,753 @@
  %7D%0A    %7D
+%0A%0A    private WebView prepareWebView() %7B%0A        WebView webview = new WebView(getActivity().getApplicationContext());%0A        webview.getSettings().setJavaScriptEnabled(true);%0A        webview.getSettings().setBuiltInZoomControls(true);%0A        webview.setWebChromeClient(new WebChromeClient() %7B%0A            public void onProgressChanged(WebView view, int progress) %7B%0A                //TODO handle progress%0A            %7D%0A        %7D);%0A        webview.setWebViewClient(new WebViewClient() %7B%0A            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) %7B%0A                Toast.makeText(getActivity(), description, Toast.LENGTH_SHORT).show();%0A            %7D%0A        %7D);%0A%0A        return webview;%0A    %7D
 %0A%7D%0A%0A

Use webview for comments for now