#import "ProgramController.h"
#import "Program.h"

@implementation ProgramController

- (id)init
{
    NSCalendarDate *aDate;
    Program *aProgram;

    self = [super init];

    programList = [[NSMutableArray alloc] init];

    aDate = [NSCalendarDate dateWithString:@"8/13/1996 23:45"
        calendarFormat:@"%m/%d/%Y %H:%M"];
    aProgram = [[Program alloc] initWithTitle:@"Faster, Pussycat, Kill Kill!"
        channel:@"Cinerip" broadcastDate:aDate cost:@"$2.50"];
    [programList addObject:aProgram];
    [aProgram release];

    aDate = [NSCalendarDate dateWithString:@"8/14/1996 21:00"
         calendarFormat:@"%m/%d/%Y %H:%M"];
    aProgram = [[Program alloc] initWithTitle:@"JFK"
         channel:@"The Conspiracy Channel" broadcastDate:aDate cost:@"$1.50"];
    [programList addObject:aProgram];
    [aProgram release];

    aDate = [NSCalendarDate dateWithString:@"8/15/1996 19:00"
         calendarFormat:@"%m/%d/%Y %H:%M"];
    aProgram = [[Program alloc] initWithTitle:@"It's a Wonderful Life"
         channel:@"Movie Classics" broadcastDate:aDate cost:@"$1.99"];
    [programList addObject:aProgram];
    [aProgram release];

    return self;
}


- (void)updateList:(id)sender
{
    [table reloadData];
    return;
}


- (void)tableClicked:(id)sender
{
    Program *selectedProgram;

    selectedProgram = [self selectedProgram];
    [channelField setStringValue:[selectedProgram channel]];
    [costField setStringValue:[selectedProgram cost]];

    return;
}


- (Program *)selectedProgram
{
    int row;
    Program *theProgram;

    row = [table selectedRow];
    theProgram = [programList objectAtIndex:row];
    return theProgram;
}


- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [programList count];
}


- (id)tableView:(NSTableView *)tableView
    objectValueForTableColumn:(NSTableColumn *)tableColumn
    row:(int)row
{
    Program *theProgram = [programList objectAtIndex:row];
    id colID = [tableColumn identifier];

    if (!theProgram) return nil;
    if ([colID isEqual:@"title"]) return [theProgram title];
    else if ([colID isEqual:@"broadcastDate"]) {
        return [theProgram broadcastDate];
    }
    else return nil;
}


@end
