/* 
 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */
#ifndef _LF_FILECHOOSER_H_
#define _LF_FILECHOOSER_H_

#include <mforms/mforms.h>

#include "lf_mforms.h"
#include "lf_view.h"
#include "base/file_utilities.h"

#define RESPONSE_OK 1
#define RESPONSE_CANCEL 0



namespace mforms {
namespace gtk {

class FileChooserImpl : public ViewImpl
{
  Gtk::FileChooserDialog *_dlg;
  Gtk::Widget *get_outer() const { return _dlg; }

  Gtk::Table *_options_table;
  std::map<std::string, Gtk::ComboBoxText*> _combos;
 
  static std::vector<std::string> split_string(const std::string &s, const std::string &sep)
  {
    std::vector<std::string> parts;
    std::string ss= s;

    std::string::size_type p;

    if (s.empty())
      return parts;

    p= ss.find(sep);
    while (!ss.empty() && p != std::string::npos)
    {
      parts.push_back(ss.substr(0, p));
      ss= ss.substr(p+sep.size());

      p= ss.find(sep);
    }
    parts.push_back(ss);

    return parts;
  
  }
  
  static bool create(::mforms::FileChooser *self, ::mforms::Form *owner, ::mforms::FileChooserType type, const bool show_hidden)
  {
    return new FileChooserImpl(self, owner, type, show_hidden) != 0;
  }

  static void set_title(::mforms::FileChooser *self, const std::string &title)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if ( dlg )
      dlg->_dlg->set_title(title);
  }

  static bool show_modal(::mforms::FileChooser *self)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    bool res;
    res= dlg->_dlg->run() == RESPONSE_OK;
    dlg->_dlg->hide();
    return res;
  }

