//
// EditEvent.m
// Copyright (c) 1989, 1990 by Jiro Nakamura 
// All rights reserved
//
// This is the control object that handles the window that allows the user to
// edit Cassandra's database
//
// RCS Information
// Revision Number->	$Revision: 2.3 $
// Last Revised->	$Date: 91/11/01 17:23:11 $
//
static char rcsid[] = "$Id: EditEvent.m,v 2.3 91/11/01 17:23:11 jiro Exp Locker: jiro $";

#import <appkit/Form.h>
#import <appkit/Button.h>
#import <appkit/Matrix.h>
#import <appkit/Panel.h>       	/* for NXRunAlertPanel() */
#import <appkit/publicWraps.h>  /* for NXBeep( ) 	*/

#import "calendar.h"
#import "Event.h"
#import "Global.h"
#import "EditEvent.h"
#import "Cassandra.h"

@implementation EditEvent

/* The main reason for the following method is just to avoid warnings
 *     about rcsid being unreferenced...   Garance/Jul 28/94
 */
- (const char *) returnRcsid
{
     return rcsid;
}

- open: sender
{
	ev = [Event newAt: [global eventFile]];
	[ev firstEvent];
	[hereForm setIntValue: [ev present] at:0];	
	
	[self readEvent:self];
	return self;
}

- readEvent:sender
{
	EFileLink here, previous, next;
	
	here = [hereForm intValueAt:0];
	if( here < 0)
		{
		NXRunAlertPanel("File Read Error", 
			"Sorry, you can't read from negative event numbers.",
			"Ahh..", NULL,NULL);
		return self;
		}

	[ev readEvent : here];
	next = [ev next];
	previous = [ev previous];

	if( ![global guru])
		{
		if( here == 0)
			NXRunAlertPanel("Warning",
				"You are reading event 0. "
				"It is where important event linking and "
				"global information is kept. Be very careful "
				"when modifying any of the variables in it.",
				"OK",NULL,NULL);
		
		if( next == -1 || previous == -1)
			NXRunAlertPanel("Warning",
				"This is a deleted event. The values in it "
				"are now meaningless.","OK",NULL,NULL);
		}
		
	vHere = here;
	vNow = *[ev time];
	
	[nextForm	setIntValue	: next];
	[previousForm	setIntValue	: previous];
	if( [global guru])
		{
		[nextForm	setEnabled:	TRUE];
		[previousForm	setEnabled:	TRUE];
		}
	else
		{
		[nextForm	setEnabled:	FALSE];
		[previousForm	setEnabled:	FALSE];
		}
		
	[self displayValues: self];
	
	[hereForm selectTextAt:0];	
    return self;
}


- writeEvent:sender
{
	int here, previous, next, vAnniv;
	
	here = [hereForm intValueAt:0];
	next = [nextForm intValueAt:0];
	previous = [previousForm intValueAt:0];
	
	if( here == 0)
		{
		if(NXRunAlertPanel("Alert!", 
			"Event 0 is an important header location. "
			"Are you SURE you want to overwrite it?",
			"Overwrite","Cancel", NULL ) == 0)
			return self;
		}
		
	if( here < 0)
		{
		NXRunAlertPanel("File Write Error", "Sorry, you can't "
			"write to negative event numbers.", 
			"Ahh..", NULL,NULL);
		return self;
		}
	
	if( vHere != here && NXRunAlertPanel("Event Mismatch in Writing",
				"The event you read from is not the same "
				"as the one you are writing to. "
				"Are you SURE you want to do this?",
				"Cancel","Write",NULL) == 1)
		return self;
			
	[ev setMessage 	: (char *) [messageForm stringValueAt:0]];
	[ev setShowMessage: 	[messageButton		state]];
	switch( [alarmSoundRadioMatrix selectedRow] )
		{
		case 0:	// sound
			[ev setAlarmAction: 1];
			break;
		case 1: // runCommand
			[ev setAlarmAction: 3];
			break;
		case 2: // nada
			[ev setAlarmAction: 0];
		break;
		}
	[ev setAlarmSound : (char *) [alarmSoundForm stringValueAt:0]];

	[ev setTime:	[self	getDate: self]];
		
	if( [anniversaryForm intValueAt:0] > 99)
		{
		NXRunAlertPanel("Anniversary Overload",
			"Due to a design error that cannot be fixed until "
			"Version 2.0, you cannot have a reoccuring event "
			"of more than 99 intervals.","OK",NULL,NULL);
		[anniversaryForm selectTextAt: 0];
		return nil;
		}
		
	vAnniv = ([anniversaryRadioMatrix selectedCol] +1 ) * 100;	

	[ev setAnniversary:	([anniversaryButton state]
				? ([anniversaryForm intValueAt:0] + vAnniv)
				: 0) ];

	[ev setDestroy:		0];	// Destroy not supported after v0.9.2

	[ev setSnoozeNo:		([snoozeButton state]
				? [snoozeNoForm intValueAt:0]
				: 0)];
	[ev setSnoozeInt: 	([snoozeButton state]	
				? [snoozeIntForm intValueAt:0]
				: 0)];
	[ev setPriority: 	([priorityRadioMatrix selectedCol]*50)];
	[ev setNext : next];
	[ev setPrevious : previous];


	if( ! [global guru] && (timeCompare(&vNow, [ev time]) != 0))
		{
		if(NXRunAlertPanel( "Warning",
				"You have changed the date or time, use "
				"re-insert to put the event back in the "
				"queue. Insert will duplicate the "
				"event.","Insert","Cancel",NULL) == 0)
			return self;
		}
		
	[ev writeEvent : here];

	[hereForm selectTextAt:0];
	if( ![global guru])	/* If we're not a guru, then update */
		[cassandra queueDidChange:self];

	// The window has been saved
	[self setDocEdited:	FALSE];

    return self;
}


