package mjkarbasian.moshtarimadar;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
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.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import mjkarbasian.moshtarimadar.Data.KasebContract;
import mjkarbasian.moshtarimadar.adapters.CustomerAdapter;

/**
 * A placeholder fragment containing a simple view.
 */
public class CustomersLists extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private final String LOG_TAG = CustomersLists.class.getSimpleName();
    final static int FRAGMENT_CUSTOMER_LOADER = 2;
    String searchQuery;

    CustomerAdapter mCustomerAdapter = null;
    ListView mListView;
    String[] mProjection;


    public CustomersLists() {
        super();
    }

    public void onCreate(Bundle savedInstanceState) {

        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab_customers);

        mProjection = new String[]{
                KasebContract.Customers._ID,
                KasebContract.Customers.COLUMN_FIRST_NAME,
                KasebContract.Customers.COLUMN_LAST_NAME,
                KasebContract.Customers.COLUMN_STATE_ID};

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(getArguments()!=null) searchQuery = getArguments().getString("query");
        mCustomerAdapter = new CustomerAdapter(
                getActivity(),
                null,
                0);

        View rootView = inflater.inflate(R.layout.fragment_customers, container, false);
        mListView = (ListView) rootView.findViewById(R.id.list_view_customers);
        mListView.setAdapter(mCustomerAdapter);


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = (Cursor) parent.getItemAtPosition(position);
                if (cursor != null) {
                    Intent intent = new Intent(getActivity(), DetailCustomer.class)
                            .setData(KasebContract.Customers.buildCustomerUri(
                                    Long.parseLong(cursor.getString(cursor.getColumnIndex(KasebContract.Customers._ID)))
                            ));
                    startActivity(intent);
                }
            }
        });


//                    String locationSetting = Utility.getPreferredLocation(getActivity());
//                    Intent intent = new Intent(getActivity(), DetailActivity.class)
//                            .setData(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
//                                    locationSetting, cursor.getLong(COL_WEATHER_DATE)
//                            ));
//                    startActivity(intent);
//                    ((Callback)getActivity())
//                            .onItemSelected(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
//                                    locationSetting, cursor.getLong(COL_WEATHER_DATE)));
//                }
//                mPosition = position;

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.menu_customers, menu);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onStart() {
        Log.d(LOG_TAG, "onStart");
        super.onStart();
        updateList();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d(LOG_TAG, "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
        getLoaderManager().initLoader(FRAGMENT_CUSTOMER_LOADER, null, this);
    }

    private void updateList() {
        getLoaderManager().restartLoader(FRAGMENT_CUSTOMER_LOADER, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Log.d(LOG_TAG, "onCreateLoader");
        String whereClause;
        String[] selectArg = null;
        if(searchQuery!=null) {
            whereClause = mProjection[1] + " LIKE ? " ;
//            + " OR " + mProjection[2] + " LIKE ? ";
            selectArg = new String[1];
            selectArg[0] = "%"+searchQuery + "%" ;
//            selectArg[1] = searchQuery + "%" ;
        }
        else
        {
         whereClause = null;
        }
        return new CursorLoader(getActivity(), KasebContract.Customers.CONTENT_URI, mProjection, whereClause, selectArg, null);
}

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        Log.d(LOG_TAG, "onLoadFinished");
        mCustomerAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        Log.d(LOG_TAG, "onLoadReset");
        mCustomerAdapter.swapCursor(null);
    }


}

@@ -4829,32 +4829,16 @@
 LIKE ? %22
- ;%0A//           
  + %22 OR 
@@ -4905,17 +4905,17 @@
  String%5B
-1
+2
 %5D;%0A     
@@ -4960,18 +4960,16 @@
 + %22%25%22 ;%0A
-//
         
@@ -4987,16 +4987,20 @@
 rg%5B1%5D = 
+%22%25%22+
 searchQu

add search on last name, must add back handling