package com.androidbegin.buttonclicktutorial;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class repeat_list extends Activity
{

	private ArrayList<String> stafid = new ArrayList<String>();
	private ArrayList<String> nama = new ArrayList<String>();
	private ArrayList<String> jbt = new ArrayList<String>();

	private ListView userList;
	private AlertDialog.Builder Dialog;

	
	DB_alarm db;
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repeat_display_list);
		
		userList = (ListView) findViewById(R.id.List);
		
        ActionBar actionBar = getActionBar();
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("Listing des repeat");

		
		db = new DB_alarm(getApplicationContext());
		
		
		//click to update data
		userList.setOnItemClickListener(new OnItemClickListener()
		{
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
					{

						Intent i = new Intent(getApplicationContext(), infos_repeat.class);
						i.putExtra("stafid", stafid.get(arg2));
						startActivity(i);
					}
		});
				
		//long-press to update data
		userList.setOnItemLongClickListener(new OnItemLongClickListener() 
		{

					public boolean onItemLongClick(AdapterView<?> arg0, View arg1,	final int arg2, long arg3)
					{
						
						//invoking AlertDialog box
						Dialog = new AlertDialog.Builder(repeat_list.this);
						Dialog.setTitle("Update/Delete staff " + nama.get(arg2));
						Dialog.setMessage("Do you want to update/delete the record?(Hit back to cancel)");
						
						//user select UPDATE
						Dialog.setNegativeButton("UPDATE",new DialogInterface.OnClickListener() 
						{

								public void onClick(DialogInterface dialog,	int which)
								{
									//Update record selected
									Intent i = new Intent(getApplicationContext(), edit_infos_repeat.class);
									i.putExtra("stafid", stafid.get(arg2));
									startActivity(i);
									dialog.cancel();
								}
						});//end UPDATE
						
						//user select DELETE
						Dialog.setPositiveButton("DELETE", new DialogInterface.OnClickListener() 
						{

									public void onClick(DialogInterface dialog,int which)
									{
										//dataBase.delete(DBHelper.TABLE_NAME, DBHelper.STAFID + "="+ stafid.get(arg2), null);
										Toast.makeText(getApplicationContext(),	nama.get(arg2) + " is deleted.", Toast.LENGTH_SHORT).show();
										displayData();
										dialog.cancel();
									}
						});//end DELETE
						
						AlertDialog alert = Dialog.create();
						alert.show();

						return true;
					}
					
				});//end setOnItemLongClickListener

		

		
	}	

	
	@Override
	protected void onResume() 
	{
		//refresh data for screen is invoked/displayed
		displayData();
		super.onResume();
	}
	
	
	/**
	 * displays data from SQLite
	 */
	private void displayData() 
	{
//		dataBase = mHelper.getWritableDatabase();
//		Cursor mCursor = dataBase.rawQuery("SELECT * FROM "	+ DBHelper.TABLE_NAME, null);
		
		
		String requete = "SELECT * FROM table_repeat";
		
		Cursor mCursor = db.DB_query(requete);
		int val = mCursor.getCount();
	    Log.i("**************** => ", String.valueOf(val));

		//reset variables
		stafid.clear();
		nama.clear();
		jbt.clear();
		
		//fetch each record
		if (mCursor.moveToFirst()) 
		{
			do {
					//get data from field 
					stafid.add(mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_ID)));
					nama.add(mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_NAME)));
					jbt.add(mCursor.getString(mCursor.getColumnIndex(DB_alarm.COLUMN_REPEAT_INTERVAL)));
				
			} while (mCursor.moveToNext());
			
			//display to screen
			DisplayAdapter_repeat_list disadpt = new DisplayAdapter_repeat_list(repeat_list.this, stafid, nama, jbt);
			userList.setAdapter(disadpt);
			mCursor.close();

		}
		
		/*
		public static final String TABLE_REPEAT = "table_repeat";
		public static final String COLUMN_REPEAT_ID = "_id";
		public static final String COLUMN_REPEAT_NAME = "repeat_name";	 
		public static final String COLUMN_REPEAT_TIME = "repeat_time";
		public static final String COLUMN_REPEAT_DAYS = "repeat_days";
		public static final String COLUMN_REPEAT_INTERVAL = "repeat_interval";
		public static final String COLUMN_REPEAT_ACTIVE = "alarm_active";	
		public static final String COLUMN_REPEAT_VIBRATE = "option_vibrate";
		public static final String COLUMN_REPEAT_RINGTONE = "option_ringtone";
		public static final String COLUMN_REPEAT_CREATED_TIME = "created_time";
		public static final String COLUMN_REPEAT_MODIFIED_TIME = "modified_time";
		*/
		
		
	}
	
	/**************************************************/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.remind_menu, menu);
		return true;
	}


	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	      switch (item.getItemId())
	      {

	      		case android.R.id.home:
	      			this.finish();
	      			return true;
	      			
	      		case R.id.add_event:
	      			Toast.makeText(getApplicationContext(),"Add event",Toast.LENGTH_SHORT).show();
	      			Intent Intent02 = new Intent(repeat_list.this, config_alarm.class);
	      			startActivity(Intent02);
	      			return true;
		     
	      		case R.id.preferences:
	      			Toast.makeText(getApplicationContext(),"Not defined",Toast.LENGTH_SHORT).show();
	      			//Intent Intent01 = new Intent(repeat_list.this, SettingsActivity.class);
	      			//startActivity(Intent01);
	      			return true;
		    	
		    
	      		default:
		          return super.onOptionsItemSelected(item);
		 }
	}

	
}
