/***************************************************************************
 *   Copyright (C) 2009 by Stefan Bühler                                   *
 *   stbuehler@web.de                                                      *
 *                                                                         *
 *   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 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.             *
 ***************************************************************************/
#include "optiondlg.h"
#include <QPushButton>

namespace toruschess {
	
	OptionDlg::OptionDlg(Game *game, QWidget *parent)
	: QDialog(parent), m_game(game) {
		m_curMode = m_game->gameMode();
		m_curAIStrength = m_game->aiStrength();
		setupUi(this);
		
		m_bgMode = new QButtonGroup(this);
		m_bgMode->addButton(rb_mode1, HUMAN_HUMAN);
		m_bgMode->addButton(rb_mode2, HUMAN_COMPUTER);
		m_bgMode->addButton(rb_mode3, COMPUTER_HUMAN);
		m_bgMode->addButton(rb_mode4, COMPUTER_COMPUTER);
		
		dlgReset();
		
		connect(m_game, SIGNAL(changedMode(GameMode)), this, SLOT(gameChangedMode(GameMode)));
		connect(m_game, SIGNAL(changedAIStrength(int)), this, SLOT(gameChangedAIStrength(int)));
		
		connect(this, SIGNAL(accepted()), this, SLOT(dlgAccept()));
		connect(this, SIGNAL(rejected()), this, SLOT(dlgReset()));
		
		connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
		connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
		
		connect(buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), this, SLOT(dlgReset()));
	}
	
	void OptionDlg::gameChangedMode(GameMode mode) {
		if (m_curMode != mode) {
			if (m_bgMode->checkedId() == m_curMode) {
				m_bgMode->button(mode)->setChecked(true);
			}
			m_curMode = mode;
		}
	}
	
	void OptionDlg::gameChangedAIStrength(int aiStrength) {
		if (m_curAIStrength != aiStrength) {
			if (sb_aiStrength->value() == m_curAIStrength) {
				sb_aiStrength->setValue(aiStrength);
			}
			m_curAIStrength = aiStrength;
		}
	}
	
	void OptionDlg::dlgReset() {
		sb_aiStrength->setValue(m_curAIStrength);
		m_bgMode->button(m_curMode)->setChecked(true);
	}
	
	void OptionDlg::dlgAccept() {
		m_curAIStrength = sb_aiStrength->value();
		m_game->setAiStrength(m_curAIStrength);
		m_curMode = (GameMode) m_bgMode->checkedId();
		m_game->setGameMode(m_curMode);
	}
	
}
