//
//  MFUtilities.mm
//  MySQLWorkbench
//
//  Created by Alfredo Kojima on 5/Mar/09.
//  Copyright 2009 Sun Microsystems Inc. All rights reserved.
//

#import "MFUtilities.h"

#include "mforms/mforms.h"
#import "MFBase.h"


static int util_show_message(const std::string &title, const std::string &text,
                             const std::string &ok, const std::string &cancel,
                             const std::string &other)
{
  int res= NSRunAlertPanel(wrap_nsstring(title), wrap_nsstring(text), 
                           ok.empty() ? nil : wrap_nsstring(ok),
                           cancel.empty() ? nil : wrap_nsstring(cancel),
                           other.empty() ? nil : wrap_nsstring(other));
  
  if (res == NSAlertDefaultReturn)
    return mforms::ResultOk;
  else if (res == NSAlertAlternateReturn)
    return mforms::ResultCancel;
  else
    return mforms::ResultOther;
}


static int util_show_error(const std::string &title, const std::string &text,
                           const std::string &ok, const std::string &cancel,
                           const std::string &other)
{
  int res= NSRunAlertPanel(wrap_nsstring(title), wrap_nsstring(text), 
                           ok.empty() ? nil : wrap_nsstring(ok),
                           cancel.empty() ? nil : wrap_nsstring(cancel),
                           other.empty() ? nil : wrap_nsstring(other));
  
  if (res == NSAlertDefaultReturn)
    return mforms::ResultOk;
  else if (res == NSAlertAlternateReturn)
    return mforms::ResultCancel;
  else
    return mforms::ResultOther;
}


static int util_show_warning(const std::string &title, const std::string &text,
                             const std::string &ok, const std::string &cancel,
                             const std::string &other)
{
  int res= NSRunAlertPanel(wrap_nsstring(title), wrap_nsstring(text), 
                           ok.empty() ? nil : wrap_nsstring(ok),
                           cancel.empty() ? nil : wrap_nsstring(cancel),
                           other.empty() ? nil : wrap_nsstring(other));
  
  if (res == NSAlertDefaultReturn)
    return mforms::ResultOk;
  else if (res == NSAlertAlternateReturn)
    return mforms::ResultCancel;
  else
    return mforms::ResultOther;
}


static void util_set_clipboard_text(const std::string &text)
{
  NSPasteboard *pasteBoard= [NSPasteboard generalPasteboard];
  [pasteBoard declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner:nil];
  [pasteBoard setString: [NSString stringWithUTF8String:text.c_str()]
                                      forType: NSStringPboardType];
}

static std::string util_get_clipboard_text()
{
  NSPasteboard *pasteBoard= [NSPasteboard generalPasteboard];
  return [[pasteBoard stringForType: NSStringPboardType] UTF8String] ?:"";
}

static void util_open_url(const std::string &url)
{
  [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString: [NSString stringWithUTF8String: url.c_str()]]];
}


static std::string get_special_folder(mforms::FolderType type)
{
  switch (type)
  {
    case mforms::Documents:
      return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] UTF8String];
    case mforms::Desktop:
      return [[NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) lastObject] UTF8String];
  }
  return "";  
}


void cf_util_init()
{
  ::mforms::ControlFactory *f = ::mforms::ControlFactory::get_instance();
    
  f->_utilities_impl.show_message= &util_show_message;
  f->_utilities_impl.show_error= &util_show_error;
  f->_utilities_impl.show_warning= &util_show_warning;
  f->_utilities_impl.set_clipboard_text= &util_set_clipboard_text;
  f->_utilities_impl.get_clipboard_text= &util_get_clipboard_text;
  f->_utilities_impl.open_url= &util_open_url;
  f->_utilities_impl.get_special_folder= &get_special_folder;
}
