/*
 Flash: Flashcard program
 Copyright (C) 2005, Niko Matsakis (niko@alum.mit.edu)
 
 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 "QuizGuess.h"
#import "Accessors.h"
#import "QuizMatching.h"

@interface QuizGuess (Private)
- (void) setPrecision:(NSNumber*)str;
@end

@implementation QuizGuess

+ (QuizGuess*) quizGuessWithGuess:(NSString*)guess
{
    return [[[self alloc] initWithGuess:guess] autorelease];
}

- initWithGuess:(NSString*)guess
{
    if ((self = [super init])) {
        [self setGuess:guess];
        [self setPrecision:[NSNumber numberWithInt:0]];
    }
    return self;
}

- (void) dealloc 
{
    [self setGuess:nil];
    [self setAnswerWord:nil];
    [self setPrecision:nil];
    [super dealloc];
}

// Invoked internally 
- (void) determinePrecision
{            
    if (![self guess] || ![self answerWord])
        return;
    
    NSString *guess = [self guess];
    NSString *answer = [[self answerWord] textValue];
    [self setPrecision:StarCount(answer, guess)];
}

- (NSString*) guess
{
    return a_guess;
}

- (void) setGuess:(NSString*)str
{
    [a_guess release];
    a_guess = [str retain];
    [self determinePrecision];
}
    
- (void) setAnswerWord:(NSManagedObject*)word 
{
    [a_answerWord release];
    a_answerWord = [word retain];
    [self determinePrecision];
}

- (NSManagedObject*) answerWord
{
    return a_answerWord;
}

- (NSNumber*) precision
{
    return a_precision;
}

- (void) setPrecision:(NSNumber*)str
{
    [a_precision release];
    a_precision = [str retain];
}

@end
