/*
 * Copyright 2013-2015 The GDG Frisbee Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gdg.frisbee.android.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import java.util.ArrayList;

import org.gdg.frisbee.android.Const;
import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.adapter.ContributorAdapter;
import org.gdg.frisbee.android.api.ApiRequest;
import org.gdg.frisbee.android.api.GitHub;
import org.gdg.frisbee.android.api.model.Contributor;
import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.cache.ModelCache;
import org.gdg.frisbee.android.utils.Utils;
import org.joda.time.DateTime;

import butterknife.ButterKnife;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import timber.log.Timber;

/**
 * Created with IntelliJ IDEA.
 * User: maui
 * Date: 08.07.13
 * Time: 01:49
 * To change this template use File | Settings | File Templates.
 */
public class ContributorsFragment extends GdgListFragment {

    protected ContributorAdapter mAdapter;
    private ApiRequest mFetchContent;

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Timber.d("onSaveInstanceState()");
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        Timber.d("onResume()");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Timber.d("onActivityCreated()");

        mAdapter = new ContributorAdapter(getActivity());
        setListAdapter(mAdapter);

        loadContributors();
    }

    protected void loadContributors() {
        GitHub githubClient = new GitHub();
        mFetchContent = githubClient.getContributors(Const.GITHUB_ORGA, Const.GITHUB_REPO,
                new Response.Listener<ArrayList<Contributor>>() {
                    @Override
                    public void onResponse(final ArrayList<Contributor> contributors) {
                        App.getInstance().getModelCache().putAsync("frisbee_contributors", contributors, DateTime.now().plusDays(1), new ModelCache.CachePutListener() {
                            @Override
                            public void onPutIntoCache() {
                                mAdapter.addAll(contributors);
                            }
                        });
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        //To change body of implemented methods use File | Settings | File Templates.
                    }
                });

        if(Utils.isOnline(getActivity())) {
            mFetchContent.execute();
        } else {
            App.getInstance().getModelCache().getAsync("frisbee_contributors", false, new ModelCache.CacheListener() {
                @Override
                public void onGet(Object item) {
                    ArrayList<Contributor> contributors = (ArrayList<Contributor>)item;

                    Crouton.makeText(getActivity(), getString(R.string.cached_content), Style.INFO).show();
                    mAdapter.addAll(contributors);
                }

                @Override
                public void onNotFound(String key) {
                    Crouton.makeText(getActivity(), getString(R.string.offline_alert), Style.ALERT).show();
                }
            });
        }
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Timber.d("onCreateView()");
        View v = inflater.inflate(R.layout.fragment_gdl_list, null);
        ButterKnife.inject(this, v);
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        Timber.d("onStart()");
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Contributor contributor = mAdapter.getItem(position);
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(contributor.getHtmlUrl()));
        startActivity(i);
    }
}

@@ -933,37 +933,8 @@
 r;%0A%0A
-import java.util.ArrayList;%0A%0A
 impo
@@ -1369,16 +1369,45 @@
 eTime;%0A%0A
+import java.util.ArrayList;%0A%0A
 import b
@@ -1573,159 +1573,8 @@
 r;%0A%0A
-/**%0A * Created with IntelliJ IDEA.%0A * User: maui%0A * Date: 08.07.13%0A * Time: 01:49%0A * To change this template use File %7C Settings %7C File Templates.%0A */%0A
 publ
@@ -1676,46 +1676,8 @@
 ter;
-%0A    private ApiRequest mFetchContent;
 %0A%0A  
@@ -2330,32 +2330,49 @@
 tHub();%0A        
+final ApiRequest 
 mFetchContent = 
@@ -3206,110 +3206,8 @@
 ) %7B%0A
-                        //To change body of implemented methods use File %7C Settings %7C File Templates.%0A
     
@@ -3255,16 +3255,17 @@
       if
+ 
 (Utils.i
@@ -3620,16 +3620,17 @@
 ibutor%3E)
+ 
 item;%0A%0A 
@@ -4048,127 +4048,8 @@
  %7D%0A%0A
-    @Override%0A    public void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A    %7D%0A%0A
     
@@ -4256,20 +4256,32 @@
 t_gd
-l
+e
 _list, 
-null
+container, false
 );%0A 

reformat ContributorsFragment