package com.daimajia.swipedemo;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.daimajia.swipe.util.Attributes;
import com.daimajia.swipedemo.adapter.RecyclerViewAdapter;
import com.daimajia.swipedemo.adapter.util.DividerItemDecoration;
import com.daimajia.swipedemo.Contact;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import jp.wasabeef.recyclerview.animators.FadeInLeftAnimator;

public class MainActivity extends Activity {

    /**
     * RecyclerView: The new recycler view replaces the list view. Its more modular and therefore we
     * must implement some of the functionality ourselves and attach it to our recyclerview.
     * <p/>
     * 1) Position items on the screen: This is done with LayoutManagers
     * 2) Animate & Decorate views: This is done with ItemAnimators & ItemDecorators
     * 3) Handle any touch events apart from scrolling: This is now done in our adapter's ViewHolder
     */

    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;

    private ArrayList<Contact> mDataSet;

    // Les contacts issus de la base de données du téléphone
    private ArrayList<Contact> contactsFromPhoneDatabase;

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

    }

    @Override
    public void onResume(){
        super.onResume();
        setContentView(R.layout.recyclerview);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //TODO : Version ??
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ActionBar actionBar = getActionBar();
            if (actionBar != null) {
                actionBar.setTitle("Liste des contacts");
            }
        }

        // Layout Managers:
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // Item Decorator:
        recyclerView.addItemDecoration(new DividerItemDecoration(getResources().getDrawable(R.drawable.divider)));
        recyclerView.setItemAnimator(new FadeInLeftAnimator());

        // Adapter:
        mDataSet = getContacts();
        mAdapter = new RecyclerViewAdapter(this, mDataSet);
        ((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
        recyclerView.setAdapter(mAdapter);

        /* Listeners */
        recyclerView.setOnScrollListener(onScrollListener);

        // Lancement de la recherche des changements dans les contacts

        // Update de la BdD de l'application

        ChangesDetector.updateApplicationDatabase(this.getApplicationContext());





    }

    /**
     * Substitute for our onScrollListener for RecyclerView
     */
    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            Log.e("ListView", "onScrollStateChanged");
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            // Could hide open views here if you wanted. //
        }
    };







    //Récupération de la liste de contact


    public  ArrayList<Contact> getContacts() {
        //Adresse de la table dans la base de donnée
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

        //Récupération des données
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.RawContacts.CONTACT_ID};

        //Initialisation du curseur
        Cursor people = getContentResolver().query(uri, projection, null, null, null);

        int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int indexId = people.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);

        ArrayList<Contact> contacts = new ArrayList<Contact>();

        //Mise en forme des données
        people.moveToFirst();
        do {
            String name   = people.getString(indexName);
            String number = people.getString(indexNumber);
            String contactid = people.getString(indexId);

            Contact contact1 = new Contact();

            contact1.setFirstName(name);
            contact1.setPhoneNumber(number);
            contact1.setContactId(contactid);


            contacts.add(contact1);

        } while (people.moveToNext());

        return contacts;


    }


}
@@ -2532,239 +2532,8 @@
 );%0A%0A
-        // Adapter:%0A        mDataSet = getContacts();%0A        mAdapter = new RecyclerViewAdapter(this, mDataSet);%0A        ((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);%0A        recyclerView.setAdapter(mAdapter);%0A
 %0A   
@@ -2814,16 +2814,338 @@
 xt());%0A%0A
+        // Adapter:%0A%0A        //Generate data%0A        Generator gn = new Generator(getApplicationContext());%0A        mDataSet = gn.getList1();%0A%0A%0A        mAdapter = new RecyclerViewAdapter(this, mDataSet);%0A        ((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);%0A        recyclerView.setAdapter(mAdapter);%0A%0A
 %0A%0A%0A%0A    
@@ -3777,1483 +3777,5 @@
 %0A%0A%0A%0A
-%0A%0A%0A    //R%C3%A9cup%C3%A9ration de la liste de contact%0A%0A%0A    public  ArrayList%3CContact%3E getContacts() %7B%0A        //Adresse de la table dans la base de donn%C3%A9e%0A        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;%0A%0A        //R%C3%A9cup%C3%A9ration des donn%C3%A9es%0A        String%5B%5D projection    = new String%5B%5D %7BContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,%0A                ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.RawContacts.CONTACT_ID%7D;%0A%0A        //Initialisation du curseur%0A        Cursor people = getContentResolver().query(uri, projection, null, null, null);%0A%0A        int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);%0A        int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);%0A        int indexId = people.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);%0A%0A        ArrayList%3CContact%3E contacts = new ArrayList%3CContact%3E();%0A%0A        //Mise en forme des donn%C3%A9es%0A        people.moveToFirst();%0A        do %7B%0A            String name   = people.getString(indexName);%0A            String number = people.getString(indexNumber);%0A            String contactid = people.getString(indexId);%0A%0A            Contact contact1 = new Contact();%0A%0A            contact1.setFirstName(name);%0A            contact1.setPhoneNumber(number);%0A            contact1.setContactId(contactid);%0A%0A%0A            contacts.add(contact1);%0A%0A        %7D while (people.moveToNext());%0A%0A        return contacts;%0A%0A%0A    %7D%0A%0A%0A
 %7D

Modify contact