import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

class Client extends JFrame
{
	//main panel objects
	private	JPanel						dataPanel;
	private JPanel 						topPanel;
	private JPanel						bottomPanel;
	private	static JTable				table;
	private	JScrollPane 				scrollPane;
	private String[] 					columnNames;
	private static AbstractTableModel 	tableModel;
	private static String[][] 			tableData;

	private	JLabel						labelMeeting;
	private JTextField					textMeeting;
	private JLabel						labelTime;
	private JTextField					textTime;
	private JButton						btnAdd;
	private JButton						btnDelete;
	private JButton						btnQuery;
	private JButton						btnShowAll;

	public Client() {

		this.setTitle( "Diary Client" );
		this.setSize( 395, 515 );

		// create panels
		this.dataPanel = new JPanel();
		this.dataPanel.setLayout( new BorderLayout() );

		this.topPanel = new JPanel();
		this.topPanel.setLayout( new FlowLayout() );

		this.bottomPanel = new JPanel();
		this.bottomPanel.setLayout( new FlowLayout() );

		this.getContentPane().setLayout( new BorderLayout() );
		this.getContentPane().add( this.topPanel, BorderLayout.NORTH );
		this.getContentPane().add( this.dataPanel, BorderLayout.CENTER );
		this.getContentPane().add( this.bottomPanel, BorderLayout.SOUTH );

		// build the panes
		this.buildColumnNames();
		this.buildTableData();
		this.buildTableModel();
		this.buildControls();
		
		// set menu
		this.setJMenuBar( this.setupMenu() );

		//make table
		table = new JTable( tableModel );
		scrollPane = new JScrollPane( table );
		this.dataPanel.add( scrollPane, BorderLayout.CENTER );

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main( String[] args ) {
		Client c = new Client();
		c.setVisible( true );
	}

	
	public static int getSlotFromTime(String time) {
		Integer tval = new Integer(time.substring(0,2));
		int theval = tval.intValue();
		theval--;
		return theval;
	}

	public static void notifyMeeting(String time, String meeting) {
		int slot = Client.getSlotFromTime(time);
		
		String msg = "Notification: Meeting ";
		if ( meeting == "" ) 
			msg = msg + tableData[slot][1] + " at " + time + " deleted.";
		else
			msg = msg + meeting + " added at " + time + ".";
			
		JOptionPane.showConfirmDialog((Component)null, msg, "Notification", JOptionPane.DEFAULT_OPTION);	
		
		tableData[slot][1] = meeting;
		refresh();
	}
	
	/* BELOW THIS LINE IS ENTIRELY FOR UI BASED STUFFS */

	public static void refresh() {
		tableModel.fireTableDataChanged();
	}

	public JMenuBar setupMenu() {
		JMenuBar menuBar = new JMenuBar();

		JMenu diary = new JMenu("Diary");
		menuBar.add( diary );

		JMenuItem quit = new JMenuItem("Quit");
		diary.add(quit);

		quit.addActionListener( new ActionListener() { 
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		return menuBar;
	}

	public void buildColumnNames() {
		this.columnNames = new String[2];
		this.columnNames[0] = "Time";
		this.columnNames[1] = "Meeting Name";
	}
	
	private void buildTableModel() {
		tableModel = new AbstractTableModel() {
			public int getColumnCount() { return columnNames.length; }
			public int getRowCount() { return tableData.length;}
			public Object getValueAt(int row, int col) {return tableData[row][col];}
            public String getColumnName(int column) {return columnNames[column];}
            public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
            public boolean isCellEditable(int row, int col) {return (1==2);}
			public void setValueAt(String aValue, int row, int column) { tableData[row][column] = aValue; }
		};
	}

	private void buildTableData() {
		tableData = new String[24][2];
		Integer tval;
		String sval;
		for ( int i=0; i<24; i++ ) {
			tval = new Integer((i+1)*100);
			sval = tval.toString();
			if ( sval.length() == 3 ) 
				sval = "0" + sval;
		
			tableData[i][0] = sval;
			tableData[i][1] = "";
		}
	}
	
	private void buildControls() {

		this.labelMeeting = new JLabel("Meeting:");
		this.textMeeting = new JTextField("", 15);
		this.labelTime = new JLabel("Time:");
		this.textTime = new JTextField("", 4);

		this.btnAdd = new JButton("Add");
		ActionListener al = new ActionListener() { 
			public void actionPerformed(ActionEvent e) {

				String time = textTime.getText();
				String name = textMeeting.getText();
				if ( time.length() == 0 || name.length() == 0 ) {
					JOptionPane.showConfirmDialog((Component)null, "Please enter values for meeting and time!", "Error", JOptionPane.DEFAULT_OPTION);	
					return;
				}
		
				// kill values in texts
				textTime.setText("");
				textMeeting.setText("");
				
				// TODO: ADD REAL MEETING ADDER
				
				// temporary notify
				notifyMeeting(time, name);
			}
		};
		this.btnAdd.addActionListener(al);
		
		this.topPanel.add( this.labelMeeting );
		this.topPanel.add( this.textMeeting );
		this.topPanel.add( this.labelTime );
		this.topPanel.add( this.textTime );
		this.topPanel.add( this.btnAdd );
		
		// bottom panel
		this.btnDelete = new JButton("Delete Meeting");
		this.btnDelete.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selections[] = table.getSelectedRows();
				if ( selections.length == 0 )
					JOptionPane.showMessageDialog(topPanel, "You didn't select anything to delete!");
				
				if ( JOptionPane.showConfirmDialog((Component)null, "Delete selected meeting(s)?", "alert", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION )
					return;

				
				for ( int i=0; i<selections.length; i++) {
				
					// TODO: REQUEST DELETES ON TIME ITEMS
					
					System.out.println("Delete " + tableData[selections[i]][0]);					// temporary
					notifyMeeting(tableData[selections[i]][0], "");
				}


			}
		});

		this.btnQuery = new JButton("Query Time");
		this.btnQuery.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name = JOptionPane.showInputDialog((Component)null, "Enter the time to check: ","Time Query", JOptionPane.PLAIN_MESSAGE);
				if ( name != null ) { 
				
					// TODO: QUERY THE TIME HERE
					
					JOptionPane.showConfirmDialog((Component)null, name, "alert", JOptionPane.DEFAULT_OPTION);
				}
			}
		});

		this.btnShowAll = new JButton("List All");
		this.btnShowAll.addActionListener( new ActionListener() {
			public void actionPerformed( ActionEvent e ) {
			
				// TODO: CALL SHOW ALL AND LOOP THROUGH RESULTS AND DISPLAY

				JOptionPane.showConfirmDialog((Component)null, "YES IT IS\nMultieline?", "alert", JOptionPane.DEFAULT_OPTION);
			}
		});

		this.bottomPanel.add( this.btnDelete );
		this.bottomPanel.add( this.btnQuery );
		this.bottomPanel.add( this.btnShowAll );
	}

}
