/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2005  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 "reservation.h"
#include "table.h"
#include "name.h"
#include "ui.h"
#include "main_window.h"

#include "widgets/stock_button.h"
#include "../../party/rule.h"
#include "../../game/game.h"
#include "../../basistypes.h"

#include <gtkmm/stock.h>
#include <gtkmm/notebook.h>
#include <gtkmm/alignment.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/table.h>
#include <gtkmm/main.h>
namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.0
   **
   **/
  Reservation::Reservation(Table* const table, Player const* const player) :
    HTIN(table, player),
  Gtk::StickyDialog("Reservation", *(parent->ui->main_window), false, true),
  announce_button(NULL),
  notebook(NULL),
  swines_button(NULL),
  hyperswines_button(NULL),
  gametype_buttons(),
  marriage_selector_buttons()
  {
    this->signal_realize().connect(SigC::slot(*this,
					      &Reservation::init));

    return ;
  } // Reservation::Reservation(Table* const table, Player const& player)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Reservation::~Reservation()
  {
    return ;
  } // Reservation::~Reservation()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    Reservation::init()
    {
      this->set_icon(this->ui->main_window->icon);
      if (this->player->game().humanno() <= 1)
	this->ui->translations->add(*this,
				      ::translator("Reservation"));
      else
	this->ui->translations->add(*this,
				      ::translator("Reservation (%sname%)",
						   this->player->name()));

      this->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);

      this->announce_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::OK,
					   "reservation announce"));
      this->ui->translations->add(*(this->announce_button),
				    ::translator("reservation announce"));
      this->add_action_widget(*(this->announce_button), Gtk::RESPONSE_ACCEPT);
      this->notebook = Gtk::manage(new Gtk::Notebook());
      this->notebook->set_tab_pos(Gtk::POS_LEFT);
      this->swines_button = Gtk::manage(new Gtk::CheckButton("swines"));
      this->ui->translations->add(*(this->swines_button),
				    ::translator("swines"));
      this->hyperswines_button = Gtk::manage(new Gtk::CheckButton("hyperswines"));
      this->ui->translations->add(*(this->hyperswines_button),
				    ::translator("hyperswines"));

      this->announce_button->grab_default();

      this->get_vbox()->add(*(this->notebook));

      { // create the buttons
	Gtk::RadioButton::Group gametype_group;
	for (int g = GAMETYPE::FIRST; g <= GAMETYPE::LAST; g++) {
	  this->gametype_buttons.push_back(Gtk::manage(new Gtk::RadioButton(gametype_group, "type")));
	  this->gametype_buttons.back()->set_data("gametype", new GameType(GameType(g)));
	  this->ui->translations->add(*(this->gametype_buttons.back()),
					::translator(::name(GameType(g))));
	  this->gametype_buttons.back()->signal_toggled().connect(SigC::bind<int const>(SigC::slot(*this, &Reservation::gametype_changed), g));
	} // for (g \in GAMETYPE)

	Gtk::RadioButton::Group marriage_selector_group;
	for (int m = MARRIAGE_SELECTOR::FIRST; m <= MARRIAGE_SELECTOR::LAST; m++) {
	  this->marriage_selector_buttons.push_back(Gtk::manage(new Gtk::RadioButton(marriage_selector_group, "selector")));
	  this->marriage_selector_buttons.back()->set_data("selector", new MarriageSelector(MarriageSelector(m)));
	  this->ui->translations->add(*(this->marriage_selector_buttons.back()),
					::translator(::name(MarriageSelector(m))));
	} // for (g \in GAMETYPE)
      } // create the buttons

      { // set the pages of the notebook
	Gtk::Alignment* alignment = NULL;
	{ // General
	  alignment = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER,
						     Gtk::ALIGN_CENTER,
						     0, 0));
	  alignment->set_border_width(2 ex);
	  Gtk::Label* label = Gtk::manage(new Gtk::Label("general"));
	  this->ui->translations->add(*label,
					::translator("general"));
	  this->notebook->append_page(*alignment, *label);

	  Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox(false, 1 ex));
	  alignment->add(*hbox);

	  Gtk::VBox* vbox = NULL;
	  { // the swines
	    vbox = Gtk::manage(new Gtk::VBox(false, 10));
	    hbox->add(*vbox);

	    vbox->add(*(this->swines_button));
	    vbox->add(*(this->hyperswines_button));
	  } // the swines
	  { // gametypes
	    vbox = Gtk::manage(new Gtk::VBox(false, 10));
	    hbox->add(*vbox);

	    vbox->add(*(this->gametype_buttons[GAMETYPE::NORMAL]));
	    vbox->add(*(this->gametype_buttons[GAMETYPE::FIVE_NINES]));
	    vbox->add(*(this->gametype_buttons[GAMETYPE::SIX_KINGS]));
	    vbox->add(*(this->gametype_buttons[GAMETYPE::FOX_HIGHEST_TRUMP]));
	    vbox->add(*(this->gametype_buttons[GAMETYPE::POVERTY]));
	    vbox->add(*(this->gametype_buttons[GAMETYPE::MARRIAGE]));
	  } // gametypes
	  { // marriage selectors
	    vbox = Gtk::manage(new Gtk::VBox(false, 10));
	    hbox->add(*vbox);

	    vbox->add(*(this->marriage_selector_buttons[MARRIAGE_SELECTOR::FIRST_FOREIGN]));
	    vbox->add(*(this->marriage_selector_buttons[MARRIAGE_SELECTOR::FIRST_TRUMP]));
	    vbox->add(*(this->marriage_selector_buttons[MARRIAGE_SELECTOR::FIRST_COLOR]));
	    vbox->add(*(this->marriage_selector_buttons[MARRIAGE_SELECTOR::FIRST_CLUB]));
	    vbox->add(*(this->marriage_selector_buttons[MARRIAGE_SELECTOR::FIRST_SPADE]));
	    vbox->add(*(this->marriage_selector_buttons[MARRIAGE_SELECTOR::FIRST_HEART]));
	  } // marriage selectors
	} // General
	{ // Solo
	  alignment = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER,
						     Gtk::ALIGN_CENTER,
						     0, 0));
	  alignment->set_border_width(2 ex);
	  Gtk::Label* label = Gtk::manage(new Gtk::Label("solo"));
	  this->ui->translations->add(*label,
					::translator("solo"));
	  this->notebook->append_page(*alignment, *label);

	  Gtk::Table* table = Gtk::manage(new Gtk::Table(4, 3));
	  table->set_row_spacings(10);
	  table->set_col_spacings(10);
	  alignment->add(*table);

	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_CLUB]),
			0, 1, 0, 1);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_SPADE]),
			0, 1, 1, 2);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_HEART]),
			0, 1, 2, 3);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_DIAMOND]),
			0, 1, 3, 4);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_MEATLESS]),
			1, 2, 0, 1);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_JACK]),
			1, 2, 1, 2);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_QUEEN]),
			1, 2, 2, 3);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_KING]),
			1, 2, 3, 4);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_QUEEN_JACK]),
			2, 3, 0, 1);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_KING_JACK]),
			2, 3, 1, 2);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_KING_QUEEN]),
			2, 3, 2, 3);
	  table->attach(*(this->gametype_buttons[GAMETYPE::SOLO_KOEHLER]),
			2, 3, 3, 4);
	} // Solo
      } // set the pages of the notebook

      // signals
      this->announce_button->signal_clicked().connect(SigC::slot(*this,
								 &Gtk::Widget::hide)
						     );

      this->show_all_children();

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

  /**
   **
   ** gets a reservation
   **
   ** @param	-
   **
   ** @return	reservation
   **
   ** @version	0.6.0
   **
   ** @author	Diether Knof
   **
   **/
  ::Reservation
    Reservation::get()
    {
      this->realize();

      this->notebook->set_current_page(0);

      this->sensitivity_update();
      this->set_default();

      this->present();

      while (!this->ui->thrower
	     && this->is_visible())
	Gtk::Main::iteration();

      this->ui->game().set_type(GAMETYPE::NORMAL);

      ::Reservation reservation;
      { // set the reservation
	for (vector<Gtk::RadioButton*>::iterator widget
	     = this->gametype_buttons.begin();
	     widget != this->gametype_buttons.end();
	     widget++)
	  if ((*widget)->get_active())
	    reservation.game_type
	      = *(static_cast<GameType*>((*widget)->get_data("gametype")));

	if (reservation.game_type == GAMETYPE::MARRIAGE) {
	  for (vector<Gtk::RadioButton*>::iterator widget
	       = this->marriage_selector_buttons.begin();
	       widget != this->marriage_selector_buttons.end();
	       widget++)
	    if ((*widget)->get_active())
	      reservation.marriage_selector
		= *(static_cast<MarriageSelector*>((*widget)->get_data("selector")));
	} // if (reservation.game_type == GAMETYPE::MARRIAGE)
	reservation.swines = this->swines_button->get_active();
	reservation.hyperswines = this->hyperswines_button->get_active();
      } // set the reservation

      this->hide();

      return reservation;
    } // ::Reservation Reservation::get()

  /**
   **
   ** set to default values
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.0
   **
   ** @author	Diether Knof
   **
   **/
  void
    Reservation::set_default()
    {
      ::Reservation const reservation = this->player->reservation_get_default();

      this->sensitivity_update();

      this->swines_button->set_active(reservation.swines);
      this->hyperswines_button->set_active(reservation.hyperswines);

      for (vector<Gtk::RadioButton*>::const_iterator
	   g = this->gametype_buttons.begin();
	   g != this->gametype_buttons.end();
	   g++)
	if (*(static_cast<GameType*>((*g)->get_data("gametype")))
	    == reservation.game_type) {
	  (*g)->set_active();
	  break;
	}

      for (vector<Gtk::RadioButton*>::const_iterator
	   s = this->marriage_selector_buttons.begin();
	   s != this->marriage_selector_buttons.end();
	   s++)
	if (*(static_cast<MarriageSelector*>((*s)->get_data("selector")))
	    == reservation.marriage_selector) {
	  (*s)->set_active();
	  break;
	}

      this->name().draw(true);
      this->table().name(this->player->game().startplayer()).draw(true);

      return ;
    } // void Reservation::set_default()

  /**
   **
   ** update the sensitivity
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Reservation::sensitivity_update()
    {
      if (!this->is_realized())
	return ;

      if (::game_status != GAMESTATUS::GAME_RESERVATION)
	return ;

      ::Rule const& rule = this->player->game().rule();
      //::Player const& player = this->player->game().player_current();
      ::Player const& player = *(this->player);
      ::Hand const& hand = player.hand();

      if (rule(Rule::SWINES)
	  && !rule(Rule::SWINE_ONLY_SECOND))
	this->swines_button->show();
      else
	this->swines_button->hide();
      this->swines_button->set_sensitive(hand.numberoftrumpaces()
					 == rule(Rule::NUMBER_OF_SAME_CARDS));

      if (rule(Rule::HYPERSWINES))
	this->hyperswines_button->show();
      else
	this->hyperswines_button->hide();
      this->hyperswines_button->set_sensitive(hand.numberoftrumpnines()
					      == rule(Rule::NUMBER_OF_SAME_CARDS));

      for (vector<Gtk::RadioButton*>::iterator widget
	   = this->gametype_buttons.begin();
	   widget != this->gametype_buttons.end();
	   widget++)
	if (rule(*(static_cast<GameType*>((*widget)->get_data("gametype")))))
	  (*widget)->show();
	else
	  (*widget)->hide();

      if (rule(Rule::SOLO))
	this->notebook->get_nth_page(1)->show();
      else
	this->notebook->get_nth_page(1)->hide();

      for (vector<Gtk::RadioButton*>::iterator widget
	   = this->marriage_selector_buttons.begin();
	   widget != this->marriage_selector_buttons.end();
	   widget++)
	if (rule(*(static_cast<MarriageSelector*>((*widget)->get_data("selector")))))
	  (*widget)->show();
	else
	  (*widget)->hide();

      this->gametype_buttons[GAMETYPE::FIVE_NINES]->set_sensitive((hand.numberofnines() >= Rule::MIN_NUMBER_OF_THROWING_NINES)
								  && rule(GAMETYPE::FIVE_NINES));
      this->gametype_buttons[GAMETYPE::SIX_KINGS]->set_sensitive((hand.numberofkings() >= Rule::MIN_NUMBER_OF_THROWING_KINGS)
								 && rule(GAMETYPE::SIX_KINGS));
      this->gametype_buttons[GAMETYPE::POVERTY]->set_sensitive((hand.numberoftrumps() <= Rule::MAX_NUMBER_OF_POVERTY_TRUMPS)
							       && rule(GAMETYPE::POVERTY));
      { // look, whether there is a card that is higher than the fox
	unsigned c;
	for (c = 0; c < player.hand().cardsnumber(); c++)
	  if (Card(Card::DIAMOND, Card::ACE).less(player.hand().card(c)))
	    break;
	this->gametype_buttons[GAMETYPE::FOX_HIGHEST_TRUMP]->set_sensitive((c == player.hand().cardsnumber())
									   && rule(GAMETYPE::POVERTY));
      } // look, whether there is a card that is higher than the fox
      this->gametype_buttons[GAMETYPE::MARRIAGE]->set_sensitive((hand.numberofclubqueens() == rule(Rule::NUMBER_OF_SAME_CARDS)));

      for (vector<Gtk::RadioButton*>::iterator widget
	   = this->marriage_selector_buttons.begin();
	   widget != this->marriage_selector_buttons.end();
	   widget++)
	(*widget)->set_sensitive(this->gametype_buttons[GAMETYPE::MARRIAGE]->sensitive());

      //if (rule(Rule::SOLO))

      return ;
    } // void Reservation::sensitivity_update()

  /**
   **
   ** the gametype has changed
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.0
   **
   ** @author	Diether Knof
   **
   **/
  void
    Reservation::gametype_changed(int const gametype)
    {
      if (gametype == GAMETYPE::MARRIAGE) {
	bool const selected = this->gametype_buttons[gametype]->get_active();
	for (vector<Gtk::RadioButton*>::iterator widget
	     = this->marriage_selector_buttons.begin();
	     widget != this->marriage_selector_buttons.end();
	     widget++)
	  (*widget)->set_sensitive(selected);
      } // if (gametype == GAMETYPE::MARRIAGE)

      // '::party.game()' here, because the game has to be changed,
      // 'this->player->game()' is a const game
      GameType const old_gametype = this->player->game().type();
      this->ui->game().set_type(static_cast<GameType>(gametype));
      this->ui->game().players_hands_sort();

      this->name().draw(true);
      this->table().name(this->player->game().startplayer()).draw(true);

      // update the swines
      if ((old_gametype != gametype)
	  && ( (old_gametype == GAMETYPE::SOLO_CLUB)
	       || (old_gametype == GAMETYPE::SOLO_SPADE)
	       || (old_gametype == GAMETYPE::SOLO_HEART)
	       || (old_gametype == GAMETYPE::SOLO_DIAMOND)
	       || (gametype == GAMETYPE::SOLO_CLUB)
	       || (gametype == GAMETYPE::SOLO_SPADE)
	       || (gametype == GAMETYPE::SOLO_HEART)
	       || (gametype == GAMETYPE::SOLO_DIAMOND)
	     )) {
	::Rule const& rule = this->player->game().rule();
	::Hand const& hand = this->player->hand();

	this->swines_button->set_sensitive(hand.numberoftrumpaces()
					   == rule(Rule::NUMBER_OF_SAME_CARDS));
	this->swines_button->set_active(this->swines_button->is_sensitive());
	this->hyperswines_button->set_sensitive(hand.numberoftrumpnines()
						== rule(Rule::NUMBER_OF_SAME_CARDS));
	this->hyperswines_button->set_active(this->hyperswines_button->is_sensitive());
      } // if (gametype has changed to/from color solo)

      return ;
    } // void Reservation::gametype_changed(int const gametype)

  /**
   **
   ** nothing
   **
   ** @param	update	whether to update the area
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.0
   **
   **/
  void
    Reservation::draw(bool const update)
    {
      return ;
    } // void Reservation::draw(bool const update = true)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
