package com.example.preferences02;


import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class MainActivity extends AppCompatActivity 
{
		
		private String id;//to hold selected stafid 
		private DBHelper mHelper;
		private SQLiteDatabase dataBase;

		//variables to hold staff records
		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 build;

 
		  public static final String[] monthArr = new String[]{ "Month","Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"  };
			
		
		@Override
		public void onCreate(Bundle savedInstanceState) 
		{
			
	 
				super.onCreate(savedInstanceState);
				setContentView(R.layout.activity_display_list);
				
				userList = (ListView) findViewById(R.id.List);

				mHelper = new DBHelper(this);
				
				//click to update data
				userList.setOnItemClickListener(new OnItemClickListener() 
				{
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
						{
							Intent i = new Intent(getApplicationContext(), ViewRecord.class);
							i.putExtra("stafid", stafid.get(arg2));
							startActivity(i);
						}
				});//end setOnItemClickListener
				
				//long-press to update data
				userList.setOnItemLongClickListener(new OnItemLongClickListener() 
				{

						public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int arg2, long arg3)
						{
						
								//invoking AlertDialog box
								build = new AlertDialog.Builder(MainActivity.this);
								build.setTitle("Update/Delete staff " + nama.get(arg2));
								build.setMessage("Do you want to update/delete the record?(Hit back to cancel)");
							
								//user select UPDATE
								build.setNegativeButton("UPDATE", new DialogInterface.OnClickListener()
								{
										public void onClick(DialogInterface dialog,	int which)
										{
											//Update record selected
											Intent i = new Intent(getApplicationContext(), UpdateRecord.class);
											i.putExtra("stafid", stafid.get(arg2));
											startActivity(i);
											dialog.cancel();
										}
								});//end UPDATE
							
								//user select DELETE
								build.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 = build.create();
								alert.show();
			
								return true;
					}
				});//end setOnItemLongClickListener

				
		}
 
		
		protected void onListItemClick(ListView l, View v, int position, long id) 
		{
			//openContextMenu(v);
			Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_LONG).show();

			
		}
		
		@Override
		protected void onResume() 
		{
			//refresh data for screen is invoked/displayed
			//displayData();
			Toast.makeText(getApplicationContext(), "TAP to view complete info, \n LONG-TAP to update/delete...", Toast.LENGTH_LONG).show();
			super.onResume();
		}

		/**
		 * displays data from SQLite
		 */
		private void displayData() 
		{
				dataBase = mHelper.getWritableDatabase();
				//the SQL command to fetched all records from the table
				Cursor mCursor = dataBase.rawQuery("SELECT * FROM "	+ DBHelper.TABLE_NAME, null);
		
				//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(DBHelper.STAFID)));
						nama.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.NAMA)));
						jbt.add(mCursor.getString(mCursor.getColumnIndex(DBHelper.JBT)));
						
					} while (mCursor.moveToNext());
					//do above till data exhausted 
				}
				
				//display to screen
				DisplayAdapter disadpt = new DisplayAdapter(MainActivity.this, stafid, nama, jbt);
				userList.setAdapter(disadpt);
				mCursor.close();
		}//end displayData

		

		@Override
		public boolean onCreateOptionsMenu(Menu menu) 
		{
			// Inflate the menu; this adds items to the action bar if it is present.
			getMenuInflater().inflate(R.menu.menu_main, menu); 
			return true;
		}
		
		@Override
		public boolean onOptionsItemSelected(MenuItem item)
		 { 
			    	switch(item.getItemId())
			    	{
			    	
			    	case R.id.menu_debug_list:
	    				Intent i11 = new Intent(getApplicationContext(), debug_alarm_listing.class);
	    				startActivity(i11);
			    		Toast.makeText(getApplicationContext(), "debug on going ...", Toast.LENGTH_LONG).show();
			    		break;
			    		
		    			case R.id.menu_preferernces:
		    				Intent i1 = new Intent(getApplicationContext(), debug_preferences.class);
		    				startActivity(i1);
				    		break;
	
		    				
			    		case R.id.menu_add:
			    			Intent i2 = new Intent(getApplicationContext(), debug_alarm_listing.class);
			     			startActivity(i2);
				    		break;

			     			
			     	/*		
				        case R.id.add:
				            count=(TextView)findViewById(R.id.textView);
				            count.setText("Add is clicked");
				            return(true);
				           
				       /* case R.id.reset:
				            count=(TextView)findViewById(R.id.textView);
				            count.setText("Nothing is selected");
				            return(true);
				            */
				        case R.id.about:
				            Toast.makeText(this, R.string.about_toast, Toast.LENGTH_LONG).show();
				    		break;

				        case R.id.exit:
				            finish();
				    		break;

			    	}
			    	return(super.onOptionsItemSelected(item));
		    }

		

}