package shts.jp.android.nogifeed.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.apache.http.Header;

import shts.jp.android.nogifeed.R;
import shts.jp.android.nogifeed.adapters.FeedListAdapter;
import shts.jp.android.nogifeed.api.AsyncRssClient;
import shts.jp.android.nogifeed.common.Logger;
import shts.jp.android.nogifeed.listener.RssClientFinishListener;
import shts.jp.android.nogifeed.models.Entries;
import shts.jp.android.nogifeed.models.Entry;
import shts.jp.android.nogifeed.utils.IntentUtils;
import shts.jp.android.nogifeed.utils.UrlUtils;

// TODO: 通信ができない場合、エラー表示を行う
// 現在は Exception が発生する
// http://www.google.com/design/spec/whats-new/whats-new.html
public class AllFeedListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private ListView mAllFeedList;
    private FeedListAdapter mFeedListAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_all_feed_list, null);
        mAllFeedList = (ListView) view.findViewById(R.id.all_feed_list);

        // SwipeRefreshLayoutの設定
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(
                R.color.nogifeed, R.color.nogifeed, R.color.nogifeed, R.color.nogifeed);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getAllFeeds();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private void getAllFeeds() {

        boolean ret = AsyncRssClient.read(getActivity().getApplicationContext(),
                UrlUtils.FEED_ALL_URL, new RssClientFinishListener() {
            @Override
            public void onSuccessWrapper(int statusCode, Header[] headers, Entries entries) {
                // do nothing. set up feed at onFinish()
            }

            @Override
            public void onFailureWrapper(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                // do nothing. set up feed at onFinish()
            }

            @Override
            public void onFinish(Entries entries) {
                Logger.v("getAllFeed()", "get all member feed : size(" + entries.size() + ")");
                if (entries == null || entries.isEmpty()) {
                    // Show error toast
                    Toast.makeText(getActivity(), getResources().getString(R.string.feed_failure),
                            Toast.LENGTH_SHORT).show();
                } else {
                    // refresh feed list
                    setupAdapter(entries);
                }

                if (mSwipeRefreshLayout.isRefreshing()) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
            }
        });

        if (!ret) {
            // Show error toast
            Toast.makeText(getActivity(), getResources().getString(R.string.feed_failure),
                    Toast.LENGTH_SHORT).show();
            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    }

    private void setupAdapter(Entries entries) {
        mFeedListAdapter = new FeedListAdapter(getActivity(), entries);
        mAllFeedList.setAdapter(mFeedListAdapter);
        mAllFeedList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Entry entry = (Entry) mAllFeedList.getItemAtPosition(position);
                IntentUtils.startBlogActivity(getActivity(), entry);
            }
        });
    }

    @Override
    public void onRefresh() {
        getAllFeeds();
    }

}

@@ -38,16 +38,45 @@
 ments;%0A%0A
+import android.app.Activity;%0A
 import a
@@ -1070,24 +1070,106 @@
 Listener %7B%0A%0A
+    private static final String TAG = AllFeedListFragment.class.getSimpleName();%0A%0A
     private 
@@ -2997,16 +2997,68 @@
 size(%22 +
+%0A                        (entries == null ? %22null%22 :
  entries
@@ -3068,16 +3068,17 @@
 ze()
+)
  + %22)%22);
 %0A   
@@ -3073,16 +3073,269 @@
  + %22)%22);
+%0A%0A                // check Activity life cycle%0A                final Activity activity = getActivity();%0A                if (activity == null) %7B%0A                    Logger.w(TAG, %22activity already finished%22);%0A                    return;%0A                %7D
 %0A       
@@ -3466,48 +3466,18 @@
 ext(
-getA
+a
 ctivity
-(), getResources().getString(
+, 
 R.st
@@ -3485,33 +3485,32 @@
 ing.feed_failure
-)
 ,%0A              

add null check