- murderEvent : sender
{
	int here, next;
		
	here = [hereForm intValueAt:0];
	[ev readEvent: here];
	next = [ev next];
	
	if( [ev present] <= 0)
		{
		NXRunAlertPanel("Delete Error",
			"Cannot reset/cancel events 0 or below.", 
			"Oh well...",NULL,NULL);
		return self;
		}
		
	if( NXRunAlertPanel("Warning",
			"This will reset this event. "
			"Are you sure you want to do this?",
			"Reset","Cancel",NULL) == 0)
		return self;
		
	here = [ev murderEvent : here];
	
	// If the event was truly deleted when canceled, then we will 
	// read the next event, otherwise we will read the event 
	// EFileLink returned by murderEvent
	if( here != -1)
		next = here;
	
	[hereForm setIntValue : next at: 0];
	[self readEvent:self];

	[cassandra queueDidChange:self];

   	 return self;
}

- deleteEvent:sender
{
	int here, next;
		
	here = [hereForm intValueAt:0];
	[ev readEvent: here];
	next = [ev next];
	
	if( [ev present] <= 0)
		{
		NXRunAlertPanel("Delete Error",
				"Cannot delete events 0 or below.",
				"Oh well...",NULL,NULL);
		return self;
		}
		
	if( NXRunAlertPanel("Warning", 
			"This will permanently delete this event. "
			"Are you sure you want to do this?", 
			"Delete","Cancel",NULL) == 0)
		return self;
		
	[ev deleteEvent : here];
	
	[hereForm setIntValue : next at: 0];
	[self readEvent:self];

	[cassandra queueDidChange:self];
   	 return self;
}


- insertEvent:sender
{
	if( ![global guru])
		{
		if( NXRunAlertPanel("Warning", 
				"You really should use the Add Event panel. "
				"Using insert on an already existing "
				"record will duplicate it.",
				"Insert","Cancel",NULL) == 0)
			return self;
		}

	[super insertEvent : self];
	if( ![global guru])
		[cassandra queueDidChange:self];
	return self;
}


- reinsertEvent:sender
{
	int here, vAnniv;
	
	here = [hereForm intValueAt:0];
	if( vHere != here)
		{
		if(NXRunAlertPanel("Event Mismatch on Re-insert",
				"The event you read from is not the same "
				"as the one you are reinserting into. "
				"This could mess things up PRETTY BADLY. "
				"Are you SURE you want to do this?",
				"Re-insert","Cancel",NULL) == 0)
			return self;
		}

	if(here <= 0)
		{
		NXRunAlertPanel("Reinsert Error",
			"Cannot reinsert events 0 or below.", 
			"Oh well...",NULL,NULL);
		return self;
		}
		
	if( [self dateFinished: self] == NULL)
		return self;

	[ev readEvent: here];
	[ev deleteEvent : here];
		
	[ev setMessage 	: (char *) [messageForm stringValueAt:0]];
	[ev setShowMessage:	[messageButton		state]];
	switch( [alarmSoundRadioMatrix selectedRow] )
		{
		case 0:	// sound
			[ev setAlarmAction: 1];
			break;
		case 1: // runCommand
			[ev setAlarmAction: 3];
			break;
		case 2: // nada
			[ev setAlarmAction: 0];
		break;
		}
	[ev setAlarmSound : (char *) [alarmSoundForm stringValueAt:0]];
	
	[ev setTime:	[self	getDate: self]];	

	if( [anniversaryForm intValueAt:0] > 99)
		{
		NXRunAlertPanel("Anniversary Overload",
			"Due to a design error that cannot be fixed until "
			"Version 1.1, you cannot have a reoccuring event of "
			"more than 99 intervals.","OK",NULL,NULL);
		[anniversaryForm selectTextAt: 0];
		return nil;
		}
		
	vAnniv = ([anniversaryRadioMatrix selectedCol] +1 ) * 100;	

	[ev setAnniversary: 	([anniversaryButton state]
				? ([anniversaryForm intValueAt:0]  + vAnniv)
				: 0) ];

	[ev setDestroy:		0];	// Destroy not supported after v0.9.2

	[ev setSnoozeNo: 	([snoozeButton state]
				? [snoozeNoForm intValueAt:0]
				: 0)];
	[ev setSnoozeInt: 	([snoozeButton state]	
				? [snoozeIntForm intValueAt:0]
				: 0)];
	[ev setPriority: 	([priorityRadioMatrix selectedCol]*50)];
	
	[ev insertEvent];

	[hereForm setIntValue :[ev present] at: 0];
	[self readEvent:self];

	if( ![global guru])
		[cassandra queueDidChange:self];
	return self;
}

- nextLink : sender
{
	int here;

	here = [nextForm intValueAt:0];
	if( here == 0 && [global guru] == NO)
		{
		NXBeep();
		return self;
		}
	[hereForm setIntValue: here at:0];
	[self readEvent:self];
	return self;
}

- previousLink : sender
{
	int here;
	
	here = [previousForm intValueAt:0];
	if( here == 0 && [global guru] == NO)
		{
		NXBeep();
		return self;
		}
		
	[hereForm setIntValue: here at:0];
	[self readEvent:self];
	return self;
}

- nextEvent : sender
{
	int here;
	
	here = [hereForm intValueAt:0]+1;
	[hereForm setIntValue: here at:0];
	[self readEvent:self];
	return self;
}

- previousEvent : sender
{
	int here;
	
	here = [hereForm intValueAt:0] -1 ;
	[hereForm setIntValue: here at:0];
	[self readEvent:self];
	return self;
}
@end
