//
//  Drachma.m
//  drachma
//
//  Created by Niko Matsakis on 7/19/06.
//  Copyright Niko Matsakis 2006 . All rights reserved.
//

#import "Drachma.h"
#import "DrachmaController.h"
#import "DrachmaPayeeManagement.h"
#import "ModeledObjectTypes.h"
#import "OAValueTransformers.h"
#import "OACoreData.h"

@implementation Drachma

+ (void) initialize
{
    OARegisterValueTransformers();
}

// This method is invoked every time a document is loaded, 
// whether it is brand new or not.
- (id)init 
{
    self = [super init];
    if (self != nil) {
        [self initPayeeManagement];
    }
    return self;
}

// This method is only invoked when a BRAND NEW document is created
- (id)initWithType:(NSString *)type error:(NSError **)error {
    self = [super initWithType:type error:error];
    if (self != nil) {
        // Create a Meta object to track the version # of the data, and the
        // two default categories.
        NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
        [[managedObjectContext undoManager] disableUndoRegistration];
        [NSEntityDescription insertNewObjectForEntityForName:@"Meta"
                                      inManagedObjectContext:managedObjectContext];
        
        Category *salary = 
            [NSEntityDescription insertNewObjectForEntityForName:@"Category"
                                          inManagedObjectContext:managedObjectContext];
        [salary setName:@"Salary"];
        [salary setImmutable:[NSNumber numberWithBool:YES]];
        [salary setSalary:[NSNumber numberWithBool:YES]];
        
        Category *uncategorized = 
            [NSEntityDescription insertNewObjectForEntityForName:@"Category"
                                          inManagedObjectContext:managedObjectContext];
        [uncategorized setName:@"Uncategorized"];
        [uncategorized setImmutable:[NSNumber numberWithBool:YES]];
        
        [managedObjectContext processPendingChanges];
        [[managedObjectContext undoManager] enableUndoRegistration];
    }
    return self;
}

- (void) dealloc
{
    [self deallocPayeeManagement];
    [m_model release];
    [super dealloc];
}

- (void)makeWindowControllers
{
    [self addWindowController:[DrachmaController drachmaControllerWithDrachma:self]];
}

- (void)windowControllerDidLoadNib:(NSWindowController *)windowController 
{
    [super windowControllerDidLoadNib:windowController];
    // user interface preparation code
}

- (id) managedObjectModel
{
    if (m_model == nil) {
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        NSString *path = [bundle pathForResource:@"Drachma-002" ofType:@"mom"];
        NSURL *url = [NSURL fileURLWithPath:path];    
        m_model = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
    }
    return m_model;
}

- (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
    return [super setManagedObjectContext:managedObjectContext];
}

- (NSManagedObjectContext *)managedObjectContext
{
    return [super managedObjectContext];
}

- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
    return [super readFromURL:absoluteURL ofType:typeName error:outError];
}

- (NSArray*) sortedBudgets
{
    NSManagedObjectContext *mctx = [self managedObjectContext];
    NSFetchRequest *req = [[[NSFetchRequest alloc] init] autorelease];
    [req setEntity:[NSEntityDescription entityForName:@"Budget" inManagedObjectContext:mctx]];
    NSSortDescriptor *sortDesc = [[[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES] autorelease];
    [req setSortDescriptors:[NSArray arrayWithObject:sortDesc]];
    return [mctx executeArrayQueryFromFetchRequest:req];
}

@end
