#import "ExpenseTransferer.h"
#import "DrachmaController.h"
#import "Drachma.h"
#import "OACoreData.h"
#import "OAMacros.h"
#import "OACategories.h"
#import "ModeledObjectTypes.h"

@implementation ExpenseTransferer

- (void) initiateTransferOfSelectedExpenses:(NSArray*)expenses 
                             delegate:delegate 
                             selector:(SEL)sel
                               window:(NSWindow*)window
{
	m_expenses = [expenses retain];
    m_delegate = delegate;
    m_whenDone = sel;
    
    [m_categoryController setSelectionIndex:0];
    
    [NSApp beginSheet:m_sheet
       modalForWindow:window 
        modalDelegate:self
       didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
          contextInfo:nil];
}

- (void) dealloc 
{
	[m_expenses release];
	[super dealloc];
}

- (void) didEndSheet:sheet returnCode:(int)code contextInfo:(void*)ctx
{
    [m_sheet orderOut:self];
    [m_delegate performSelector:m_whenDone 
                     withObject:[NSNumber numberWithInt:code]];
    m_delegate = nil;
}

- (IBAction)dontTransfer:(id)sender
{
    [NSApp endSheet:m_sheet returnCode:NO];
}

- (IBAction)transfer:(id)sender
{
    Category *newCategory = [m_categoryController selectedObject];
    // TODO: ensure that this is not nULL
    assert (newCategory != nil);

    FOREACH(Expense *, expense, m_expenses) {
        [expense setCategory:newCategory];
    } ENDFOREACH;
    [NSApp endSheet:m_sheet returnCode:YES];
}

@end
