/*
**  CalendarView.m
**
**  Copyright (c) 2003
**
**  Author: Yen-Ju Chen <yjchenx@hotmail.com>
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "CalendarView.h"
#include "ClockView.h"

@implementation CalendarView

#define isLeapYear(year) (((year % 4) == 0 && ((year % 100) != 0)) || (year % 400) == 0)

static short numberOfDaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

- (NSCalendarDate *) date
{
  return date;
}

- (void) setDate: (NSCalendarDate *) newDate
{
   int i, currentDay, currentMonth, currentYear;
   int daysInMonth, startDayOfWeek, day;
   NSCalendarDate *firstDayOfMonth;
   NSButtonCell *tempCell;

   ASSIGN(date, newDate);

   /* Date */

   [yearLabel setStringValue: [date descriptionWithCalendarFormat: @"%Y"]];
   [monthLabel setStringValue: [date descriptionWithCalendarFormat: @"%B"]];

   currentYear = [date yearOfCommonEra];
   currentMonth = [date monthOfYear];
   firstDayOfMonth = [NSCalendarDate dateWithYear: currentYear
                                            month: currentMonth
                                              day: 1
                                             hour: 0
                                           minute: 0
                                           second: 0
                                         timeZone: [NSTimeZone localTimeZone]];

   daysInMonth = numberOfDaysInMonth[currentMonth - 1];

   if ((currentMonth == 2) && (isLeapYear(currentYear)))
      daysInMonth++;

   startDayOfWeek = [firstDayOfMonth dayOfWeek];

   day = 1;

   for (i = 0; i < 42; i++)
   {
      tempCell = [dayMatrix cellWithTag: i];
      if (i < startDayOfWeek || i >= (daysInMonth + startDayOfWeek))
      {
         [tempCell setEnabled: NO];
         [tempCell setTitle: @""];
      }
      else
      {
         [tempCell setEnabled: YES];
         [tempCell setTitle: [NSString stringWithFormat: @"%d", day++]];
      }
   }

   currentDay = [date dayOfMonth];
   [dayMatrix selectCellWithTag: startDayOfWeek + currentDay - 1];

   /* Time */
   [clockView setDate: date];
}

