/**
 * EventsTableWeekly.java
 */
package net.sf.memoranda.ui;

import java.awt.Component;
import java.awt.Font;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

import net.sf.memoranda.Event;
import net.sf.memoranda.EventsManager;
import net.sf.memoranda.date.CalendarDate;
import net.sf.memoranda.date.CurrentDate;
import net.sf.memoranda.date.DateListener;

/**
 *
 */
public class EventsTableMonthly extends AbstractEventsTable{

    public static final int EVENT = 100;

    Vector[] events;
    Vector[] days;
    /**
     * Constructor for EventsTable.
     */
    public EventsTableMonthly() {
        super();
        events = new Vector[5];
        days = new Vector[5];
        for(int i = 0;i<events.length;i++)
        	events[i] = new Vector();
        for(int i = 0;i<days.length;i++)
        	days[i] = new Vector();
        
        setCellSelectionEnabled(true);
        setModel(new EventsTableModel());
        
        initTable(CurrentDate.get());
        this.setShowGrid(false);
        CurrentDate.addDateListener(new DateListener() {
            public void dateChange(CalendarDate d) {
                //updateUI();
                initTable(d);
            }
        });
    }

    public void initTable(CalendarDate d) {
    	for(int i = 0;i<events.length;i++)
        	events[i] = new Vector();
    	for(int i = 0;i<days.length;i++)
        	days[i] = new Vector();
    	Calendar cal = d.getCalendar();
    	int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    	for(int i = 0;i<daysInMonth;i++){
    		cal.set(Calendar.DAY_OF_MONTH, i+1);
    		 Collection evs = EventsManager.getEventsForDate(new CalendarDate(cal));
    		 for(Object o:evs){
    			 events[i/7].add(o);
    			 days[i/7].add(cal.clone());
    		 }
    	}
    	
        for(int i = 0;i<getColumnModel().getColumnCount();i++){
        	getColumnModel().getColumn(i).setPreferredWidth(120);
            getColumnModel().getColumn(i).setMaxWidth(120);
        }
        clearSelection();
        updateUI();
    }

    public void refresh() {
    	
        initTable(CurrentDate.get());
    }
    public Event getEventAt(int row, int column){
    	try{
    		return (Event)events[column].get(row);
    	}
    	catch(Exception e){
    		return null;
    	}
    }
     public TableCellRenderer getCellRenderer(int row, int column) {
        return new javax.swing.table.DefaultTableCellRenderer() {

            public Component getTableCellRendererComponent(
                JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
                Component comp;
                comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                Event ev = getEventAt(row, column);
                
                if(ev == null) return comp;
                Calendar cal = (Calendar)days[column].get(row);
                comp.setForeground(java.awt.Color.gray);
                
                if (ev.isRepeatable())
                    comp.setFont(comp.getFont().deriveFont(Font.ITALIC));
                if (CalendarDate.today().getCalendar().before(cal)) {
                  comp.setForeground(java.awt.Color.black);
                }                
                else if (CurrentDate.get().equals(CalendarDate.today())) {
                  if (ev.getTime().after(new Date())) {
                    comp.setForeground(java.awt.Color.black);
                    //comp.setFont(new java.awt.Font("Dialog", 1, 12));
                    comp.setFont(comp.getFont().deriveFont(Font.BOLD));
                  }
                }
                return comp;
            }
        };

    }

    class EventsTableModel extends AbstractTableModel {

    	String[] columnNames = {"1st-7th","8th-14th","15th-21st","22nd-28th","29th-31st"};

        EventsTableModel() {
            super();
        }

        public int getColumnCount() {
            return columnNames.length;
        }
        private int maxEventsForDay(){//returns the number of events for the day of the month with the most events
        	int max = 0;
        	for(Vector v: events)
        		if(v.size() > max) max = v.size();
        	return max;
        }
        public int getRowCount() {
			int i;
			try {
				i = maxEventsForDay();
			}
			catch(NullPointerException e) {
				i = 1;
			}
			return i;
        }

        public Object getValueAt(int row, int col) {
        	Event ev = getEventAt(row,col);
        	if(ev == null) return "";
        	Calendar cal = (Calendar)days[col].get(row);
        	String res = "";
        	switch(cal.get(Calendar.DAY_OF_WEEK)){
        		case Calendar.SUNDAY: res += "SU";
        			break;
        		case Calendar.MONDAY: res += "MO";
        			break;
        		case Calendar.TUESDAY: res += "TU";
        			break;
        		case Calendar.WEDNESDAY: res += "WE";
        			break;
        		case Calendar.THURSDAY: res += "TH";
        			break;
        		case Calendar.FRIDAY: res += "FR";
        			break;
        		case Calendar.SATURDAY: res += "SA";
        			break;
        		default: res += "??";
        	}
        	String dOM = cal.get(Calendar.DAY_OF_MONTH) + "";
        	switch(cal.get(Calendar.DAY_OF_MONTH)%10){
        		case 1: dOM += "st";
        			break;
        		case 2: dOM += "nd";
        			break;
        		case 3: dOM += "rd";
        			break;
        		default:dOM+="th";
        	}
        	res+= " / " + dOM  + ": ";
        	if(ev.getText().equals("")) res+= "EVENT";
        	else res += ev.getText();
        	return res;       		
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }
    }
}