// 
//  Language.m
//  Flash2
//
//  Created by Nicholas Matsakis on 4/2/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Models.h"
#import "OxNSArray.h"
#import "OxNSString.h"

@implementation Language 

@dynamic name;
@dynamic keyboardLayout;
@dynamic rootCategory;
@dynamic allCategories;

- (NSComparisonResult) compareName:(Language*)otherLang {
    return [self.name compareIgnoringCase:otherLang.name];
}

int reorder(Category *category, int nxt) {    
    category.order = [NSNumber numberWithInt:nxt++];
    
    // order the relationships within the category as well
    NSArray *fromRel = [category.relationshipsFrom allObjects];
    fromRel = [fromRel sortedArrayUsingKey:K_RELATIONSHIP_ORDER];
    for (Relationship *rel in category.relationshipsFrom)
        rel.order = [NSNumber numberWithInt:nxt++];    
    
    // order any sub-categories
    NSArray *subCategories = [category.subCategories allObjects];
    subCategories = [subCategories sortedArrayUsingKey:K_CATEGORY_ORDER];
    for (Category *subCat in subCategories)
        nxt = reorder(subCat, nxt);
    
    return nxt;
}

- (void) reorderRelationshipsAndCategories {
    reorder(self.rootCategory, 0);
}

- (NSArray*) allRelationships {
    return [[self.allCategories allObjects] valueForKeyPath:@"@unionOfSets.relationshipsFrom"];
}

@end
