// FILE:      BrailleConverter.m
// AUTHOR:    Daniel Lee
// PURPOSE:   
// REFERENCE: None. 
// COMMENTS:  Required that the Braille Font is installed, or the program will not function
// REQUIRES:  BrailleConverter.h 
// Last Mod:  01-05-2006




#import "BrailleConverter.h"

@implementation BrailleConverter


// NAME: awakefromNib
// PURPOSE: To initilise the view for use
// IMPORTS: nil
// EXPORTS: nil
// AUTHOR: Daniel Lee
// REMARKS: Should be called after the object is started
// LAST MODIFIED: 01-05-2006

- (void) awakeFromNib
{
	
	// *******************
	// Initialise the data
	// *******************
	
	
    NSString* data = [brailleGradeMenu stringValue];

	// Fetch the cell which is currently being selected
	NSButtonCell* cCell = [translationDirectionMatrix selectedCell];
	// Fetch the title of the string
	NSString* selectedRadio = [cCell title];


	// Build up the language list and to set it to the interface so that
	// language switching is possible
	// Also, should be set to the current language of the system

    // This should actually go and look at the interface to get the correct values for language.
	language = @"English";


    // Initalise the translateToBraille variable

	if( [selectedRadio isEqualToString: @"Forward"] )
	{
	    NSLog( @"Direction of translation init to forward." );
		translateToBraille = YES;
	}
	// If the string is 'backward' and it is currently not translating backward
	else if( [selectedRadio isEqualToString: @"Backward"] )
	{
	    NSLog( @"Direction of translation init to backward." );
		translateToBraille = NO;
	}
	else
	{
	    // How can there be nothing selected in the interface when the interface is
		// automatically set to a value?
	    translateToBraille = YES;
		NSLog( @"Error in initalising the translateToBraille" );
	}
	

    // Initialise the brailleGrade variable

	if( [data isEqualToString: @"0"] )
	{
		brailleGrade = 0;
	}
	else if( [data isEqualToString: @"1"] )
	{
        brailleGrade = 1;
	}
	else if( [data isEqualToString: @"2"] )
	{
		brailleGrade = 2;
	}
	else
	{
	    // How can there be nothing selected in the interface when the interface is
		// automatically set to a value?
	}

    // Set the boxes to set the delegate to this object
    [inputTextView setDelegate: self];
	[asciiTextView setDelegate: self];

	// asciiTextView does not needs a delegate since only braille is being sent there
	// There is no input that is allowed to be placed in that box

    // Set the font of the asciiTextView and brailleTextView to braille, size 16
	
    NSLog( @"Set up font" );
    NSFont* braille = [NSFont fontWithName: @"Braille" size: 16];

	[asciiTextView setFont: braille];
    [brailleTextView setFont: braille];


	//Call BrailleTranslator and create the table	

    return;
}


// NAME: setBrailleGrade
// PURPOSE: To get the new grade value and set it if it has changed.
// IMPORTS: nil
// EXPORTS: nil
// AUTHOR: Daniel Lee
// REMARKS: None
// LAST MODIFIED: 11-04-2006

- (IBAction) setBrailleGrade: (id) sender
{
    NSString* data = [brailleGradeMenu stringValue];
	NSString* speakingText;
	
    // Tests the string for a certain value and also if the grade it is going
	// into is also not the one which is already set
	
	if( [data isEqualToString: @"0"] && ( 0 != brailleGrade ) )
	{
	    speakingText = @"Grade 0 braille translation chosen.";
		brailleGrade = 0;
	}
	else if( [data isEqualToString: @"1"] && ( 1 != brailleGrade ) )
	{
	    speakingText = @"Grade 1 braille translation chosen.";
		brailleGrade = 1;
	}
	else if( [data isEqualToString: @"2"] && ( 2 != brailleGrade ) )
	{
        speakingText = @"Grade 2 braille translation chosen.";
		brailleGrade = 2;
	}
	else
	{
	    // If nothing matches, most likely scenario is that something was clicked
	    // but the braille grade was already chosen.
		
		speakingText = @"";
	    NSLog( @"Braille grade unchanged" );
	}

	// Logic to stop speaking if there is no change and to also make the tables
	// change is there has been a change to the settings

    if( ![speakingText isEqualToString: @""] )
	{
        [speechObject speak:speakingText];
		[translatorObject changeRules: translateToBraille language: language grade: brailleGrade];
    }
}


