//
//  LanguageDefn.m
//  Flash2
//
//  Created by Nicholas Matsakis on 10/17/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Language.h"
#import "OxNSArray.h"
#import "GreekLanguage.h"
#import "Ox.h"

@implementation Language

+ (NSArray*) languages {
	return OxArr([GreekLanguage new]);
}

- initWithName:(NSString*)name
	 relations:(NSArray*)rels
  grammarRules:(NSArray*)rules
quizConfigurationKeys:(NSArray*)keys
{
	if ((self = [super init])) {
		m_name = [name copy];
		m_relations = [rels copy];
		m_grammarRules = [rules copy];
		m_quizConfigurationKeys = [keys copy];
	}
	return self;
}

- initWithName:(NSString*)name
fromPlistNamed:(NSString*)plistName
	  inBundle:(NSBundle*)bundle
{
	NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:plistName
																					  ofType:@"plist"]];
	if (plist == nil)
		return nil;
	
	NSMutableArray *relations = [NSMutableArray array];
	for (NSDictionary *relationData in [plist objectForKey:@"relations"]) {
		Relation *r = [[Relation alloc] initWithName:[relationData valueForKey:@"name"]
									   crossLanguage:[[relationData valueForKey:@"crossLanguage"] boolValue]];
		[relations addObject:r];
	}

	NSArray *quizConfigurationKeys = [plist objectForKey:@"quizConfigurationKeys"];
	
	NSArray *grammarRules = [plist objectForKey:@"grammarRules"];
	
	return [self initWithName:name relations:relations grammarRules:grammarRules quizConfigurationKeys:quizConfigurationKeys];
}

- (NSString*) protocolVersion
{
	return FLASH2_PROTOCOL_V1;	
}

- (int) languageVersion
{
	return 0;
}

- (NSDictionary*) upgradeData:(NSDictionary*)data
		  fromLanguageVersion:(int)version
{
	return data; // XXX throw exception
}

- (NSString*) name
{
	return m_name;
}

- (NSString*) keyboardName
{
	return @"U.S.";
}

- (NSArray*) grammarRules
{
	return m_grammarRules;
}

- (NSArray*) relations
{
	return m_relations;
}

- (NSArray*) quizConfigurationKeys
{
	return m_quizConfigurationKeys;
}

- (NSArray*) quizQuestionsForExpiredCards:(NSArray*)expiredCards
					  expiredGrammarRules:(NSString*)expiredGrammarRules
							   freshCards:(NSArray*)freshCards
								   length:(int)length
						quizConfiguration:(NSDictionary*)configuration
{
	return [NSArray array];
}

@end

@implementation Relation

- initWithName:(NSString*)name crossLanguage:(BOOL)crossLanguage 
{
	if ((self = [super init])) {
		m_name = [name copy];
		m_crossLanguage = crossLanguage;
	}
	return self;
}

@synthesize name = m_name;
@synthesize crossLanguage = m_crossLanguage;

@end

@implementation QuizQuestion

- initWithParts:(NSArray*)parts 
{
	if ((self = [super init])) {
		m_parts = [parts copy];		
	}
	return self;
}

@synthesize parts = m_parts;

- (BOOL) purgeDeletedCards:(NSArray*)cards 
{
	NSArray *filteredParts = (id)(intptr_t)[[m_parts filter] purgeDeletedCards:cards];
	[self willChangeValueForKey:@"parts"];
	m_parts = filteredParts;
	[self didChangeValueForKey:@"parts"];
	return ![m_parts isEmpty];
}

@end