- (id) initWithFrame: (NSRect) rect
{
   int i, j, count=0;
   NSImage *rightArrow, *leftArrow;
   NSButtonCell *dayCell, *tempCell;
   NSArray *weekArray;

   [super initWithFrame: rect];

   /* Date */

   yearLabel = [[NSTextField alloc] initWithFrame: NSMakeRect(180/2-30, rect.size.height-18, 60, 18)];
   [yearLabel setStringValue: @"This Year"];
   [yearLabel setBezeled: NO];
   [yearLabel setBackgroundColor: [NSColor windowBackgroundColor]];
   [yearLabel setEditable: NO];
   [yearLabel setSelectable: NO];
   [yearLabel setAlignment: NSCenterTextAlignment];
   [yearLabel setAutoresizingMask: NSViewMinYMargin];

   leftArrow = [NSImage imageNamed: @"common_ArrowLeftH.tiff"];
   rightArrow = [NSImage imageNamed: @"common_ArrowRightH.tiff"];

   lastYearButton = [[NSButton alloc] initWithFrame: NSMakeRect(0, rect.size.height-18, 18, 18)];
   [lastYearButton setImage: leftArrow];
   [lastYearButton setImagePosition: NSImageOnly];
   [lastYearButton setBordered: NO];
   [lastYearButton setAutoresizingMask: NSViewMinYMargin];

   nextYearButton = [[NSButton alloc] initWithFrame: NSMakeRect(180-18, rect.size.height-18, 18, 18)];
   [nextYearButton setImage: rightArrow];
   [nextYearButton setImagePosition: NSImageOnly];
   [nextYearButton setBordered: NO];
   [nextYearButton setAutoresizingMask: NSViewMinYMargin];

   [lastYearButton setTarget: self];
   [lastYearButton setAction: @selector(updateDate:)];

   [nextYearButton setTarget: self];
   [nextYearButton setAction: @selector(updateDate:)];

   [self addSubview: yearLabel];
   [self addSubview: lastYearButton];
   [self addSubview: nextYearButton];
   RELEASE(yearLabel);
   RELEASE(lastYearButton);
   RELEASE(nextYearButton);

   monthLabel = [[NSTextField alloc] initWithFrame: NSMakeRect(180/2-30, rect.size.height-36, 60, 18)];
   [monthLabel setStringValue: @"This Month"];
   [monthLabel setBezeled: NO];
   [monthLabel setBackgroundColor: [NSColor windowBackgroundColor]];
   [monthLabel setEditable: NO];
   [monthLabel setSelectable: NO];
   [monthLabel setAlignment: NSCenterTextAlignment];
   [monthLabel setAutoresizingMask: NSViewMinYMargin];

   lastMonthButton = [[NSButton alloc] initWithFrame: NSMakeRect(0, rect.size.height-36, 18, 18)];
   [lastMonthButton setImage: leftArrow];
   [lastMonthButton setImagePosition: NSImageOnly];
   [lastMonthButton setBordered: NO];
   [lastMonthButton setAutoresizingMask: NSViewMinYMargin];

   nextMonthButton = [[NSButton alloc] initWithFrame: NSMakeRect(180-18, rect.size.height-36, 18, 18)];
   [nextMonthButton setImage: rightArrow];
   [nextMonthButton setImagePosition: NSImageOnly];
   [nextMonthButton setBordered: NO];
   [nextMonthButton setAutoresizingMask: NSViewMinYMargin];

   [lastMonthButton setTarget: self];
   [lastMonthButton setAction: @selector(updateDate:)];

   [nextMonthButton setTarget: self];
   [nextMonthButton setAction: @selector(updateDate:)];

   [self addSubview: monthLabel];
   [self addSubview: lastMonthButton];
   [self addSubview: nextMonthButton];
   RELEASE(monthLabel);
   RELEASE(lastMonthButton);
   RELEASE(nextMonthButton);

   weekArray = [NSArray arrayWithObjects: @"Sun", @"Mon", @"Tue", @"Wed",
                                          @"Thr", @"Fri", @"Sat", nil];

   dayCell = [[NSButtonCell alloc] initTextCell: @""];
   [dayCell setBordered: NO];
   [dayCell setShowsStateBy: NSOnOffButton];
   [dayCell setAlignment: NSCenterTextAlignment];

   dayMatrix = [[NSMatrix alloc] initWithFrame: NSMakeRect(0, rect.size.height-130, 180, 90)
                                          mode: NSRadioModeMatrix
                                     prototype: dayCell
                                  numberOfRows: 7
                               numberOfColumns: 7];

   for (j = 0; j < 7; j++)
   {
      tempCell = [dayMatrix cellAtRow: 0 column: j];
      [tempCell setTitle: [weekArray objectAtIndex: j]];
      [tempCell setAlignment: NSCenterTextAlignment];
//      [tempCell setSelectable: NO];
      [tempCell setEnabled: NO];
   }

   RELEASE(dayCell);

   count = 0;

   for (i = 1; i < 7; i++)
      for (j = 0; j < 7; j++)
         {
           [[dayMatrix cellAtRow: i column: j] setTag: count++];
         }

   [dayMatrix setAutoresizingMask: NSViewMinYMargin];
   [dayMatrix setTarget: self];
   [dayMatrix setAction: @selector(updateDate:)];

   [self addSubview: dayMatrix];
   RELEASE(dayMatrix);

   /* Time */

   clockView = [[ClockView alloc] initWithFrame: NSMakeRect(180+5, rect.size.height-125, 130-5, 130-5)];
   [clockView setAutoresizingMask: NSViewMinYMargin | NSViewMaxXMargin];

   [self addSubview: clockView];
   RELEASE(clockView);

   return self;
}

- (void) updateDate: (id) sender
{
   int i=0, j=0, k=0;
   NSCalendarDate *newDate;

   if (sender == lastYearButton)
   {
      i = -1;
   }
   else if (sender == nextYearButton)
   {
      i = 1;
   }
   else if (sender == lastMonthButton)
   {
      j = -1;
   }
   else if (sender == nextMonthButton)
   {
      j = 1;
   }
   else if (sender == dayMatrix)
   {
      k = [[[[[sender selectedCells] lastObject] stringValue] stringByTrimmingTailSpaces] intValue] - [date dayOfMonth];
   }

   newDate = [date addYear: i
                     month: j
                       day: k
                      hour: 0
                    minute: 0
                    second: 0];
   [self setDate: newDate];
}

@end
