#import "../ADController.h"
#import <appkit/Button.h>
#import "ADSlave.h"
#import "../TVController.h"
#import "../PrefControl.h"

#define  Stat_INIT	0
#define  Stat_PLAY	1
#define  Stat_PAUSE	2
#define  Stat_QUIT	3

static id activePanel = nil;


@implementation ADController

#define  Panel_X(n)	(6.0 + ((n) % 4) * 16.0 + ((n) / 8) * 9.0)
#define  Panel_Y(n)	(400.0 - ((n) % 8) * 20.0)

/* Local Method */
- locatePanel
{
	static int panelNum = 0;

	[panel moveTopLeftTo: Panel_X(panelNum): Panel_Y(panelNum)];
	++panelNum;
	return self;
}

/* Local Method */
- jumpTo:(int) pos
{
	[slider setIntValue: pos];
	currentNum = pos;
	[nextFile setStringValue: [dirlist filenameAt: pos]];
	return self;
}


- init:sender dir:(const char *)dir with:(DirList *)list
{
	int i, m;

	[NXApp loadNibSection:"AutoDisplay.nib" owner:self];
	dirlist = list;
	status = Stat_INIT;
	fileNum = [dirlist fileNumber];
	[slider setMaxValue: (fileNum - 1)];
	[sliderMax setIntValue: fileNum];
	[[self locatePanel] jumpTo: 0];
	for (i = m = 0; dir[i]; i++)
		if (dir[i] == '/') m = i + 1;
	[[[panel setTitle: &dir[m]] display] makeKeyAndOrderFront: self];
	[[panel setFloatingPanel:YES] setWorksWhenModal:YES];
	[panel setFreeWhenClosed:YES];
	[[lButton setContinuous:YES] setPeriodicDelay:0.5 andInterval:0.2];
	[[rButton setContinuous:YES] setPeriodicDelay:0.5 andInterval:0.2];
	NXPing();
	slave = [[ADSlave alloc] init:self with:sender dir:dir];
	waiting = NO;
	return self;
}

- free
{
	[dirlist free];
	[slave free];
	[super free];
	return nil;
}


/* Local Method */
- setButton:(int)stat
{
	if (activePanel == self && stat != Stat_PLAY)
		activePanel = nil;	/* UNLOCKED */
	[playButton setState:(stat == Stat_PLAY)];
	[pauseButton setState:(stat == Stat_PAUSE)];
	[quitButton setState:(stat == Stat_INIT)];
	[stepButton setState:NO];
	[slider setEnabled:(stat != Stat_PLAY)];
	return self;
}

- pausePush:sender
{
	[self setButton: (status = Stat_PAUSE)];
	return self;
}

- playPush:sender
{
	if (activePanel != nil && activePanel != self) {
		[self setButton: status];
		NXBeep();
		return self;
	}
	activePanel = self;	/* LOCKED */
	[self setButton: Stat_PLAY];
	if (status != Stat_PLAY) {
		status = Stat_PLAY;
		[slave perform:@selector(donext:) with:self
				afterDelay: 300 cancelPrevious:YES];
		waiting = YES;
	}
	return self;
}

- stepPush:sender
{
	if (activePanel != nil && activePanel != self) {
		[self setButton: status];
		NXBeep();
		return self;
	}
	activePanel = self;	/* LOCKED */
	[playButton setState: NO];
	[pauseButton setState: NO];
	[quitButton setState: NO];
	status = Stat_PLAY;
	[slave dostep:self];
	[self setButton: (status = Stat_PAUSE)];	/* UNLOCKED */
	[self sliderUp: self];
	return self;
}

- quitPush:sender
{
	[self setButton: Stat_INIT];
	[panel close];
	NXPing();
	if (waiting) {
		status = Stat_QUIT;
		return self;
	}
	return [self free];
}


- sliderChange:sender
{
	int n = [slider intValue];
	if (currentNum != n)
		[self jumpTo: n];
	return self;
}

- sliderDown:sender
{
	if (status != Stat_PLAY && currentNum > 0)
		[self jumpTo: currentNum - 1];
	return self;
}

- sliderUp:sender
{
	if (status != Stat_PLAY && currentNum < fileNum-1)
		[self jumpTo: currentNum + 1];
	return self;
}


- (const char *)nextFilename
{
	return (status == Stat_PLAY) ? [dirlist filenameAt: currentNum] : NULL;
}

- continueLoop:(BOOL)cont
{
	int intv;

	if (status == Stat_QUIT) return [self free];
	if (!cont) {
		waiting = NO;
		return self;
	}
	if (currentNum >= fileNum-1) {
		[self pausePush: self];
		waiting = NO;
		return self;
	}
	[self jumpTo: currentNum + 1];
	intv = (thePreference) ? [thePreference autoDisplayInterval] : 2500;
	[slave perform:@selector(donext:) with:self
			afterDelay: intv cancelPrevious:YES];
	waiting = YES;
	return self;
}

- (BOOL) scanFixPosition
{
	return ([thePreference windowPosition] == pos_FixScan);
}

@end