// NAME: setLanguage
// PURPOSE: To set the new tables for translating to and from the new language
// IMPORTS: nil
// EXPORTS: nil
// AUTHOR: Daniel Lee
// REMARKS: 
// LAST MODIFIED: 01-05-2006

- (IBAction) setLanguage: (id) sender;
{
    // Get data from langaugeMenu
    // Do some testing and then using BrailleGrade to do the switching.
}


// NAME: setTranslationDirection
// PURPOSE: To set the new translation direction if it hasn't changed
// IMPORTS: nil
// EXPORTS: nil
// AUTHOR: Daniel Lee
// REMARKS: The reason why literals were used here was because the words were going to be the constants.
//          And if the words change on the interface, it will be easy to change it here since the
//          program is still quite small. This may change later on, but for now is not required
// LAST MODIFIED: 01-05-2006 

- (IBAction) setTranslationDirection: (id) sender
{
    NSButtonCell* cCell;
	
	// Fetch the cell which is currently being selected
	cCell = [translationDirectionMatrix selectedCell];

	// Fetch the title of the string
	NSString* selectedRadio = [cCell title];
	NSString* speakingText;
	
	// If the string is 'forward' and it is currently not translating forward
	if( [selectedRadio isEqualToString: @"Forward"] && !translateToBraille )
	{
	    speakingText = @"Direction of translation is now forward.";
		translateToBraille = YES;
		
		// Set up so that the inputTextView is editable and brailleTextView is not editable
		[inputTextView setEditable: YES];
		[brailleTextView setEditable: NO];
	}
	// If the string is 'backward' and it is currently not translating backward
	else if( [selectedRadio isEqualToString: @"Backward"] && translateToBraille )
	{
	    speakingText = @"Direction of translation is now backward.";
		translateToBraille = NO;
		
		// Set up so that the inputTextView is not editable and brailleTextView not editable
		[inputTextView setEditable: NO];
		[brailleTextView setEditable: YES];
	}
	else
	{
	    // If nothing matches, most likely scenario is that something was clicked
	    // but the direction is the same at the new one.
		// Set speakingText to nothing so that the speechObject will not say anything

		speakingText = @"";
	    NSLog( @"Translation Direction unchanged" );
	}

	// Logic to stop speaking if there is no change and to also make the tables
	// change is there has been a change to the settings

    if( ![speakingText isEqualToString: @""] )
	{
        [speechObject speak:speakingText];
		[translatorObject changeRules: translateToBraille language: language grade: brailleGrade];
    }
}


// NAME: textDidChange
// PURPOSE: A delegate function to do the translation when the text is edited
// IMPORTS: NSNotification
// EXPORTS: nil
// AUTHOR: Daniel Lee
// REMARKS: 
// LAST MODIFIED: 11-04-2006

- (void) textDidChange: (NSNotification *) aNotification
{

    NSString *tempOutput, *tempInput;
	NSTextView *input, *output;
	BOOL tempTranslateToBraille = translateToBraille;
    NSRange range;
	NSTextStorage *text;
/*
    // Set the input and output variables to the respective ones which are to be used
	// This is done because the code is very similar on a lot of places and that the
	// program should be a s compact as possible.
	
    if( tempTranslateToBraille )
	{
        input = brailleTextView;
        output = inputTextView;
	}
	else
	{
        input = inputTextView;
        output = brailleTextView;
	}
	
	// NSText allows the range of change to be taken
	// NSTextStorage allows the user to get the text in as words
	// (in this case anything with white space)
	
	
    // Obtaining the range of the text. First the textStorage is fetched and then
	// the range is obtained.

    text = [input textStorage];
	range = [text editedRange];

	// Essentially both should grab the text that was changed from the input

	// use NSTextStorage to get the data
	// - (NSArray *) words

    tempInput;

    // Call object for translation
	tempOutput = [translatorObject translateLine: tempInput];
	
	// Find the same range from the output box as the changed box and overwrite
	// the text
	// ie. replaceCharactersInRange: range withString: (NSString *)aString

	// make sure text is pasted to the ascii view
	// Most likely better the replace the entire text from the inputTextView, set a braille
	// font on it and then paste it to the asciiTextView
*/
}


// NAME: 
// PURPOSE: 
// IMPORTS: 
// EXPORTS: 
// AUTHOR: 
// REMARKS: 
// LAST MODIFIED: 


@end
