// src/add_request_dialog.hh - 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 3 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, see http://www.gnu.org/licenses/.


#ifndef ADD_REQUEST_DIALOG_HH
#define ADD_REQUEST_DIALOG_HH 1

namespace sgpem
{
  class Request;
  class Thread;
  class AddRequestDialog;
}  

#include <gtkmm/treeview.h>
#include <gtkmm/liststore.h>
#include <gtkmm/dialog.h>
#include <gtkmm/combobox.h>
#include <gtkmm/spinbutton.h>
#include <gtkmm/builder.h>

namespace sgpem
{
  /** \brief A dialog derived from add-request-dialog.ui
   *
   * Manages the list of subrequests, ensures only "nonempty" requests
   * are created, and supports also editing.
   */ 
  class AddRequestDialog : public Gtk::Dialog
  {
  public:
    /** \brief Constructor required by gtkbuilder
     */
    AddRequestDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& ui);

    /** \brief Attemts to show the dialog, and constructs a new request 
      * if response is OK 
      * \param owner The thread which will own the request
      * \return A pointer to the created request, or nullptr if the request
      * wasn't created
      */
    Request* run_add(Thread& owner);
    
    /** \brief Attemts to show the dialog to modify an existing request
      * \param request The request to modify
      */
    void run_edit(Request& request);
    
  private:
    /** \brief Fills the combo model with data from the latest
     * Environment
     */
    void update_combo();

    /** \brief Called when the "add subrequest" button is pressed
     */
    void _on_add();
    
    /** \brief Called when the "remove subrequest" button is pressed
     */
    void _on_remove();

    /** \brief Called when list selection changes 
     * 
     * Ensures the "remove subrequest" button is sensitive only
     * if a subrequest is selected
     */
    void _on_list_selection_changed();

    /** \brief Called when a row is added to the subrequest list
     *
     * Ensures the OK button is sensitive since we are sure there is at
     * least a subrequest
     */
    void _on_row_added(const Gtk::TreePath& path, const Gtk::TreeIter& it);
    
    /** \brief Called when a row is removed from the subrequest list
     *
     * Ensures the OK button is no more sensitive if the last item was removed
     */
    void _on_row_removed(const Gtk::TreePath& path);
    
    /** \brief Called when the combo selection changes 
     * 
     * Ensures the "add subrequest" button is sensitive only
     * if a resource is selected
     */
    void _on_combo_selection_changed();
    
    Glib::RefPtr<Gtk::Builder> _ui;
    Gtk::TreeView* _list;
    Glib::RefPtr<Gtk::ListStore> _list_model;
    Gtk::TreeModelColumnRecord _list_columns;
    Gtk::TreeModelColumn<unsigned int> _list_key_column;
    Gtk::TreeModelColumn<Glib::ustring> _list_resource_column;
    Gtk::TreeModelColumn<unsigned int> _list_duration_column;

    Gtk::Button* _add_button;
    Gtk::Button* _remove_button;
    Gtk::Button* _ok_button;
    Gtk::SpinButton* _instant_spin;
    Gtk::SpinButton* _duration_spin;

    Gtk::ComboBox* _resource_combo;
    Glib::RefPtr<Gtk::ListStore> _combo_model;
    Gtk::TreeModelColumnRecord _combo_columns;
    Gtk::TreeModelColumn<unsigned int> _combo_key_column;
    Gtk::TreeModelColumn<Glib::ustring> _combo_resource_column;
  };

} //~ namespace sgpem


#endif //~ ADD_REQUEST_DIALOG_HH
