//
//  WBOverviewWorkbenchCentral.mm
//  MySQLWorkbench
//
//  Created by Alfredo Kojima on 6/Apr/09.
//  Copyright 2009 Sun Microsystems Inc. All rights reserved.
//

#import "sigobjc++.h"
#import "WBOverviewPanel.h"
#import "WBOverviewWorkbenchCentral.h"
#include "workbench/wb_overview_workspace.h"

@implementation WBOverviewWorkbenchCentral

static NSMutableAttributedString* makeAttributedStringWithColor(NSAttributedString* text, NSColor *color,
                                                                NSFont *font)
{
  NSMutableAttributedString* attrTitle = [[NSMutableAttributedString alloc] initWithAttributedString:text];
  int len = [attrTitle length];
  NSRange range = NSMakeRange(0, len);
  [attrTitle addAttribute:NSForegroundColorAttributeName 
                    value:color
                    range:range];
  [attrTitle addAttribute:NSFontAttributeName
                    value:font
                    range:range];
  [attrTitle fixAttributesInRange:range];
  
  return [attrTitle autorelease];
}


static void DrawTiledImage(NSImage *image, NSRect rect, BOOL composite)
{
  NSSize imageSize= [image size];

  for (CGFloat y= 0; y < rect.size.height; y+= imageSize.height)
  {
    for (CGFloat x= 0; x < rect.size.width; x+= imageSize.width)
    {
      [image drawAtPoint:NSMakePoint(rect.origin.x+x, rect.origin.y+y) 
                fromRect:NSMakeRect(0, 0, imageSize.width, imageSize.height)
               operation:composite ? NSCompositeSourceOver : NSCompositeCopy 
                fraction:1.0];
    }
  }
}

- (void)drawRect:(NSRect)frame
{
  NSRect rect= [self frame];
  rect.origin= NSZeroPoint;
  
  [background compositeToPoint:NSMakePoint(0, 0) operation: NSCompositeCopy];

  rect.origin.x+= 263;
  rect.size.width-= 263;
  
  DrawTiledImage(backgroundTile, rect, NO);
}


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


- (void)awakeFromNib
{
  background= [[NSImage imageNamed: @"wb_central"] retain];
  backgroundTile= [[NSImage imageNamed: @"wb_central_bg"] retain];
}

- (void)setOverview:(WBOverviewPanel*)overview
{
  for (id child in [self subviews])
  {
    if ([child isKindOfClass: [NSButton class]])
    {
      std::string str, url;
      [overview backend]->get_field(bec::NodeId(0).append([child tag]), wb::OverviewBE::Label, str);
      [overview backend]->get_field(bec::NodeId(0).append([child tag]), wb::WorkspaceOverviewBE::LinkURL, url);

      NSString *title= [NSString stringWithUTF8String: str.c_str()];
      NSAttributedString *attributedTitle= [[[NSAttributedString alloc] initWithString: title] autorelease];

      [[child cell] setRepresentedObject: [NSString stringWithUTF8String: url.c_str()]];
      
      if ([child tag] < 100)
      {
        [child setAttributedTitle: makeAttributedStringWithColor(attributedTitle, [NSColor whiteColor], [child font])];
        [child setAttributedAlternateTitle: makeAttributedStringWithColor(attributedTitle, [NSColor blueColor], [child font])];
        [child sizeToFit];
      }
      else
      {
        [child setTitle: title];
      }
    }
    else if ([child isKindOfClass: [NSTextField class]])
    {
      std::string str;
      [overview backend]->get_field(bec::NodeId(0).append([child tag]), wb::WorkspaceOverviewBE::LinkDescription, str);

      NSString *title= [NSString stringWithUTF8String: str.c_str()];
      NSAttributedString *attributedTitle= [[[NSAttributedString alloc] initWithString: title] autorelease];

      [child setAttributedStringValue: makeAttributedStringWithColor(attributedTitle,
                                                                     [NSColor whiteColor],
                                                                     [child font])];
      [child sizeToFit];
    }
  }
}


- (IBAction)buttonClicked:(id)sender
{
  [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: [[sender cell] representedObject]]];
}

@end
