#import "Controller.h"
#import "SmsObject.h"
#import "DetController.h"
#import "StocController.h"
#import "AsciiController.h"
#import <appkit/Application.h>
#import <sys/param.h> 		/* for MAXPATHLEN */
#import <appkit/OpenPanel.h>
#import <appkit/Listener.h>
#import <appkit/Panel.h>
#import <appkit/Button.h>
#import <appkit/ButtonCell.h>
#import <appkit/Cell.h>
#import <appkit/ActionCell.h>
#import <appkit/Matrix.h>
#import <appkit/View.h>
#import <appkit/Form.h>
#import	<objc/List.h>
#import <string.h>
#import <objc/NXStringTable.h>


static BOOL getOpenPath(char *buf, char const *theType)
{
	static id	openPanel = nil;
	char const *fileTypes[2] = {0,0};

	if (!openPanel)
		openPanel = [OpenPanel new];
	if (theType && *theType)
		fileTypes[0] = theType;
	[NXApp setAutoupdate:NO];
	if ([openPanel runModalForTypes:fileTypes]) 
	{
		strcpy(buf,[openPanel filename]);
		[NXApp setAutoupdate:YES];
		return YES;
	} 
	else 
	{
		[NXApp setAutoupdate:YES];
		return NO;
	}
}

@implementation Controller

- infoPanel:sender
{
  if(!infoPanel) 
  {
    [NXApp loadNibSection:"Info.nib" owner:self];
  }
  [infoPanel makeKeyAndOrderFront:sender];
  return self;
}

- helpPanel:sender
{
  if(!helpPanel) 
  {
    [NXApp loadNibSection:"Info.nib" owner:self];
  }
  [helpPanel makeKeyAndOrderFront:sender];
  return self;
}

- headerPanel:sender
{
  if(!headerPanel) 
  {
    [NXApp loadNibSection:"Header.nib" owner:self];
  }
	[analysisParForm setStringValue:[theSmsObject getTextCharacters]];

  [headerPanel makeKeyAndOrderFront:sender];
  return self;
}

- appDidInit:sender
{
	[super init];
	theSmsObject=NULL;
	myListener=[NXApp appListener];
	[myListener setServicesDelegate: self];
	[theWindow makeKeyAndOrderFront: self];
	return self;
}
    
- saveFile:sender
{
    return self;
}

- revertToSaved:sender
{
    return self;
}

- displayDeterministic:sender
{
	if(!detController) 
		detController = [[DetController alloc] init:self];
	else   [detController init:self];
	return self;
}

- displayStochastic:sender
{
	if(!stocController) 
		stocController = [[StocController alloc] init:self];
	else
		[stocController init:self];

	return self;
}

- displayAscii:sender
{
	if(!asciiController) 
		asciiController = [[AsciiController alloc] init:self];
	else
		[asciiController init:self];

	return self;
}

- getTheSmsObject
{
	return theSmsObject;
}
    
- openFile:(const char *)fileName
{
	if (theSmsObject)
		[theSmsObject free];

	theSmsObject = [[SmsObject alloc] initFromFile:(char *)fileName];
	return self;
}

- open:sender
{
  char pChPathname[1024];
      
  if (getOpenPath(pChPathname,"sms"))
    [self openFile:pChPathname];
  else
    return self;
		
	[self getInfo:pChPathname];
	return self;
}

- getInfo:(const char *)pChPathname
{	
  char pChString[1024];
  int iFormat, iRecords, iStochType;
  float fLength;

  sprintf (pChString, "Pathname: %s", pChPathname);  
  [pathnameForm setStringValue:pChString];
  iFormat = [theSmsObject getFormat];
  switch (iFormat)
  {
    case FORMAT_HARMONIC: 
      [formatForm setStringValue:"Format: harmonic"];
      break;
    case FORMAT_INHARMONIC: 
      [formatForm setStringValue:"inharmonic format"];
      break;
    case FORMAT_HARMONIC_WITH_PHASE:
      [formatForm setStringValue:"harmonic format with phase"];
      break;
    case FORMAT_INHARMONIC_WITH_PHASE:
       [formatForm setStringValue:"inharmonic format with phase"];
       break;
    default: 
      [formatForm setStringValue:""];
      break;
  }
  
  iRecords = [theSmsObject getNRecords];
  fLength = (float) iRecords / [theSmsObject getFrameRate];
  sprintf (pChString, "Length: %.3f seconds (%d records)", fLength, iRecords);  
  [lengthForm setStringValue:pChString];

  sprintf (pChString, "Frame rate: %d Hz", [theSmsObject getFrameRate]);
  [frameRateForm setStringValue:pChString];
  
  sprintf (pChString, "Number of sinuoids: %d", 
    [theSmsObject getNTrajectories]);
  [numberTrajForm setStringValue:pChString];
 
  sprintf (pChString, "Number of stochastic coefficients: %d", 
    [theSmsObject getNStochasticCoeff]);
  [numberCoeffForm setStringValue:pChString];

  iStochType = [theSmsObject getStochasticType];
  switch (iStochType)
  {
    case STOC_FILTER_TYPE: 
      [stochTypeForm setStringValue:"Stochastic type: IIR Filter"];
      break;
    case STOC_MAG_ENV_TYPE: 
      [stochTypeForm setStringValue:"Stochastic type: line segments"];
      break;
   case STOC_NONE: 
      [stochTypeForm setStringValue:"Stochastic type: none"];
      break;
	}

  return self;
}

- (int)appOpenFile:(const char *)path type:(const char *)type
{
	if (type && !strcmp(type, "sms")) 
	{
		[self openFile:path];
		[self getInfo:path];
		return 1;
	}
	else
		return 0;
}


@end
