
#import "SoundTable.h"
#import "Imports.h"

#define MAX_NUMBER_OF_SOUNDS 256

@implementation SoundTable

- (int) NewItem:ThisSound: 
				ThisSoundView: 
				ThisWindow:
				(char*) ThisFileName:
				(BOOL) ItsUntitled
{
	if (CurrentNumber<MAX_NUMBER_OF_SOUNDS) {
		++CurrentNumber;
		SoundID[CurrentNumber]=ThisSound;
		SoundViewID[CurrentNumber]=ThisSoundView;
		WindowID[CurrentNumber]=ThisWindow;
		
		String[CurrentNumber]=[StringTable alloc];
		[String[CurrentNumber] init];
		[String[CurrentNumber] ChangeString:ThisFileName];
		
		SoundUntitled[CurrentNumber]=ItsUntitled;
		return CurrentNumber;
	}
	else return 0;
}

- init
{
    int x;
	[super init];
	for (x=1;x<=MAX_NUMBER_OF_SOUNDS;x++)
		{SoundID[x]=NULL;
		 SoundViewID[x]=NULL;
		 WindowID[x]=NULL;
		 SoundChanged[x]=NO;
		 String[x]=NULL;
		 SoundUntitled[x]=NO;
		}
	StartPosX=250;
	StartPosY=450;
	PosX=StartPosX;
	PosY=StartPosY;
	CurrentNumber=0;
	Current=CurrentNumber;
	WindowPosCount=0;
	return self;
}

- ReturnSound
{
    return SoundID[Current];
}

- SoundEdited
{
    SoundChanged [Current]=YES;
	return self;
}

- ReturnWindow
{
    return WindowID[Current];
}

- (BOOL) FindWindow: (id) ThisWindow
{
    int n;
	int theone=0;
	for (n=1;n<=CurrentNumber;n++) {
		if (WindowID[n]==ThisWindow) theone=n;
		}
	if (!(theone==0)) Current=theone;
	return (BOOL) theone;
}

- (BOOL) Next
{
	if ([self Total])
		{++Current;
		 if (Current>CurrentNumber) Current=1;
		return YES;}
	else return NO;
}

- SetSound: ThisSound
{
    SoundID[Current]=ThisSound;
	return self;
}

- NewWindowPos
{
	if (WindowPosCount<10)
		{
		PosX=StartPosX+WindowPosCount*22;
		PosY=StartPosY-WindowPosCount*24;
		}
	else
		{
		PosX=StartPosX-150+(WindowPosCount-10)*22;
		PosY=StartPosY-(WindowPosCount-10)*24;
		}
	WindowPosCount++;
	if (WindowPosCount>19) {WindowPosCount=0;}
	return self;
}

- (int) WindowPosX
{
    return PosX;
}

- (int) WindowPosY
{
    return PosY;
}

- Delete
{
    int n;
	if ([self Total]) 
		{
		[String[Current] free];
		for (n=Current+1;n<=CurrentNumber;n++)
			{
			 SoundID[n-1]=SoundID[n];
			 SoundViewID[n-1]=SoundViewID[n];
			 WindowID[n-1]=WindowID[n];
			 SoundChanged[n-1]=SoundChanged[n];
			 String[n-1]=String[n];
			 SoundUntitled[n-1]=SoundUntitled[n];
			}
		CurrentNumber--;
		[self Previous];
	}
	return self;
}

- (BOOL) Previous
{
	if ([self Total])
		{--Current;
		 if (Current==0) Current=CurrentNumber;
		return YES;}
	else return NO;
}

- (BOOL) FindSoundView:(id) ThisSoundView
{
    int n;
	int theone=0;
	for (n=1;n<=CurrentNumber;n++) {
		if (SoundViewID[n]==ThisSoundView) theone=n;
		}
	if (!(theone==0)) Current=theone;
	return (BOOL) theone;
}

- ReturnSoundView
{
    return SoundViewID[Current];
}

- SetWindow: ThisWindow
{
    WindowID[Current]=ThisWindow;
	return self;
}

- SetSoundView: ThisSoundView
{
    SoundViewID[Current]=ThisSoundView;
	return self;
}

- (BOOL) FindSound: ThisSound
{
    int n;
	int theone=0;
	for (n=1;n<=CurrentNumber;n++) 
		{
		if (SoundID[n]==ThisSound) theone=n;
		}
	if (!(theone==0)) Current=theone;
	return (BOOL) theone;
}

- (int) Total
{
	return CurrentNumber;
}

- SoundReset
{
	SoundChanged [Current]=NO;
	return self;
}

- (BOOL) FindChangedSound
{

	BOOL foundit=NO;
	int n;
	int theone=0;
	for (n=1;n<=CurrentNumber;n++) 
		{
		if ((SoundChanged[n]==YES)&&(!foundit))
			{
			theone=n;
			foundit=YES;
			}
		}
	if (!(theone==0)) 
	{
	Current=theone;
	theone=1;
	}
	return (BOOL) theone;
}

- (char*) ReturnFileName
{
	return [String[Current] ReturnString];
}

- SetFileName: (char*) ThisFileName
{
	[String[Current] ChangeString:ThisFileName];
	return self;
}

- (BOOL) IsSoundChanged
{
	return SoundChanged[Current];
}

- (BOOL) ReturnUntitled
{
	return SoundUntitled[Current];
}


@end
