package com.example.listviewcustom05;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class Gestion extends Activity 
{
	private EditText editTextName;
    private EditText editTextAdd;
    private EditText editTextId;
    private Button btnPrev;
    private Button btnNext;
    private Button btnSave;
    private Button btnDelete;
    
    private static final String SELECT_SQL = "SELECT * FROM contact";
    
    private SQLiteDatabase db;
 
    private Cursor c;
	
	public void onCreate(Bundle savedInstanceState) 
	{
		
			super.onCreate(savedInstanceState);
			setContentView(R.layout.gestion);
			openDatabase();
		   
			editTextId = (EditText) findViewById(R.id.editTextId);
	        editTextName = (EditText) findViewById(R.id.editTextName);
	        editTextAdd = (EditText) findViewById(R.id.editTextAddress);
	 
	        btnPrev = (Button) findViewById(R.id.btnPrev);
	        btnNext = (Button) findViewById(R.id.btnNext);
	        btnSave = (Button) findViewById(R.id.btnSave);
	        btnDelete = (Button) findViewById(R.id.btnDelete);
	 
//	        btnNext.setOnClickListener(this);
//	        btnPrev.setOnClickListener(this);
//	        btnSave.setOnClickListener(this);
//	        btnDelete.setOnClickListener(this);
	        
	        btnNext.setOnClickListener(new OnClickListener()
	        {

				@Override
				public void onClick(View arg0)
				{
					 moveNext();	
				}

			});
	        

	        btnPrev.setOnClickListener(new OnClickListener()
	        {

				@Override
				public void onClick(View arg0)
				{
					movePrev();	
				}

			});

	 
	        c = db.rawQuery(SELECT_SQL, null);
	        c.moveToFirst();
	        showRecords();
		    
			
			
	}
	
	
	protected void moveNext() {
        if (!c.isLast())
            c.moveToNext();
 
        showRecords();
    }
 
    protected void movePrev() {
        if (!c.isFirst())
            c.moveToPrevious();
 
        showRecords();
 
    }
    
    protected void saveRecord() 
    {
        String name = editTextName.getText().toString().trim();
        String add = editTextAdd.getText().toString().trim();
        String id = editTextId.getText().toString().trim();
 
        String sql = "UPDATE contact SET name='" + name + "', address='" + add + "' WHERE id=" + id + ";";
 
        if (name.equals("") || add.equals("")) {
            Toast.makeText(getApplicationContext(), "You cannot save blank values", Toast.LENGTH_LONG).show();
            return;
        }
 
        db.execSQL(sql);
        Toast.makeText(getApplicationContext(), "Records Saved Successfully", Toast.LENGTH_LONG).show();
        c = db.rawQuery(SELECT_SQL, null);
        c.moveToPosition(Integer.parseInt(id));
    }
	
    private void deleteRecord() 
    {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Are you sure you want delete this person?");
 
        alertDialogBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        String id = editTextId.getText().toString().trim();
 
                        String sql = "DELETE FROM contact WHERE id=" + id + ";";
                        db.execSQL(sql);
                        Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();
                        c = db.rawQuery(SELECT_SQL,null);
                    }
                });
 
        alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
 
                    }
                });
 
 
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
 
    }
	
	 protected void openDatabase()
	 {
	        db = openOrCreateDatabase("pat06", Context.MODE_PRIVATE, null);
	 }
	
	 protected void showRecords() 
	 {
	        String id = c.getString(0);
	        String name = c.getString(1);
	        String add = c.getString(2);
	        editTextId.setText(id);
	        editTextName.setText(name);
	        editTextAdd.setText(add);
	 }
	

	   
}