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

#import "WbPhysicalLayerEditor.h"
#import "MCPPUtilities.h"

@implementation PhysicalLayerEditor

static void call_refresh(PhysicalLayerEditor *self)
{
  [self refresh];
}


- (id)initWithModule:(grt::Module*)module GRTManager:(bec::GRTManager*)grtm arguments:(const grt::BaseListRef&)args
{
  self= [super initWithModule:module GRTManager:grtm arguments:args];
  if (self != nil)
  {
    // load GUI. Top level view in the nib is the NSTabView that will be docked to the main window
    if (![[NSBundle bundleForClass:[self class]] loadNibFile:@"WbPhysicalLayerEditor"
                                           externalNameTable:[NSDictionary dictionaryWithObject:self forKey:NSNibOwner] 
                                                    withZone:nil])
      NSLog(@"Could not load WbPhysicalLayerEditor.xib");
    
    // take the minimum size of the view from the initial size in the nib.
    // Therefore the nib should be designed as small as possible
    // note: the honouring of the min size is not yet implemented
    [self setMinimumSize: [tabView frame].size];
    
    [self reinitWithArguments: args];
    
    mColorCommitTimer= [NSTimer scheduledTimerWithTimeInterval:2.0
                                                        target:self 
                                                      selector:@selector(saveColorChanges:)
                                                      userInfo:nil
                                                       repeats:YES];
  }
  return self;
}


- (void)reinitWithArguments:(const grt::BaseListRef&)args
{
  [super reinitWithArguments: args];
  delete mBackEnd;
  
  // setup the editor backend with the layer object (args[0])
  mBackEnd= new LayerEditorBE(_grtm, workbench_physical_LayerRef::cast_from(args[0]));
  
  // register a callback that will make [self refresh] get called
  // whenever the backend thinks its needed to refresh the UI from the backend data (ie, the
  // edited object was changed from somewhere else in the application)
  mBackEnd->set_refresh_ui_slot(sigc::bind(sigc::ptr_fun(call_refresh), self));
  // update the UI
  [self refresh];
}



- (void) dealloc
{
  [mColorCommitTimer invalidate];
  
  delete mBackEnd;
  [super dealloc];
}


/** Fetches object info from the backend and update the UI
 */
- (void)refresh
{
  if (mBackEnd)
  {
    [nameText setStringValue: [NSString stringWithCPPString:mBackEnd->get_name()]];
    
    [colorText setStringValue: [NSString stringWithCPPString:mBackEnd->get_color()]];
    NSColor *color= [NSColor colorFromHexString: [NSString stringWithCPPString:mBackEnd->get_color()]];
    if (color)
      [colorWell setColor: color];
  }
}


- (id)identifier
{
  // an identifier for this editor (just take the object id)
  return [NSString stringWithCPPString:mBackEnd->get_object().id()];
}


- (BOOL)matchesIdentifierForClosingEditor:(NSString*)identifier
{
  return mBackEnd->should_close_on_delete_of([identifier UTF8String]);
}


- (NSString*)title
{
  // the title for the editor
  return [NSString stringWithCPPString: strfmt("%s - Layer",mBackEnd->get_name().c_str())];
}


- (NSView*)dockableView
{
  // the view to be docked to the main window
  return tabView;
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
  if ([aNotification object] == nameText)
  {
    // set name of the layer
    mBackEnd->set_name([[nameText stringValue] UTF8String]);
  }
}


- (void)saveColorChanges:(NSTimer*)timer
{
  if (mBackEnd->get_color() != [[colorText stringValue] UTF8String])
  {
    mBackEnd->set_color([[colorText stringValue] UTF8String]);
  }
}


- (IBAction)changeColor:(id)sender
{
  if (sender == colorText)
  {
    NSColor *color= [NSColor colorFromHexString: [colorText stringValue]];
    if (color)
    {
      [mColorCommitTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: 2.0]];
      [colorWell setColor: color];
    }
  }
  else if (sender == colorWell)
  {
    [mColorCommitTimer setFireDate: [NSDate dateWithTimeIntervalSinceNow: 2.0]];
    [colorText setStringValue: [[colorWell color] hexString]];
  }
}

@end
