//
//  MOutlineView.m
//  MySQLWorkbench
//
//  Created by Alfredo Kojima on 24/May/09.
//  Copyright 2009 Sun Microsystems Inc. All rights reserved.
//

#import "MOutlineView.h"

#import "GRTTreeDataSource.h"
#import "MCPPUtilities.h"

const NSString* NSMenuActionNotification = @"NSMenuActionNotification";

@interface NSObject ()
- (bec::TreeModel*)treeModelForOutlineView:(NSOutlineView*)outline;
@end


@implementation MOutlineView


- (std::vector<bec::NodeId>)selectedNodeIds
{
  std::vector<bec::NodeId> nodes;
  NSIndexSet *iset= [self selectedRowIndexes];
  NSUInteger index = [iset firstIndex];
  while (index != NSNotFound)
  {
    nodes.push_back([[self itemAtRow: index] nodeId]);
    index = [iset indexGreaterThanIndex: index];
  }
  
  return nodes;
}


- (bec::TreeModel*)getTreeModel
{
  bec::TreeModel *model= 0;
  id dataSource= [self dataSource];
  
  // hack because of stupid implementation of DbMySQLTableEditor
  if ([dataSource respondsToSelector: @selector(treeModelForOutlineView:)])
    model= [dataSource treeModelForOutlineView: self];
  
  if ([dataSource respondsToSelector: @selector(treeModel)])
    model= [dataSource treeModel];
  
  return model;
}


- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
  bec::TreeModel *model= [self getTreeModel];
  
  if (model)
  {      
    bec::MenuItemList items= model->get_popup_items_for_nodes([self selectedNodeIds]);
    
    if (!items.empty())
    {
      NSMenu *menu= [[[NSMenu alloc] initWithTitle: @""] autorelease];
      
      for (bec::MenuItemList::const_iterator iter= items.begin(); iter != items.end(); ++iter)
      {
        if (iter->type == bec::MenuSeparator)
          [menu addItem: [NSMenuItem separatorItem]];
        else
        {
          NSMenuItem *item= [menu addItemWithTitle: [NSString stringWithCPPString: iter->caption]
                                action: @selector(activateMenuItem:)
                         keyEquivalent: @""];
          [item setTarget: self];
          if (!iter->enabled)
            [item setEnabled: NO];
          [item setRepresentedObject: [NSString stringWithCPPString: iter->name]];
        }
      }
      return menu;
    }
  }

  return [super menuForEvent: theEvent];
}


- (BOOL) canDeleteItem: (id)sender
{
  bec::TreeModel *model= [self getTreeModel];
  
  std::vector<bec::NodeId> nodes= [self selectedNodeIds];
  
  if (model && nodes.size() == 1)
    return model->can_delete_node(nodes.front());

  return NO;
}


- (void) deleteItem: (id)sender
{
  bec::TreeModel *model= [self getTreeModel];
  
  std::vector<bec::NodeId> nodes= [self selectedNodeIds];
  
  if (model && nodes.size() == 1)
  {
    if (model->delete_node(nodes.front()))
      [self noteNumberOfRowsChanged];
  }
}


- (void)activateMenuItem:(id)sender
{
  bec::TreeModel *model= [self getTreeModel];
  if (model)
  {
    if (model->activate_popup_item_for_nodes([[sender representedObject] UTF8String], 
                                            [self selectedNodeIds]))
      [self reloadData];
    else
      [[NSNotificationCenter defaultCenter] postNotificationName: NSMenuActionNotification object: sender];
  }
}

@end
