/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2006  by Diether Knof and Borg Enders
 *
 *   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; either version 2 of 
 *   the License, or (at your option) any later version.
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   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., 59 Temple Place, Suite 330, Boston, 
 *   MA  02111-1307  USA
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#include "constants.h"

#ifdef USE_UI_GTKMM

#include "gameplay_actions.h"

#include "ui.h"
#include "translations.h"

#include "../../utils/string.h"

#include <gtkmm/treeview.h>

namespace UI_GTKMM_NS {

  /**
   ** constructor
   **
   ** @param	parent	parent object
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.7.1
   **/
  GameplayActions::GameplayActions(Base* const parent) :
    Base(parent),
    Gtk::TreeView(),
    actions(NULL),
    model(),
    list(NULL)
  {
    this->init();
    this->signal_realize().connect(sigc::mem_fun(*this, &GameplayActions::language_update));

    return ;
  } // GameplayActions::GameplayActions(Base* const parent)

  /**
   ** destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.7.1
   **/
  GameplayActions::~GameplayActions()
  {
    return ;
  } // GameplayActions::~GameplayActions()

  /**
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **/
  void
    GameplayActions::init()
    {
      this->list = Gtk::ListStore::create(this->model);
      this->set_model(this->list);

      this->get_selection()->set_mode(Gtk::SELECTION_NONE);

      this->append_column("Type", this->model.type);
      this->append_column("Player", this->model.player);
      this->get_column_cell_renderer(1)->property_xalign() = 0.5;
      this->append_column("Data", this->model.data);

      for (unsigned c = 0; c < this->get_columns().size(); ++c) {
	Gtk::TreeView::Column* const column = this->get_column(c);
#ifdef WINDOWS
	column->add_attribute(*column->get_first_cell_renderer(),
			      "background", 3);
#endif
	column->set_cell_data_func(*column->get_first_cell_renderer(),
				   sigc::mem_fun(*this, &GameplayActions::set_cell_color));
      }
      this->show_all_children();

      this->update();
      this->language_update();

      return ;
    } // void GameplayActions::init()

  /**
   ** update (rewrite) all information
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.7.1
   **
   ** @todo	mark the winner gray
   ** @todo	if there is no 'player_of' don't show the column
   **/
  void
    GameplayActions::update()
    {
      if (!this->is_realized())
	return ;

      this->list->clear();

      if (this->actions) {
	for (vector< ::GameplayAction*>::const_iterator
	     a = this->actions->begin();
	     a != this->actions->end();
	     ++a) {
	  Gtk::TreeModel::Row row = *this->list->append();
	  row[this->model.type] = ::translation((*a)->type).translation();
	  if ((*a)->player == UINT_MAX)
	    row[this->model.player] = "";
	  else
	    row[this->model.player] = DK::Utils::String::to_string((*a)->player);
	  row[this->model.data] = (*a)->data_translation().translation();
	  row[this->model.action] = *a;

	} // for (a \in this->actions)
      } // if (this->actions)

      return ;
    } // void GameplayActions::update()

  /**
   ** sets the actions
   **
   ** @param	actions	actions to display
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.7.1
   **/
  void
    GameplayActions::set_actions(vector< GameplayAction*> const& actions)
    {
      this->actions = &actions;
      this->update();
      return ;
    } // void GameplayActions::set_actions(vector< GameplayAction*> actions)

  /**
   ** removes the actions
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.7.1
   **/
  void
    GameplayActions::remove_actions()
    {
      this->actions = NULL;
      this->update();
      return ;
    } // void GameplayActions::remove_actions()

  /**
   ** the name of 'player' has changed
   **
   ** @param	player	the player with the changed name
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.4
   **/
  void
    GameplayActions::name_changed(Player const& player)
    {
      if (!this->is_realized())
	return ;

      this->update();

      return ;
    } // void GameplayActions::name_changed(Player player)

  /**
   ** updates all language elements
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.7.1
   **/
  void
    GameplayActions::language_update()
    {
      if (!this->is_realized())
	return ;

      this->get_column(0)->set_title(::translation("Type").translation());
      this->get_column(1)->set_title(::translation("Player").translation());
      this->get_column(2)->set_title(::translation("Data").translation());

      this->update();

      return ;
    } // void GameplayActions::language_update()

  /**
   ** changes the color of the cell at 'iterator'
   **
   ** @param	cell_renderer	cell renderer to change
   ** @param	iterator	row
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **/
  void
    GameplayActions::set_cell_color(Gtk::CellRenderer* cell_renderer,
				    Gtk::TreeModel::iterator const& iterator)
    {
      Gtk::TreeModel::Row const row = *iterator;
      GameplayAction const* const action = row[this->model.action];

      switch (action->type) {
      case GameplayAction::TRICK_FULL:
	dynamic_cast<Gtk::CellRendererText*>(cell_renderer
					    )->property_foreground()
	  = "white";
	dynamic_cast<Gtk::CellRendererText*>(cell_renderer
					    )->property_background()
	  = "darkblue";
	break;

      default:
	dynamic_cast<Gtk::CellRendererText*>(cell_renderer
					    )->property_foreground()
	  = "black";
	dynamic_cast<Gtk::CellRendererText*>(cell_renderer
					    )->property_background()
	  = "white";
	break;
      } // if (action->type == TRICK_FULL)


      return ;
    } // void GameplayActions::set_cell_color(Gtk::CellRenderer* cell_renderer, Gtk::TreeModel::iterator iterator)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
