#include <Cocoa/Cocoa.h>
#include "gamesystem.h"
#include "gametime.h"
#include "screen.h"
#include "config.h"

@interface Notifications : NSObject
{
}
@end

@implementation Notifications

- (void)observerMethod:(NSNotification*)aNotification
{
	if(aNotification)
	{
		if([[aNotification name] compare:@"com.apple.iTunes.playerInfo"]==NSOrderedSame)
		{
			int oldState=giTunesPlaying;
			NSDictionary *dic=[aNotification userInfo]; 
			NSString *str;
			str=[dic objectForKey:@"Name"];
			if(str)
				strcpy(giTunesSong,[str cString]);
			else
				strcpy(giTunesSong,"");
			StripDiacritics(giTunesSong,strlen(giTunesSong),smSystemScript);
			str=[dic objectForKey:@"Artist"];
			if(str)
				strcpy(giTunesArtist,[str cString]);
			else
				strcpy(giTunesArtist,"");
			StripDiacritics(giTunesArtist,strlen(giTunesArtist),smSystemScript);
			str=[dic objectForKey:@"Player State"];
			if(str)
			{
				if([str compare:@"Playing"]==NSOrderedSame)
					giTunesPlaying=kITunesPlaying;
				else if([str compare:@"Paused"]==NSOrderedSame)
					giTunesPlaying=kITunesPaused;
				else
					giTunesPlaying=kITunesStopped;				
			}
			if(oldState!=giTunesPlaying||giTunesPlaying)
				giTunesLastStatusUpdate=TimeGetSeconds();
		}
		if([[aNotification name] compare:@"NSWorkspaceWillSleepNotification"]==NSOrderedSame)
		{
			if(!ScreenNoWindow())
				if(gConfig->fullscreen)
				{
					gConfig->fullscreen=false;
					ScreenReInit();
				}
		}
	}
}
 
- (id)init
{
	if(self==[super init])
	{
		[[NSDistributedNotificationCenter defaultCenter] addObserver:self
															selector:@selector(observerMethod:)
																name:nil object:nil];
		[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
															selector:@selector(observerMethod:)
																name:nil object:nil];
	}
	return self;
}

@end

void InitITunesNotifications()
{
	NSAutoreleasePool *pool=[NSAutoreleasePool new];
	Notifications *notif;
	notif = [Notifications new];
}