  static void set_directory(FileChooser *self, const std::string &path)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if ( dlg )
    {
      dlg->_dlg->set_current_folder(path);
    }
  }

  static std::string get_directory(FileChooser *self)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if ( dlg )
    {
      return dlg->_dlg->get_current_folder();
    }
    return "";
  }


  static std::string get_path(FileChooser *self)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    return dlg ? dlg->_dlg->get_filename().raw() : "";
  }

  static void set_path(FileChooser *self, const std::string &path)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    dlg->_dlg->set_filename(path);

    std::string ext = base::extension(path);
    Gtk::ComboBoxText *combo = dlg->_combos["format"];
    if (combo)
    {
      std::vector<std::string> &extensions(self->_selector_options["format"]);
      std::vector<std::string>::const_iterator it = std::find(extensions.begin(), extensions.end(), ext.substr(1));
      if (it != extensions.end())
        combo->set_active(it - extensions.begin());
    }
  }

  static void add_selector_option(FileChooser *self, const std::string &name, const std::string &label, const std::vector<std::pair<std::string,std::string> > &values)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    int row;
    if (!dlg->_options_table)
    {
      dlg->_options_table = Gtk::manage(new Gtk::Table(1, 2));
      dlg->_options_table->set_col_spacings(4);
      dlg->_dlg->set_extra_widget(*dlg->_options_table);
      row = 0;
    }
    else
    {
      row = dlg->_options_table->property_n_rows().get_value();
      dlg->_options_table->property_n_rows().set_value(row + 1);
    }
    Gtk::ComboBoxText *combo = 0; 
    if (!(combo = dlg->_combos[name]))
    {
      combo = dlg->_combos[name] = Gtk::manage(new Gtk::ComboBoxText());
      dlg->_options_table->attach(*Gtk::manage(new Gtk::Label(label)), 0, 1, row, row+1, Gtk::FILL, Gtk::FILL);
      dlg->_options_table->attach(*dlg->_combos[name], 1, 2, row, row+1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL);
      dlg->_options_table->show_all();
    }
    //combo->remove_all();
    combo->clear_items();
    for (std::vector<std::pair<std::string,std::string> >::const_iterator iter = values.begin(); iter != values.end(); ++iter)
      combo->append_text(iter->first);
    combo->set_active(0);
  }

  static std::string get_selector_option_value(FileChooser *self, const std::string &name)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if (dlg->_combos[name])
    {
      int i = dlg->_combos[name]->get_active_row_number();
      if (i >= 0)
        return self->_selector_options[name][i];
    }
    return "";
  }

  static void set_extensions(FileChooser *self, const std::string &extensions, const std::string &default_extension)
  {
    FileChooserImpl *dlg = self->get_data<FileChooserImpl>();
    if (dlg)
    {      
      // extensions format:
      // AAA Files (*.aaa)|*.aaa,BBB Files (*.bbb)
      std::vector<std::pair<std::string, std::string> > exts(self->split_extensions(extensions));
      
      for (std::vector<std::pair<std::string, std::string> >::const_iterator iter= exts.begin();
           iter != exts.end(); ++iter)
      {
        Gtk::FileFilter filter;
        
        filter.add_pattern(iter->second);
        filter.set_name(iter->first);
        
        dlg->_dlg->add_filter(filter);
      }
      
      Gtk::FileFilter filter;
      filter.add_pattern("*");
      filter.set_name("All Files");

      dlg->_dlg->add_filter(filter);
    }
  }

  FileChooserImpl(::mforms::FileChooser *form, ::mforms::Form *owner, ::mforms::FileChooserType type, bool show_hidden)
    : ViewImpl(form), _options_table(0)
  {
    // TODO: enable showing hidden files/folders.
    //FileChooserDialog *dialog = 0;
    switch (type)
    {
    case ::mforms::OpenFile:
      _dlg = new Gtk::FileChooserDialog("Open File...", Gtk::FILE_CHOOSER_ACTION_OPEN);
      _dlg->add_button(Gtk::Stock::CANCEL, RESPONSE_CANCEL);
      _dlg->add_button(Gtk::Stock::OPEN, RESPONSE_OK);
      _dlg->set_default_response(RESPONSE_OK);
      break;
    case ::mforms::SaveFile:
      _dlg = new Gtk::FileChooserDialog("Save File...", Gtk::FILE_CHOOSER_ACTION_SAVE);
      _dlg->add_button(Gtk::Stock::CANCEL, RESPONSE_CANCEL);
      _dlg->add_button(Gtk::Stock::SAVE, RESPONSE_OK);
      _dlg->set_do_overwrite_confirmation(true);
      _dlg->set_default_response(RESPONSE_OK);
      break;
    case ::mforms::OpenDirectory:
      _dlg = new Gtk::FileChooserDialog("Open Directory...", Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
      _dlg->add_button(Gtk::Stock::CANCEL, RESPONSE_CANCEL);
      _dlg->add_button(Gtk::Stock::OPEN, RESPONSE_OK);
      _dlg->set_default_response(RESPONSE_OK);
      break;      
    }
    if (owner)
    {
      FormImpl *fi = owner->get_data<FormImpl>();
      if (fi && fi->get_window())
        _dlg->set_transient_for(*fi->get_window());
    }
  }

  virtual ~FileChooserImpl()
  {
    delete _dlg;
  }

 public:
  static void init()
  {
    ::mforms::ControlFactory *f = ::mforms::ControlFactory::get_instance();

    f->_filechooser_impl.create = &FileChooserImpl::create;
    f->_filechooser_impl.set_title = &FileChooserImpl::set_title;
    f->_filechooser_impl.run_modal = &FileChooserImpl::show_modal;
    f->_filechooser_impl.set_extensions = &FileChooserImpl::set_extensions;
    f->_filechooser_impl.set_directory = &FileChooserImpl::set_directory;
    f->_filechooser_impl.get_directory = &FileChooserImpl::get_directory;
    f->_filechooser_impl.get_path = &FileChooserImpl::get_path;
    f->_filechooser_impl.set_path = &FileChooserImpl::set_path;
    f->_filechooser_impl.add_selector_option = &FileChooserImpl::add_selector_option;
    f->_filechooser_impl.get_selector_option_value = &FileChooserImpl::get_selector_option_value;
  }
};

};
};

#endif
