//
//  TimerController.m
//  Timer
//
//  Created by Wilfredo Sánchez on 7/21/05.
//  Copyright 2005 The Apache Software Foundation. All rights reserved.
//

#import "TimerController.h"

@implementation TimerController

- (void) awakeFromNib
{
    [self updatePreferences: self];
    [goButton setTitle: @"Start"];
}

- (void) updateDisplay
{
    [minutesField setIntValue: (mySeconds / 60)];
    [secondsField setIntValue: mySeconds % 60];

    if (myTimer) {
        [myTimer release];
        myTimer = nil;
    }
}

- (void) update
{
    [self updateDisplay];

    if (mySeconds > 0) {
        mySeconds -= 1;

        myTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0
                                                   target: self
                                                 selector: @selector(update)
                                                 userInfo: nil
                                                  repeats: NO];
    } else {
        [goButton setTitle: @"Start"];
    }
}

- (IBAction) start: (id)aSender
{
    mySeconds = mySecondsStart;

    if (myTimer) {
        [goButton setTitle: @"Start"];
        [self updateDisplay];
    } else {
        [goButton setTitle: @"Reset"];
        [self update];
    }
}

- (IBAction) updatePreferences: (id)aSender
{
    mySecondsStart = [countdownFromField intValue];
}

@end
