//
//  EditCardsController.m
//  Flash2
//
//  Copyright 2008 Nicholas Matsakis. 
//  The code in thie file is released to the public under the
//  terms of the MIT License as described here:
//  http://www.opensource.org/licenses/mit-license.php
//

#import "EditCardsController.h"
#import "Relation.h"
#import "OxMemoize.h"
#import "OxCoreData.h"
#import "OxKeyValue.h"
#import "Card.h"

@implementation EditCardsController

@synthesize language = a_language;

#pragma mark -
#pragma mark Initialization

- (void) awakeFromNib {
    m_mctx = [m_dict managedObjectContext];
    [self reset:self];
    [self addObserver:self forKeyPath:@"language" options:0 context:nil];
}

- (void) observeValueForKeyPath:(NSString*)keyPath 
                       ofObject:(id)object 
                         change:(NSDictionary*)change 
                        context:(void*)context
{
    invokeObservationSelector(self, keyPath, object, change, context);
}

#pragma mark -
#pragma mark New Cards

@synthesize newCards = a_newCards;

- (void) addEmptyCard:(id)sender
{  
    if (self.language == nil)
        return;
        
    if (m_lastCard) [m_lastCard removeObserver:self forKeyPath:@"relation"];   
    m_lastCard = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                  @"", @"fromString", @"", @"toString", [NSNull null], @"relation", nil];
    [m_lastCard addObserver:self forKeyPath:@"relation" options:0 context:nil];
    
    self.newCards = [self.newCards arrayByAddingObject:m_lastCard];
}

- (void) observeValueForRelationOfObject:(id)object 
                                  change:(NSDictionary*)change 
                                 context:(void*)context
{
    NSAssert(object == m_lastCard, @"Observation for an object besides m_lastCard");
    NSAssert(m_lastCard, @"m_lastCard nil");
    [self addEmptyCard:self];
}

- (void) observeValueForLanguageOfObject:(id)object 
                                  change:(NSDictionary*)change 
                                 context:(void*)context
{
    // when language changes, reset the relations and all
    NSAssert(object == self, @"Observation for an object besides m_lastCard");
    [self reset:self];
}

#pragma mark -
#pragma mark Existing Cards

+ (NSSet*) keyPathsForValuesAffectingExistingCards {
    return [NSSet setWithObjects:@"newCards", nil];
}

- (NSPredicate*) existingCards
{
    //NSArray *newCards = self.newCards;
	return nil; //[NSPredicate predicateWithFormat:@"true"];
	/*
    return [NSPredicate predicateWithFormat:
            @"fromString IN %@.@distinctUnionOfSets.fromString || "
            @"fromString IN %@.@distinctUnionOfSets.toString || "
            @"toString IN %@.@distinctUnionOfSets.fromString || "
            @"toString IN %@.@distinctUnionOfSets.toString", 
            newCards, newCards, newCards, newCards];
	 */
}

#pragma mark -
#pragma mark Actions

- (IBAction) addAll:(id)sender {
	NSNull *null = [NSNull null];
    for (NSDictionary *dict in self.newCards) {
        NSString *fromString = [dict valueForKey:@"fromString"];
        Relation *relation = [dict valueForKey:@"relation"];
        NSString *toString = [dict valueForKey:@"toString"];
        
        [dict setValue:@"" forKey:@"fromString"];
        [dict setValue:@"" forKey:@"toString"];
        
        if ([fromString isEqualToString:@""] || (id)relation == null || [toString isEqualToString:@""]) 
            continue;
        
        Card *card = [NSEntityDescription insertNewObjectForEntityForName:@"Card"
                                                   inManagedObjectContext:m_mctx];
        card.fromString = fromString;
        card.relation = relation;
        card.toString = toString;        
    }
}

- (IBAction) reset:(id)sender {
    self.newCards = [NSArray array];
    [self addEmptyCard:nil];
}

@end
