package com.example.patrice.content_provider;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

//http://www.tutos-android.com/contentprovider-android

// http://romain.raveaux.free.fr/document/Android/TD/tpcontentprovider-8-12.pdf

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        insertRecords();
        displayContentProvider();
    }

    private void displayContentProvider()
    {
        String columns[] = new String[] { SharedInformation.Cours.COURS_ID, SharedInformation.Cours.COURS_NAME, SharedInformation.Cours.COURS_DESC };
        Uri mContacts = AndroidProvider.CONTENT_URI;
        Cursor cur = managedQuery(mContacts, columns, null, null, null);

        Toast.makeText(MainActivity.this, cur.getCount() + "", Toast.LENGTH_LONG).show();

        if (cur.moveToFirst())
        {
            String name = null;
            do {
                name = cur.getString(cur.getColumnIndex(SharedInformation.Cours.COURS_ID)) + " " +
                        cur.getString(cur.getColumnIndex(SharedInformation.Cours.COURS_NAME)) + " " +
                        cur.getString(cur.getColumnIndex(SharedInformation.Cours.COURS_DESC));
                Toast.makeText(this, name + " ", Toast.LENGTH_LONG).show();
            } while (cur.moveToNext());
        }

    }

    private void insertRecords()
    {
        ContentValues contact = new ContentValues();
        contact.put(SharedInformation.Cours.COURS_NAME, "Android");
        contact.put(SharedInformation.Cours.COURS_DESC, "Introduction à la programmation sous Android");
        getContentResolver().insert(AndroidProvider.CONTENT_URI, contact);

        contact.clear();
        contact.put(SharedInformation.Cours.COURS_NAME, "Java");
        contact.put(SharedInformation.Cours.COURS_DESC, "Introduction à la programmation Java");
        getContentResolver().insert(AndroidProvider.CONTENT_URI, contact);

        contact.clear();
        contact.put(SharedInformation.Cours.COURS_NAME, "Iphone");
        contact.put(SharedInformation.Cours.COURS_DESC, "Introduction à l'objectif C");
        getContentResolver().insert(AndroidProvider.CONTENT_URI, contact);
    }
}