package fr.utc.assos.uvweb.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;

import fr.utc.assos.uvweb.R;
import fr.utc.assos.uvweb.api.UvwebProvider;
import fr.utc.assos.uvweb.model.Comment;
import fr.utc.assos.uvweb.model.Newsfeed;
import fr.utc.assos.uvweb.ui.activity.CommentActivity;
import fr.utc.assos.uvweb.ui.adapter.NewsfeedAdapter;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class NewsfeedFragment extends Fragment implements Callback<Newsfeed>, NewsfeedAdapter.ItemClickListener {
    private static final String TAG = NewsfeedFragment.class.getSimpleName();
    private static final String STATE_NEWSFEED_ITEMS = "state_newsfeed_items";

    private static final int LOADING_STATE_IN_PROGRESS = 0;
    private static final int LOADING_STATE_COMPLETE = 1;

    private RecyclerView recyclerView;

    private NewsfeedAdapter adapter;
    private List<Comment> comments;
    private ProgressBar progressBar;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_newsfeed, container, false);

        recyclerView = (RecyclerView) root.findViewById(R.id.recycler);
        progressBar = (ProgressBar) root.findViewById(R.id.progressbar);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new NewsfeedAdapter(this);
        recyclerView.setAdapter(adapter);

        return root;
    }

    private void setLoadingState(int loadingState) {
        if (loadingState == LOADING_STATE_IN_PROGRESS) {
            progressBar.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null &&
                (comments = savedInstanceState.getParcelableArrayList(STATE_NEWSFEED_ITEMS)) != null) {
            updateViews();
        } else {
            setLoadingState(LOADING_STATE_IN_PROGRESS);
            UvwebProvider.getNewsfeed(this);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (comments != null) {
            outState.putParcelableArrayList(STATE_NEWSFEED_ITEMS, new ArrayList<Parcelable>(comments));
        }
    }

    @Override
    public void success(Newsfeed newsfeed, Response response) {
        comments = newsfeed.getComments();
        if (getActivity() != null) {
            updateViews();
        }
    }

    private void updateViews() {
        adapter.setComments(comments);
        setLoadingState(LOADING_STATE_COMPLETE);
    }

    @Override
    public void failure(RetrofitError error) {
        Log.e(TAG, "Failed loading newsfeed", error);
        Snackbar.make(getView(), getString(R.string.loading_error), Snackbar.LENGTH_SHORT).show();
        if (getActivity() != null) {
            setLoadingState(LOADING_STATE_COMPLETE);
        }
    }

    @Override
    public void onClick(Comment comment) {
        Intent intent = new Intent(getActivity(), CommentActivity.class);
        intent.putExtra(CommentActivity.ARG_COMMENT, comment);
        intent.putExtra(CommentActivity.ARG_UVNAME, comment.getUvName());
        startActivity(intent);
    }
}

@@ -2482,11 +2482,11 @@
 null
- &&
+) %7B
 %0A   
@@ -2498,13 +2498,8 @@
     
-    (
 comm
@@ -2568,17 +2568,42 @@
 D_ITEMS)
-)
+;%0A            if (comments
  != null
@@ -2598,32 +2598,36 @@
 ents != null) %7B%0A
+    
             upda
@@ -2641,37 +2641,64 @@
 ();%0A        
+      
-%7D else %7B%0A    
+  return;%0A            %7D%0A        %7D%0A
         setL
@@ -2745,20 +2745,16 @@
         
-    
 UvwebPro
@@ -2778,26 +2778,16 @@
 (this);%0A
-        %7D%0A
     %7D%0A%0A 

Remove inner assignment