package com.mobile.cupboardmanager;


import android.content.ContentUris;
import android.content.Intent;
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.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mobile.cupboardmanager.contentprovider.DBContentProvider;

/**
 * Display interactive list of ShoppingItems, with creation button
 */
public class ShoppingFragment extends Fragment implements
        LoaderManager.LoaderCallbacks<Cursor>, CustomCursorAdapter.onViewListener {


    private CustomCursorAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_shopping, container, false);
        rootView.findViewById(R.id.add_shopping_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ItemActivity.class);
                intent.putExtra(ItemActivity.INTENT_ITEM_URI,
                        DBContentProvider.SHOPPING_ITEMS.CONTENT_URI);
                startActivity(intent);
            }
        });

        // Populate data
        populateData(rootView);

        return rootView;
    }



    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {

        String[] projection = {DBContentProvider.SHOPPING_ITEMS._ID,
                DBContentProvider.SHOPPING_ITEMS.Quantity };
        CursorLoader cursorLoader = new CursorLoader(getActivity().getBaseContext(),
                DBContentProvider.SHOPPING_ITEMS.CONTENT_URI, null, null, null, null);
        return cursorLoader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        adapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // data is no longer available so get rid of reference
        adapter.swapCursor(null);
    }

    private void populateData(View rootView) {
        getLoaderManager().initLoader(0, null, this);

        Cursor cursor = getActivity().getContentResolver().query(
                DBContentProvider.SHOPPING_ITEMS.CONTENT_URI, null, null, null, null);

        adapter = new CustomCursorAdapter(getActivity(), cursor, false, R.layout.shoppingrow);
        adapter.setOnViewListener(this);

        ListView listView = (ListView)rootView.findViewById(R.id.shopping_list);
        listView.setAdapter(adapter);
    }

    @Override
    public void onBindView(View view, Cursor cursor) {
        final String itemName = cursor.getString(cursor.getColumnIndex(DatabaseHandler.ITEM_NAME));
        final long shoppingId = cursor.getLong(cursor.getColumnIndex(DatabaseHandler.SHOPPING_ID));
        ((TextView)view.findViewById(R.id.item_name)).setText(itemName);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ItemActivity.class);
                intent.putExtra(ItemActivity.INTENT_ITEM_URI,
                        DBContentProvider.SHOPPING_ITEMS.CONTENT_URI);
                intent.putExtra(ItemActivity.INTENT_ITEM_ID, shoppingId);
                startActivity(intent);
            }
        });

        view.findViewById(R.id.need_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "TODO", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

@@ -67,16 +67,54 @@
 ntUris;%0A
+import android.content.ContentValues;%0A
 import a
@@ -680,16 +680,82 @@
 vider;%0A%0A
+import java.util.Calendar;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 /**%0A * D
@@ -3020,169 +3020,1171 @@
     
-@Override%0A    public void onBindView(View view, Cursor cursor) %7B%0A        final String itemName = cursor.getString(cursor.getColumnIndex(DatabaseHandler.ITEM_NAME
+private void moveShoppingItemToCupboard(long shoppingId, long itemID, int quantity) %7B%0A        ContentValues values = new ContentValues();%0A        values.put(DatabaseHandler.CUPBOARD_ITEM, itemID);%0A        values.put(DatabaseHandler.CUPBOARD_QUANTITY, quantity);%0A%0A        Calendar calendar = Calendar.getInstance();%0A        long tomorrowInMs = calendar.getTimeInMillis() + TimeUnit.DAYS.toMillis(1);%0A        // Placeholder date%0A        values.put(DatabaseHandler.CUPBOARD_EXPIRY_TIME, tomorrowInMs);%0A%0A        getActivity().getContentResolver().insert(DBContentProvider.CUPBOARD_ITEMS.CONTENT_URI,%0A                values);%0A        getActivity().getContentResolver().delete(ContentUris.withAppendedId(%0A                DBContentProvider.SHOPPING_ITEMS.CONTENT_URI, shoppingId), null, null);%0A    %7D%0A%0A    @Override%0A    public void onBindView(View view, Cursor cursor) %7B%0A        final String itemName = cursor.getString(cursor.getColumnIndex(DatabaseHandler.ITEM_NAME));%0A        final long itemId = cursor.getLong(cursor.getColumnIndex(DatabaseHandler.SHOPPING_ITEM));%0A        final int itemQuantity = cursor.getInt(cursor.getColumnIndex(DatabaseHandler.SHOPPING_QUANTITY
 ));%0A
@@ -4979,32 +4979,110 @@
 Click(View v) %7B%0A
+                moveShoppingItemToCupboard(shoppingId, itemId, itemQuantity);%0A
                 
@@ -5116,12 +5116,35 @@
 ), %22
-TODO
+Item moved to cupboard list
 %22, T

Implement action move to cupboard list