/* 
   NSTimer.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <math.h>
#include <Foundation/NSTimer.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSInvocation.h>
#include <Foundation/NSRunLoop.h>

@implementation NSTimer
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
    invocation:(NSInvocation*)anInvocation
    repeats:(BOOL)_repeats
{
    id timer = [self timerWithTimeInterval:seconds
			invocation:anInvocation
			repeats:_repeats];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    return timer;
}

+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
    target:(id)anObject
    selector:(SEL)aSelector
    userInfo:(id)anArgument
    repeats:(BOOL)_repeats
{
    id timer = [self timerWithTimeInterval:seconds
			target:anObject
			selector:aSelector
			userInfo:anArgument
			repeats:_repeats];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    return timer;
}

+ (NSTimer*)timerWithTimeInterval:(NSTimeInterval)seconds
    invocation:(NSInvocation*)anInvocation
    repeats:(BOOL)_repeats
{
    id timer = [[[self alloc] initWith:seconds invocation:anInvocation
			userInfo:nil repeat:_repeats]
		    autorelease];
    return timer;
}

+ (NSTimer*)timerWithTimeInterval:(NSTimeInterval)seconds
    target:(id)anObject
    selector:(SEL)aSelector
    userInfo:(id)anArgument
    repeats:(BOOL)_repeats
{
    id anInvocation = [NSInvocation invocationWithMethodSignature:
			[anObject methodSignatureForSelector:aSelector]];
    id timer = [[[self alloc] initWith:seconds invocation:anInvocation
			userInfo:anArgument repeat:_repeats]
		    autorelease];
    [anInvocation setTarget:anObject];
    [anInvocation setSelector:aSelector];
    [anInvocation setArgument:&timer atIndex:2];

    return timer;
}

- (void)dealloc
{
    [fireDate release];
    [invocation release];
    [userInfo release];
    [super dealloc];
}

- (void)fire
{
    if(isValid)
	[invocation invoke];
}

- (NSDate*)fireDate
{
    if(!repeats)
	return [fireDate addTimeInterval:timeInterval];
    else {
	NSTimeInterval ellapsedTimeSinceCreated =
			    - [fireDate timeIntervalSinceNow];
	NSTimeInterval nextTime =
	    ceil(ellapsedTimeSinceCreated / timeInterval) * timeInterval;
	return [fireDate addTimeInterval:nextTime];
    }
}

- userInfo		{ return userInfo; }
- (void)invalidate	{ isValid = NO; }
- (BOOL)isValid		{ return isValid; }
- (BOOL)repeats		{ return repeats; }

@end

/*
 * NSTimer implementation messages
 */
 
@implementation NSTimer(NSTimerImplementation)
- initWith:(NSTimeInterval)seconds invocation:(NSInvocation*)anInvocation
    userInfo:(id)anObject repeat:(BOOL)_repeats
{
    timeInterval = seconds;
    fireDate = [[NSDate date] retain];
    invocation = [anInvocation retain];
    userInfo = [anObject retain];
    repeats = _repeats;
    isValid = YES;
    return self;
}

@end
