// src/add_request_dialog.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This 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; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "add_request_dialog.hh"

#include <sgpemv2/templates/sequences.tcc>

#include <sgpemv2/history.hh>
#include <sgpemv2/environment.hh>
#include <sgpemv2/simulation.hh>
#include <sgpemv2/resource.hh>

#include <iostream>
#include <sstream>

#include "gettext.h"

#include <gtkmm/spinbutton.h>

#include <cassert>

using namespace sgpem;
using namespace Gtk;
using namespace Glib;
using Gnome::Glade::Xml;
using std::vector;

AddRequestDialog::AddRequestDialog(BaseObjectType* cobject, const RefPtr<Xml>& glade) :
  Dialog(cobject), _glade(glade)
{
  _glade->get_widget("SubRequests.View", _list);
  _glade->get_widget("Add", _add_button);
  _glade->get_widget("Remove", _remove_button);
  _glade->get_widget("Resource.Combo", _resource_combo);
  _glade->get_widget("OK.Button", _ok_button);
  _glade->get_widget("Instant.Spin", _instant_spin);
  _glade->get_widget("Duration.Spin", _duration_spin);


  /** ATTACH SIGNAL HANDLERS FOR BUTTONS **/
  
  _add_button->signal_clicked().connect(
      sigc::mem_fun(*this, &AddRequestDialog::_on_add));

  _remove_button->signal_clicked().connect(
      sigc::mem_fun(*this, &AddRequestDialog::_on_remove));

  _ok_button->set_sensitive(false);
  _remove_button->set_sensitive(false);
  _add_button->set_sensitive(false);
  
  /** INITIALIZE COMBOBOX **/
  _combo_columns.add(_combo_key_column);
  _combo_columns.add(_combo_resource_column);

  _combo_model = ListStore::create(_combo_columns);
  _resource_combo->set_model(_combo_model);
  _resource_combo->pack_start(_combo_key_column, false);
  _resource_combo->pack_start(_combo_resource_column, true);

  _resource_combo->signal_changed().connect(
      sigc::mem_fun(*this, &AddRequestDialog::_on_combo_selection_changed));
  
  /** INITIALIZE LISTVIEW **/
  
  _list_columns.add(_list_key_column);
  _list_columns.add(_list_resource_column);
  _list_columns.add(_list_length_column);
  
  _list_model = ListStore::create(_list_columns);
  _list->set_model(_list_model);

  _list_model->signal_row_deleted().connect(
      sigc::mem_fun(*this, &AddRequestDialog::_on_row_removed));

  _list_model->signal_row_inserted().connect(
      sigc::mem_fun(*this, &AddRequestDialog::_on_row_added));

  _list->append_column(_("key"), _list_key_column);
  _list->append_column(_("resource"), _list_resource_column);
  _list->append_column(_("length"), _list_length_column);

  _list->get_selection()->signal_changed().connect(
      sigc::mem_fun(*this, &AddRequestDialog::_on_list_selection_changed));
}

Request*
AddRequestDialog::run_add(Thread& owner)
{
  update_combo();

  Request* r = NULL;
  
  // reset the dialog data
//  _list_model->clear();
//  _instant_spin->set_value(0.0);
//  _duration_spin->set_value(0.0);
  
  if(run() == RESPONSE_OK)
  {
    assert(_list_model->children());

    History& h = Simulation::get_instance().get_history();

    r = &h.add_request(owner, _instant_spin->get_value_as_int());

    TreeNodeChildren sreq_container = _list_model->children();

    for(Iseq<TreeIter> it = iseq(sreq_container); it; ++it)
      h.add_subrequest(*r, (*it)[_list_key_column], (*it)[_list_length_column]);
  }

  hide();

  return r;
}

void
AddRequestDialog::run_edit(Request& request)
{
  update_combo();

  _list_model->clear();

  History& history = Simulation::get_instance().get_history();
  const Environment::Resources& resources = history.get_last_environment().get_resources();
  
  _instant_spin->set_value(static_cast<double>(request.get_instant()));
  
  // PLEASE KEEP THIS A COPY, WE *NEED* TO COPY IT
  vector<SubRequest*> subrequests = request.get_subrequests();

  for(Iseq<vector<SubRequest*>::iterator> it = iseq(subrequests); it; ++it)
  {
    SubRequest& sr = *(*it);

    TreeModel::Row row = *(_list_model->append());

    unsigned int key = sr.get_resource_key();
    row[_list_key_column] = key;

    const ustring name = resources.find(key)->second->get_name();
    row[_list_resource_column] = name;

    row[_list_length_column] = sr.get_length();
  }

  if(run() == RESPONSE_OK)
  {
    assert(_list_model->children());

    // I know it's a bit hack-ish, but do you know an elegant alternative way?
    for(Iseq<vector<SubRequest*>::iterator> it = iseq(subrequests); it; ++it)
      history.remove(*(*it));

    history.edit_request(request, _instant_spin->get_value_as_int());

    TreeNodeChildren sreq_container = _list_model->children();

    for(Iseq<TreeIter> it = iseq(sreq_container); it; ++it)
      history.add_subrequest(request, (*it)[_list_key_column], (*it)[_list_length_column]);
  }

  hide();
}

void
AddRequestDialog::update_combo()
{
  typedef Environment::Resources::const_iterator ResourceIt;
  
  const Environment::Resources& resources = 
    Simulation::get_instance().get_history().get_last_environment().get_resources();
  
  _combo_model->clear();
  
  for(Iseq<ResourceIt> it = iseq(resources); it; ++it)
  {
    TreeModel::Row row = *(_combo_model->append());
    row[_combo_key_column] = it->first;
    row[_combo_resource_column] = it->second->get_name();
  }
  
  Dialog::on_show();
}

void
AddRequestDialog::_on_add()
{
  TreeModel::iterator sel = _resource_combo->get_active();

  TreeModel::Row row = *(_list_model->append());

  const unsigned int key = (*sel)[_combo_key_column];
  row[_list_key_column] = key;

  const ustring resource = (*sel)[_combo_resource_column];
  row[_list_resource_column] = resource;

  const unsigned int length = _duration_spin->get_value_as_int();
  row[_list_length_column] = length;
}

void
AddRequestDialog::_on_remove()
{
  TreeModel::iterator it = _list->get_selection()->get_selected();
  _list_model->erase(it);
}

void 
AddRequestDialog::_on_list_selection_changed()
{
  _remove_button->set_sensitive(
      _list->get_selection()->count_selected_rows() > 0);
}

void
AddRequestDialog::_on_row_added(const Gtk::TreePath& path, const Gtk::TreeIter& it)
{
  _ok_button->set_sensitive(true);
}

void 
AddRequestDialog::_on_row_removed(const Gtk::TreePath& path)
{
  _ok_button->set_sensitive(static_cast<bool>(_list_model->children()));
}

void 
AddRequestDialog::_on_combo_selection_changed()
{
  _add_button->set_sensitive(
      static_cast<bool>(_resource_combo->get_active()));
}

