package barqsoft.footballscores.app.fragments;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import barqsoft.footballscores.R;
import barqsoft.footballscores.adapters.ScoresAdapter;
import barqsoft.footballscores.adapters.ViewHolder;
import barqsoft.footballscores.app.activity.MainActivity;
import barqsoft.footballscores.db.DatabaseContract;

/**
 * A placeholder fragment containing a simple view.
 */
public class MainScreenFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    public ScoresAdapter mAdapter;
    public static final int SCORES_LOADER = 0;
    private String[] fragmentdate = new String[1];
    private int last_selected_item = -1;

    public MainScreenFragment() {
    }

    public void setFragmentDate(String date) {
        fragmentdate[0] = date;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        final ListView score_list = (ListView) rootView.findViewById(R.id.scores_list);
        mAdapter = new ScoresAdapter(getActivity(), null, 0);
        score_list.setAdapter(mAdapter);
        getLoaderManager().initLoader(SCORES_LOADER, null, this);
        mAdapter.detail_match_id = MainActivity.selected_match_id;
        score_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ViewHolder selected = (ViewHolder) view.getTag();
                mAdapter.detail_match_id = selected.match_id;
                MainActivity.selected_match_id = (int) selected.match_id;
                mAdapter.notifyDataSetChanged();
            }
        });
        return rootView;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        return new CursorLoader(getActivity(), DatabaseContract.FixtureEntry.buildScoreWithDate
                (fragmentdate[0]),
                null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        //Log.v(FetchScoreTask.LOG_TAG,"loader finished");
        //cursor.moveToFirst();
        /*
        while (!cursor.isAfterLast())
        {
            Log.v(FetchScoreTask.LOG_TAG,cursor.getString(1));
            cursor.moveToNext();
        }
        */

        int i = 0;
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            i++;
            cursor.moveToNext();
        }
        //Log.v(FetchScoreTask.LOG_TAG,"Loader query: " + String.valueOf(i));
        mAdapter.swapCursor(cursor);
        //mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        mAdapter.swapCursor(null);
    }


}

@@ -799,57 +799,8 @@
 ment
- implements LoaderManager.LoaderCallbacks%3CCursor%3E
  %7B%0A 
@@ -902,10 +902,8 @@
 ring
-%5B%5D
  fra
@@ -911,69 +911,12 @@
 ment
-d
+D
 ate
- = new String%5B1%5D;%0A    private int last_selected_item = -1
 ;%0A%0A 
@@ -1018,23 +1018,20 @@
 fragment
-d
+D
 ate
-%5B0%5D
  = date;
@@ -1478,63 +1478,22 @@
     
-getLoaderManager().initLoader(SCORES_LOADER, null, this
+restartLoader(
 );%0A 
@@ -2074,361 +2074,455 @@
     
-@Override%0A    public Loader%3CCursor%3E onCreateLoader(int i, Bundle bundle) %7B%0A        return new CursorLoader(getActivity(), DatabaseContract.FixtureEntry.buildScoreWithDate%0A                (fragmentdate%5B0%5D),%0A                null, null, null, null);%0A
+private void restartLoader() %7B%0A        if (getLoaderManager().getLoader(SCORES_LOADER) == null) %7B%0A            getLoaderManager().initLoader(SCORES_LOADER, null, matchLoaderCallbacks);%0A        %7D else %7B%0A            getLoaderManager().restartLoader(SCORES_LOADER, null, matchLoaderCallbacks);%0A    
     %7D%0A
-%0A
     
-@Override%0A    public void onLoadFinished(Loader%3CCursor%3E cursorLoader, Cursor c
+%7D%0A%0A    private LoaderManager.LoaderCallbacks%3CCursor%3E matchLoaderCallbacks%0A            = new LoaderManager.LoaderCallbacks%3CC
 ursor
+%3E(
 ) %7B%0A
+%0A
         
 //Lo
@@ -2521,99 +2521,133 @@
     
-//Log.v(FetchScoreTask.LOG_TAG,%22loader finished%22);%0A        //cursor.moveToFirst();%0A
+@Override%0A        public Loader%3CCursor%3E onCreateLoader(int i, Bundle bundle) %7B%0A            return new CursorLoader(%0A 
         
 /*%0A 
@@ -2646,19 +2646,19 @@
     
-/*%0A
+   
         
 whil
@@ -2657,37 +2657,22 @@
     
-while (!cursor.isAfterLast())
+getActivity(),
 %0A   
@@ -2676,18 +2676,16 @@
         
-%7B%0A
         
@@ -2692,91 +2692,97 @@
     
-Log.v(FetchScoreTask.LOG_TAG,cursor.getString(1));%0A            cursor.moveToNext();
+DatabaseContract.FixtureEntry.buildScoreWithDate(fragmentDate),%0A                    null,
 %0A   
@@ -2786,26 +2786,24 @@
         
-%7D%0A
         
 */%0A%0A    
@@ -2798,101 +2798,52 @@
     
-*/%0A%0A        int i = 0;%0A        cursor.moveToFirst();%0A        while (!cursor.isAfterLast()) %7B%0A
+    null,%0A                    null,%0A        
     
@@ -2854,12 +2854,12 @@
     
-i++;
+null
 %0A   
@@ -2871,26 +2871,8 @@
     
-cursor.moveToNext(
 );%0A 
@@ -2880,24 +2880,25 @@
       %7D%0A
+%0A
         
 //Log.v(
@@ -2893,78 +2893,103 @@
     
-//Log.v(FetchScoreTask.LOG_TAG,%22Loader query: %22 + String.valueOf(i));%0A
+@Override%0A        public void onLoadFinished(Loader%3CCursor%3E cursorLoader, Cursor cursor) %7B%0A    
     
@@ -3033,10 +3033,12 @@
     
-//
+    
 mAda
@@ -3066,27 +3066,35 @@
 nged();%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     @Overrid
@@ -3095,24 +3095,28 @@
 verride%0A    
+    
 public void 
@@ -3160,32 +3160,36 @@
 ader) %7B%0A        
+    
 mAdapter.swapCur
@@ -3203,14 +3203,24 @@
 l);%0A    
-%7D%0A
+    %7D%0A    %7D;
 %0A%0A%7D%0A

Refactor fragment loader