#import "ColumnListEditor.h"
#import "NamePrompter.h"
#import "NSArrayExtensions.h"
#import "Actions.h"

/* The items in our _data array are not just string sbut
instead instances of this wrapper class.  This is because
the Action methods for removing objects don't take an index
and want a unique object to remove, and uising NSStrings
I can't guarantee that.  The Action interface is kind of dumb...
but it is convenient most of the time! */

@interface ColumnTitle : NSObject 
{
    NSString *_title;
}

+ titleWithTitle:(NSString*)title;

- (NSString*) description;
    
@end

@implementation ColumnTitle

+ titleWithTitle:(NSString*)title
{
    ColumnTitle *res = [[[ColumnTitle alloc] init] autorelease];
    if (res) {
        res->_title = [title retain];
    }
    
    return res;
}

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

- (NSString*) description
{
    return _title;
}

@end

@implementation ColumnListEditor

- (void) setListData:(NSArray*)data
{
    [_data release];
    _data = [[NSMutableArray alloc] initWithCapacity:32];
    
    NSEnumerator *oenum = [data objectEnumerator];
    id obj;
    while ((obj = [oenum nextObject])) {
        [_data addObject:[ColumnTitle titleWithTitle:[obj description]]];
    }
    
    [_columnList reloadData];
}

- (NSArray*) stringArray
{
    return [_data mappedArray:@selector(description)];
}

- (IBAction)addColumnTitle:(id)sender
{
    if (_data) {
        [NamePrompter promptForName:self
                           selector:@selector(addColumnTitleNamed:)
                             window:[_target window]];
    }
}

- (void) addColumnTitleNamed:(NSString*)name
{
    [Action addObject:[ColumnTitle titleWithTitle:name]
                   to:_data 
              atIndex:-1 
               target:_target];
    [_columnList reloadData];
}

- (IBAction)deleteColumnTitle:(id)sender
{
    // remove selected indices:
    if (_data) {
        NSIndexSet *indices = [_columnList selectedRowIndexes];
        NSEnumerator *enumer = [[_data objectsAtIndices:indices] objectEnumerator];
        id deadman;
        while ( (deadman = [enumer nextObject]) ) {
            [Action removeObject:deadman from:_data target:_target];
        }
        [_columnList reloadData];
    }
}

- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
    if (_data) return [_data count];
    return 0;
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
    assert (_data);
    return [[_data objectAtIndex:rowIndex] description];
}

- (void)tableView:(NSTableView *)aTableView 
   setObjectValue:(id)anObject 
   forTableColumn:(NSTableColumn *)aTableColumn 
              row:(int)rowIndex
{
    id oldval = [_data objectAtIndex:rowIndex];
    
    // add a new value at the same index
    [Action addObject:[ColumnTitle titleWithTitle:[anObject description]]
                   to:_data
              atIndex:rowIndex
               target:_target];
               
    // remove the old one
    [Action removeObject:oldval
                    from:_data
                  target:_target];
}

@end
