/*
 * Scrabbalize
 * Copyright (c) 2007-2008, Blue Static <http://www.bluestatic.org>
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU 
 * General Public License as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#import "AppController.h"
#import "Word.h"

@implementation AppController

/**
 * Initializes the application and loads the dictionary archive
 */
- (id)init
{
	if (self = [super init])
	{
		NSString *path = [NSString stringWithFormat:@"%@/dictionary.txt", [[NSBundle mainBundle] resourcePath]];
		NSString *file = [NSString stringWithContentsOfFile:path];
		NSArray *strings = [file componentsSeparatedByString:@"\n"];
		
		dictionary = [[NSMutableArray alloc] init];
		for (int i = 0; i < [strings count]; i++)
		{
			NSString *str = [strings objectAtIndex:i];
			Word *word = [[Word alloc] initWithWord:str];
			[dictionary addObject:word];
			[word release];
		}
	}
	return self;
}

/**
 * Destructor
 */
- (void)dealloc
{
	[dictionary release];
	[super dealloc];
}

/**
 * Action that filters through all the words with the given tiles and then produces
 * the list
 */
- (IBAction)findWords:(id)sender
{
	[wordlist removeObjects:[wordlist arrangedObjects]];
	
	Word *temp = [[Word alloc] initWithWord:[tilesField stringValue]];
	NSString *tiles = [temp alphagram];
	[temp release];
	
	NSRange wildRange = [tiles rangeOfString:@"?" options:NSBackwardsSearch];
	int numWild = (wildRange.location == NSNotFound ? 0 : wildRange.length + wildRange.location);
	int length = [tiles length];
	
	for (int i = 0; i < [dictionary count]; i++)
	{
		Word *word = [dictionary objectAtIndex:i];
		int wildCounter = numWild;
		
		if ([word length] > length)
		{
			continue;
		}
		
		BOOL add = YES;
		int j = 0, k = numWild;
		while (k < length && j < [word length])
		{
			unichar charW = [[word alphagram] characterAtIndex:j];
			unichar charT = [tiles characterAtIndex:k];
			if (charW == charT)
			{
				j++;
				k++;
			}
			else if (charW < charT)
			{
				wildCounter--;
				if (wildCounter < 0)
				{
					add = NO;
					break;
				}
				else
				{
					j++;
				}
			}
			else
			{
				k++;
			}
		}
		
		if (j < [word length] && j + wildCounter < [word length])
		{
			add = NO;
		}
		
		if (add)
		{
			[wordlist addObject:word];
		}
		add = YES;
	}
	
	// resort
	[wordlistView setSortDescriptors:nil];
	[wordlistView setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO]]];
	[wordlistView deselectAll:nil]; 
}

@end
