/*
 Oaxaca: Useful Cocoa Library
 Copyright (C) 2005, Niko Matsakis (niko@alum.mit.edu)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#import "OAFormPrompter.h"
#import "OACategories.h"

@implementation OAFormPrompter

+ (BOOL) promptForItems:(NSArray*)items target:target selector:(SEL)whenadded window:(NSWindow*)win;
{
  // Create the OAFormPrompter object
  OAFormPrompter *newprompt = 
    [[OAFormPrompter alloc] initWithTarget:target selector:whenadded];
  [NSBundle loadNibNamed:@"OAFormPrompter" owner:newprompt];
  
  // Set up the form to have one entry for each item
  [newprompt prepareFormWithItems:items];
  
  // Attach the sheet
  [NSApp beginSheet: newprompt->_window
         modalForWindow: win
         modalDelegate: nil
         didEndSelector: nil
         contextInfo: nil]; 
  [NSApp runModalForWindow: newprompt->_window]; 

  // (The app is running modally here.  When the Add or Don't Add are clicked,
  // the modal session will end.)

  BOOL result = newprompt->_result;

  // Once it's over...
  [NSApp endSheet:newprompt->_window];
  [newprompt->_window orderOut:self];
  [newprompt release];
  return result;
}

- initWithTarget:target selector:(SEL)whenadded
{
  if ((self = [super init])) {
      _target = target;
      _whenAdded = whenadded;
  }
  return self;
}

- (IBAction) add:sender
{
    [_target performSelector:_whenAdded withObject:[_form valuesAsDictionary]];
    _result = YES;
    [NSApp stopModal];
}

- (IBAction) dontAdd:sender
{
    [NSApp stopModal];
}

- (void) prepareFormWithItems:(NSArray*)items
{
    NSSize winsize = [_window contentMinSize];
    NSSize cellsize;

    // form always begins with one dummy entry, delete it
    [_form removeEntryAtIndex:0];
    
    // add one entry for each item
    NSEnumerator *itemsenum = [items objectEnumerator];
    id item;
    while ((item = [itemsenum nextObject])) {
        NSFormCell *fcell = [_form addEntry:[NSString stringWithFormat:@"%@:", item]];
        cellsize = [fcell cellSize];
    }
    
    int itemcnt = [items count];
    if (itemcnt > 0) {
        float reqsize = cellsize.height * itemcnt; 
    
        // we need to resize the window to account for the form
        winsize.height += reqsize - cellsize.height;  // already had one row built in
        [_window setContentSize:winsize];
        NSLog (@"OAFormPrompter reqsize=%f itemcnt=%d matrixrow=%d\n",
               reqsize, itemcnt, [_form numberOfRows]);
        NSRect frame = [_form bounds];
        NSLog (@"OAFormPrompter form bounds=(%f,%f)x(%f,%f)",
               frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
        [_form sizeToCells];
        frame = [_form bounds];
        NSLog (@"OAFormPrompter form bounds=(%f,%f)x(%f,%f)",
               frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
    }
}

@end
