import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;

//This class implements a monthly calendar

public class CalendarBox extends Panel implements ActionListener {


  public CalendarBox(Label monthlabel)
  {
    super();
   
    label = monthlabel;

    Year curYear = new Year();

    GregorianCalendar todaysCal = new GregorianCalendar();
 
    GregorianCalendar firstCalendar = new GregorianCalendar(
				todaysCal.get(Calendar.YEAR),
				todaysCal.get(Calendar.MONTH),
				1);
    
    
    TodaysDate today = new TodaysDate();
    

    //find what day of the week does this month start on
    firstDayOfMonth = firstCalendar.get(Calendar.DAY_OF_WEEK);

    //set the state of the calendar
    currentDate = todaysCal.get(Calendar.DATE);
    currentMonth = todaysCal.get(Calendar.MONTH);
    currentYear = todaysCal.get(Calendar.YEAR);

    label.setText(today.intToMonth(currentMonth));
    
    //find the number of days in this month
    nDaysInMonth = curYear.getNumDaysInMonth(currentMonth);
   
    //display the calendar
    setLayout(new GridLayout(6,7));
    for (int i=1; i<42; i++)
      addButton(i); 
           

  }
 

  //redraw the calendar based on the recently changed internal state
  private void updateCalendar()
  {
  
    System.out.println("inside update");
    System.out.println("currentmonth,currentyear " + currentMonth + " " + currentYear);
    Year year = new Year();
    GregorianCalendar newCal = new GregorianCalendar(currentYear,
						     currentMonth,
						     1);
    

    firstDayOfMonth = newCal.get(Calendar.DAY_OF_WEEK);
    
    System.out.println("firstdayinmonth " + firstDayOfMonth);

    nDaysInMonth = year.getNumDaysInMonth(currentMonth);
    this.removeAll();
    
   
    for (int i=1; i<42; i++)
      addButton(i); 
    validate();

    TodaysDate today = new TodaysDate();
    label.setText(today.intToMonth(currentMonth));

  }

  public void nextMonth()
  {
    System.out.println("inside nextmonth");
    if (currentMonth == 11)
      {
	currentMonth=0;
	currentYear++;
      }
    else
      currentMonth++;

   
    updateCalendar();

  }

  public void previousMonth()
  {
    if (currentMonth == 0)
      {
	currentMonth = 11;
	currentYear--;
      }
    else
      {
	currentMonth--;
      }
	updateCalendar();
  }

  public void showCurrentMonth()
  {

  }
  private void addButton(int buttonNumber)
  {
    
    Button b;

    if ((buttonNumber < firstDayOfMonth) || 
	(buttonNumber > (nDaysInMonth + firstDayOfMonth-1)))
      {
	b = new Button(Integer.toString(buttonNumber));
	this.add(b);
	b.setVisible(false);
      }
    else
      {
	b = new Button(Integer.toString(buttonNumber-(firstDayOfMonth-1)));
	this.add(b);
	b.addActionListener(this); 
	b.setBackground(Color.white);
      }
  }
  
  public void actionPerformed(ActionEvent evt)
  {
    
  }
  

  private int firstDayOfMonth;
  private int nDaysInMonth;
  private int currentDate;
  private int currentMonth;
  private int currentYear;
  private Label label;
 
}







