/* OFInfoManager.m
 *
 * <Short class description goes here>
 *
 * For interface info see the header file. The comments in this file
 * cover only the real implementation details.
 *
 *
 * Written by:	   Thomas Engel
 * Created on:	   Sun 16-Feb-1997 (Copyleft)
 */


#import <OFInfoManager.h>

@implementation OFInfoManager

- (id)init
{
	self = [super init];
	if( !self ) return self;

	// OK. We really are an object...here we go with our init.

    _currentIconState = OFInfoShowingAppIcon;
	animationRunning = NO;
	
	return self;
}

- (void)makeKeyAndOrderFront:(id)sender
{
	if( !panel )
	{
		if( ![NSBundle loadNibNamed:@"Info" owner:self] )
			NSRunAlertPanel( NULL, @"Couldn't load Info.nib",
							 @"OK", NULL, NULL );
	}
	
    // Now if we have a panel we will show it an shedule the first timed switch of the note images

    if( panel )
    {
        // This is hardcoded to a color default which might not be present in all our apps...

        [desktopView setBackgroundColor:[[NSUserDefaults standardUserDefaults] colorForKey:@"DesktopColor"]];

        [panel makeKeyAndOrderFront:self];
        [self _sheduleNextTimedSwitch];
    }
}

- (void)switchToNextIcon:sender
{
	switch( _currentIconState )
	{
        case OFInfoShowingAppIcon:
            return [self switchToCompanyIcon:self];
        case OFInfoShowingCompanyIcon:
            return [self switchToDeveloperIcon:self];
		case OFInfoShowingDeveloperIcon:
            return [self switchToDesignerIcon:self];
		case OFInfoShowingDesignerIcon:
			return [self switchToAppIcon:self];
	}
}

- (void)switchToAppIcon:sender
{
    [self _switchTo:OFInfoShowingAppIcon using:appButton];
}

- (void)switchToCompanyIcon:sender
{
    [self _switchTo:OFInfoShowingCompanyIcon using:companyButton];
}

- (void)switchToDeveloperIcon:sender
{
    [self _switchTo:OFInfoShowingDeveloperIcon using:developerButton];
}

- (void)switchToDesignerIcon:sender
{
    [self _switchTo:OFInfoShowingDesignerIcon using:designerButton];
}

- (void)_switchTo:(int)aStyle using:aButton;
{
    if( animationRunning ) [currentAnimator stopAnimation];

    if( !animationRunning &&
        _currentIconState != aStyle )
	{
        // Ok we really have to switch so something new...
        // Now invalidate a pending timer and remove the current note image

        if( noteSwitchTimer )
        {
            [noteSwitchTimer invalidate];
            [noteSwitchTimer autorelease];
            noteSwitchTimer = nil;
        }
        [noteImageView setImage:nil];

        // Now shedule the new timer to put the new note into the view after a small delay of 1 second.

        noteSwitchTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)1.0
                                                           target:self
                                                         selector:@selector(_placeNewNoteImage:)
                                                         userInfo:[aButton image]
                                                          repeats:NO];
        [noteSwitchTimer retain];

        _currentIconState = aStyle;
	}
}

- (void)_placeNewNoteImage:(NSTimer *)ourTimer
/*"
    No matter if the switch was forced by a timer event or a direct user action. We always have to make the final switch 	from a timer event.
   The timer's userInfo has to contain the new image which ahs to be placed intot he info panels area.
   Once we have made the switch we will shedual another Timer event for the next periodic image change.
"*/
{
    [noteImageView setImage:[ourTimer userInfo]];

    // No matter how this switch was caused....we'll shedule a new timer to switch to the next image

    [self _sheduleNextTimedSwitch];
}

- (void)_sheduleNextTimedSwitch
{
    if( noteSwitchTimer )
    {
        [noteSwitchTimer invalidate];
        [noteSwitchTimer autorelease];
    }
    noteSwitchTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimeInterval)7.0
                                                       target:self
                                                     selector:@selector(switchToNextIcon:)
                                                     userInfo:nil
                                                      repeats:NO];
    [noteSwitchTimer retain];
}

- (void)startRandomAnimation:sender
{
	// if there is no animation running right now...
	
	if( animationRunning ) return;
	
	// register that a animation will run and choose a random animator-obj
	// To be able to switch to a original Image we have to tell this object
	// that if is now showing the AnimationImages
	
	[self switchToAppIcon:self];
	animationRunning = YES;
    _currentIconState = OFInfoShowingAnimatedIcon;

    // <<BUG>> This is wrong and we currently have not included any kind of
    // useful animator support.

	currentAnimator = nil;
		
	[currentAnimator setDelegate:self];
	[currentAnimator startAnimation:self];
}

- (void)animationDidStop:sender
{
	// After the animation we have to switch to the original AppIcon
	// we are also registered as the animators delegate.

	animationRunning = NO;
	[self switchToAppIcon:self];
}

- (void)windowDidClose:(NSNotification *)notification;
{
    // We're the InfoPanels delegate. So we can stop an animation even if it
    // has not finished yet.

    if( animationRunning ) [currentAnimator stopAnimation];
}

- (void)windowDidMiniaturize:(NSNotification *)notification;
{
    // We're the InfoPanels delegate. So we can stop an animation even if it
    // has not finished yet.

    if( animationRunning ) [currentAnimator stopAnimation];
}

@end


/* History:
 *
 * Sun 16-Feb-1997	Created this class.
 */

/* Bugs:
 *
 * 00000	No bugs known.
 */