package com.example.cpcreator;

import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.database.Cursor;
import android.util.AndroidException;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class StudentGrades extends Activity {

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

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_student_grades, menu);
      return true;
   }

   public void onClickAddName(View view) {
      // Add a new student record
      ContentValues values = new ContentValues();

      values.put(StudentsProvider.NAME, 
      ((EditText)findViewById(R.id.txtName)).getText().toString());
      
      values.put(StudentsProvider.GRADE, 
      ((EditText)findViewById(R.id.txtGrade)).getText().toString());

      Uri uri = getContentResolver().insert(
      StudentsProvider.CONTENT_URI, values);
      
      Toast.makeText(getBaseContext(), 
      uri.toString(), Toast.LENGTH_LONG).show();
   }

   public void onClickRetrieveStudents(View view) throws AndroidException {
      // Retrieve student records
//      String URL = "content://com.example.provider.College/students";
//      Uri students = Uri.parse(URL);
//      Cursor c = managedQuery(students, null, null, null, "name");
//      if (c.moveToFirst()) {
//         do{
//            Toast.makeText(this, 
//            c.getString(c.getColumnIndex(StudentsProvider._ID)) + 
//            ", " +  c.getString(c.getColumnIndex( StudentsProvider.NAME)) + 
//            ", " + c.getString(c.getColumnIndex( StudentsProvider.GRADE)), 
//            Toast.LENGTH_SHORT).show();
//         } while (c.moveToNext());
//      }
	   
	   String URL = "content://com.example.provider.College/students";
       Uri students = Uri.parse(URL);
       ContentProviderClient yourCR = getContentResolver().acquireContentProviderClient(students);       
       Cursor c = yourCR.query(students, null, null, null, null);
       if (c.moveToFirst()) {
           do{
              Toast.makeText(this, 
              c.getString(c.getColumnIndex(StudentsProvider._ID)) + 
              ", " +  c.getString(c.getColumnIndex( StudentsProvider.NAME)) + 
              ", " + c.getString(c.getColumnIndex( StudentsProvider.GRADE)), 
              Toast.LENGTH_SHORT).show();
           } while (c.moveToNext());
        }
   }
}