//package com.team5.aLife.BaseStation;
//
//import java.util.ArrayList;
//
//import android.app.AlertDialog;
//import android.app.ListActivity;
//import android.app.ProgressDialog;
//import android.content.Context;
//import android.content.DialogInterface;
//import android.content.Intent;
//import android.database.Cursor;
//import android.os.Bundle;
//import android.util.Log;
//import android.view.ContextMenu;
//import android.view.LayoutInflater;
//import android.view.MenuItem;
//import android.view.View;
//import android.view.ViewGroup;
//import android.view.ContextMenu.ContextMenuInfo;
//import android.widget.ArrayAdapter;
//import android.widget.ListView;
//import android.widget.TextView;
//import android.widget.AdapterView.AdapterContextMenuInfo;
//
//import com.team5.aLife.BaseStation.DBHandler.DB_Devices;
//import com.team5.aLife.BaseStation.DBHandler.DB_Notifications_Setup;
//
//public class NotificationHistoryActivity extends ListActivity {
//	
//	// TODO: See http://stackoverflow.com/questions/3044049/android-listview-onclick-event-in-the-tabs for the FLIPPER???
//	
//    private static final String TAG = "NotificationActivity:  ";
//  
//    private ProgressDialog m_ProgressDialog = null; 						// progress dialog
//    private ArrayList<ALifeNotificationLogEntry> notificationList = null;	// holds our device list
//    private NotificationAdapter dAdapter;									// custom class that extends ArrayAdapter
//    private DBHandler db = new DBHandler(this);								// database handler object
//
//    private Intent myIntent;
//    
//    private final String delimiter = "[&]+";
//    
//    /** Called when the activity is first created. */
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.notification);
//        
//        registerForContextMenu(getListView());
//        
//        notificationList = new ArrayList<ALifeNotificationLogEntry>();			// create a new array of devices
//        // Create the row adapter for the ListView        
//        this.dAdapter = new NotificationAdapter(this, R.layout.listviewrow, notificationList);
//        // Set the ROW adapter for the ListView
//        setListAdapter(this.dAdapter);
//        // Bind the Device selection
//
//        // Get the device list from the server in JSON format
//        refreshNotifications();
//                
//    } // end onCreate
//
//	protected void onListItemClick(ListView l, View v, int position, long id){
//		
//		selectedNotification = notificationList.get(position);
//		
//		switch (getDeviceType(selectedNotification.getDeviceID())){	   
//	 	   case Device.DEVICE_TYPE_THERMOSTAT:
//	 		   Log.i(TAG + "onListItemClick", "Selected a thermostat");
//	 		   
//	 		   break;
//	 	  case Device.DEVICE_TYPE_POWER_MONITOR_SWITCH:
//	 		   Log.i(TAG + "onListItemClick", "Selected a power monitor with switch");
//	 		   myIntent = new Intent();
//		       myIntent.setClassName("com.team5.aLife.BaseStation",
//		                             "com.team5.aLife.BaseStation.NotificationPowerActivity");
//		       startActivity(myIntent);
//	 		   break;
//	 	  case Device.DEVICE_TYPE_SWITCH:
//	 		   Log.i(TAG + "onListItemClick", "Selected a switch device");
//	 		   
//	 		   break;
//	 	  case Device.DEVICE_TYPE_WIRELESS_SENSOR:
//	 		   Log.i(TAG + "onListItemClick", "Selected a wireless sensor");
//	 		   
//	 		   break;
//	 	  default: 
//	 		   super.onListItemClick(l, v, position, id);
//	 	  	   break;
//		}
//    	
//    }
//
//    public void onResume()
//    {
//    	super.onResume();
//    	refreshNotifications();
//    }
//    
//    private void refreshNotifications(){
//    	// Since this is a network action show the progress dialog
//        m_ProgressDialog = ProgressDialog.show(NotificationHistoryActivity.this,    
//                "Please wait...", "Retrieving data ...", true);
//        
//        // reset the device list to empty
//        notificationList.clear();
//        
//        // Holds all the devices
//    	
//        ALifeNotificationSetup tempNotification;
//    	
//    	db.open();
//    	
//    	Cursor c = db.getAllNotificationsForUser(LoginActivity.activeUser.getUserID());
//    	
//    	/* Get the indices of the Columns we will need */
//        int IDCol = c.getColumnIndex(DB_Notifications_Setup._ID);
//        int deviceIDCol = c.getColumnIndex(DB_Notifications_Setup.DEVICE_ID);
//        int userIDCol = c.getColumnIndex(DB_Notifications_Setup.USER_ID);
//        int notificationTypeCol = c.getColumnIndex(DB_Notifications_Setup.NOTIFICATION_TYPE);
//        int triggerConditionCol = c.getColumnIndex(DB_Notifications_Setup.TRIGGERING_CONDITION);
//        
//        if (c != null)
//	    	if (c.moveToFirst())
//	    	{	
//	    		do {
//	    		//  Retrieve the values of the Entry the Cursor is pointing to. 
//	            int ID = c.getInt(IDCol);
//	            int deviceID = c.getInt(deviceIDCol);
//	            int userID = c.getInt(userIDCol);
//	            int notificationType = c.getInt(notificationTypeCol);
//	            String triggeringCondition = c.getString(triggerConditionCol);
//	            
//	            tempNotification = new ALifeNotificationSetup(ID, deviceID, userID, notificationType, 
//	            										  triggeringCondition);
//	            
//	            notificationList.add(tempNotification);
//	            
//	    		}while (c.moveToNext());
//	    		
//	            c.close();
//	    		
//	    	}
//        
//    	db.close(); 
//        
//        // Turn off the progess dialog
//        m_ProgressDialog.dismiss();
//        
//        // Let the ListView know there is new data to display
//        Log.i(TAG+"refreshDevices", "I have " + notificationList.size() + " devices.  About to call updateListView");
//        runOnUiThread(updateListView);
//
//    }
//    
//    private Runnable updateListView = new Runnable() {
//
//        @Override
//        public void run() {
//        	Log.i(TAG+"updateListView.run()", "updateListView is being run");
////            if(notificationList != null && notificationList.size() > 0)
////            {
////                dAdapter.notifyDataSetChanged();
////                // Clear all items so they don't start stacking up
////                dAdapter.clear();
////                Log.i(TAG+"updateListView.run()", "notificationList is not null and has size > 0.  notificationList size is " + notificationList.size());
////                for(int i=0; i<notificationList.size(); i++)
////                {
////                	dAdapter.add(notificationList.get(i));
////                	Log.i(TAG+"updateListView.run()", "Iterating through device list " + i);
////                }
////            }
//            m_ProgressDialog.dismiss();
//            dAdapter.notifyDataSetChanged();
//            Log.i(TAG+"updateListView.run()", "Should be done updating adapter");
//        }
//    };
//    
//    @Override
//    public void onCreateContextMenu(ContextMenu menu, View v,
//                                    ContextMenuInfo menuInfo) {
//      super.onCreateContextMenu(menu, v, menuInfo);
//      menu.setHeaderTitle("Options");
//      menu.add(0, CONTEXT_MENU_DELETE_NOTIFICATION, 0, "Delete User");
//    }
//    
//    @Override
//    public boolean onContextItemSelected(MenuItem item) {
//    	final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//        switch (item.getItemId()) {
//        
//        case CONTEXT_MENU_DELETE_NOTIFICATION:
//          
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//    			builder.setTitle("Delete Notification")
//    		   .setMessage("All changes are final, do you want to proceed?")
//    	       .setCancelable(false)
//    	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//    	           public void onClick(DialogInterface dialog, int id) 
//    	           {
//    	        	 db.open();
//    	           
//    	        	 db.deleteNotification(notificationList.get((int)info.id).getDeviceID());
//    	           
//    	        	 db.close();
//    	        	   
//    	        	 refreshNotifications();
//    	           }
//    	       })
//    	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
//    	           public void onClick(DialogInterface dialog, int id) 
//    	           {
//    	                dialog.cancel();
//    	           }
//    	       });
//    			builder.show();
//        return true;
//        default:
//          return super.onContextItemSelected(item);
//      }
//    }
//    
//           
//    class NotificationAdapter extends ArrayAdapter<ALifeNotificationLogEntry> {
//
//        private ArrayList<ALifeNotificationLogEntry> items;
//
//        public NotificationAdapter(Context context, int textViewResourceId, ArrayList<ALifeNotificationLogEntry> items) {
//        		// Constructor for super class ArrayAdapter
//                super(context, textViewResourceId, items);
//                this.items = items;
//        }
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//                View v = convertView;
//                if (v == null) {
//                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//                    v = vi.inflate(R.layout.listviewrow, null);
//                }
//                ALifeNotificationLogEntry o = items.get(position);
//                if (o != null) {
//                        TextView tt = (TextView) v.findViewById(R.id.NotificationHistoryDeviceName);
//                        TextView bt = (TextView) v.findViewById(R.id.NotificationHistoryStatus);
//                        TextView time = (TextView) v.findViewById(R.id.NotificationHistoryBy);
//                        if (tt != null) {
//                              tt.setText(getDeviceName(o.getDeviceID()));                            
//                        }
//                        if(bt != null){
//                        	
//                        	int deviceType = getDeviceType(o.getDeviceID());
//                        	
//                        	if (deviceType == Device.DEVICE_TYPE_POWER_MONITOR_SWITCH )
//                        	{
//                        		bt.setText("Device is on");
//                        	}
//                        	else if(deviceType == Device.DEVICE_TYPE_SWITCH )
//                        	{
//                        		bt.setText("Garage is open");
//                        	}
//                        	else if(deviceType == Device.DEVICE_TYPE_THERMOSTAT)
//                        	{
//                        		bt.setText("Outside temperate is 5 deg off");
//                        	}
//                        	else if(deviceType == Device.DEVICE_TYPE_WIRELESS_SENSOR )
//                        	{
//                        		bt.setText("Garage is open");
//                        	}
//                        	
//                        }
//                        if (time != null) {
//                        	time.setText(parseCondtionToString(o.getTriggeringCondition()));
//                        }
//                }
//                return v;
//        }
//        
//
//    } // End Device Adapter
//    
//    private String getDeviceName(int deviceID) {
//		
//		String deviceName = "";
//		
//		db.open();
//		
//		Cursor c = db.getDevice(deviceID);
//		
//		/* Get the indices of the Columns we will need */
//        int deviceNameCol = c.getColumnIndex(DB_Devices.NAME);
//        
//        // Check if our result was valid. 
//        if (c != null) {
//        	
//                // Check if at least one Result was returned. 
//                if (c.moveToFirst()) 
//                {	
//                	
//                    //  Retrieve the values of the Entry the Cursor is pointing to. 
//                    deviceName = c.getString(deviceNameCol);
//                	
//                }
//        }
//		
//		c.close();
//		db.close();
//		
//		return deviceName;
//	}
//    
//} // end customListView
