#import "GanttInspectorController.h"

@implementation GanttInspectorController

- (void)ganttViewSelectionDidChange:(NSNotification *)note
{
    GanttView *view = [note object];
    NSIndexSet *selection = [view selectedRowIndexes];
    NSInteger count = [selection count];

    UKLog(@"Count: %d", count);

    if (count == 0)
    {
        [self setTask:nil];
        [taskTextField setStringValue:@"No tasks selected"];
        [taskEndTextField setStringValue:nil];
        [taskStartTextField setStringValue:nil];
    } else if (count == 1) {
        [self setTask:[view taskAtIndex:[selection firstIndex]]];
        [taskTextField setStringValue:[task name]];
        [taskStartTextField setStringValue:
            [[task startDate] descriptionWithCalendarFormat:@"%H:%M %e %b %Y"
                timeZone:[NSTimeZone systemTimeZone] locale:nil]];
        [taskEndTextField setStringValue:
            [[task endDate] descriptionWithCalendarFormat:@"%H:%M %e %b %Y"
                timeZone:[NSTimeZone systemTimeZone] locale:nil]];
    } else {
        [self setTask:nil];
        [taskTextField setStringValue:
            [NSString stringWithFormat:@"%d tasks selected", count]];
        [taskEndTextField setStringValue:nil];
        [taskStartTextField setStringValue:nil];
    }
        

}

- (void)awakeFromGSMarkup
{
    UKLog(@"Hello ma honey, hello ma baby");
    [inspector orderFront:self];
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
        selector:@selector(ganttViewSelectionDidChange:)
        name:@"GanttViewSelectionDidChange"
        object:nil];
}

- (void)setTask:(GanttTask *)aTask
{
    ASSIGN(task, aTask);
}

- (void)setInspector:(id)obj
{
    ASSIGN(inspector, obj);
}

- (void)setTaskTextField:(id)obj
{
    ASSIGN(taskTextField, obj);
}

- (void)setTaskStartTextField:(id)obj
{
    ASSIGN(taskStartTextField, obj);
}

- (void)setTaskEndTextField:(id)obj
{
    ASSIGN(taskEndTextField, obj);
}

- (void)dealloc
{
    DESTROY(task);
    DESTROY(taskTextField);
    DESTROY(inspector);
    [super dealloc];
}

@end
