//****************************************************************************
//Copyright (C) 2005-2006 Beijing BlueDJ Technology Co.,Ltd. All rights reserved.

//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 (in the file LICENSE.GPL); if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Please visit http://www.bluedj.com for more infomation about us.
//Contact us at ggwizard@gmail.com or darkdong@gmail.com.
//****************************************************************************/

#include "UIHallSetup.h"
#include "Utility.h"
#include "HallUtility.h"
#include "DJGlobal.h"

UIHallSetup::UIHallSetup(QWidget *parent) 
	:QDialog( parent )
{
	setupUi(this);
	
	cbSameLogin->hide();
	
	QValidator *validator = new QIntValidator(0, 65535, this);
	lePort->setValidator( validator );
	
	connect(leServer, SIGNAL(editingFinished()), SLOT(serverFinished()));
	connect(lePort, SIGNAL(editingFinished()), SLOT(portFinished()));
	
    QTranslator translator;
    translator.load( ":/HallRes/lang/LanguageNames");

	QList<const char*>	langs;
	langs << "en" << "zh_CN";// << "zh_TW" << "ko";
	
	comboLang->addItem( tr("AutoDetect"), "auto" );
	for ( int i = 0; i < langs.size(); i++ ) {
		djDebug() << "lang" << langs.at(i);
		comboLang->addItem( translator.translate( "LanguageNames", langs.at(i) ), langs.at(i) );
	}
    
    //get value from settings
    spinMaxSize->setValue( GetSettingMaxImageSize() );
    cbSameLogin->setChecked( GetSettingSameLogin() );
    cbSound->setChecked( GetSettingSound() );
    cbSaveChat->setChecked( GetSettingSaveChat() );
    cbPopupChat->setChecked( GetSettingPopupChat() );
    
	QString locale	= GetSettingLocaleName();
	int index	= comboLang->findData(locale);
	if ( -1 == index ) {
		//not found
		locale == "auto";
		index	= 0;
	}
	comboLang->setCurrentIndex(index);
	
	QString	username,password;
	GetSettingProxyAuth( username,password ); 
	leUsername->setText(username);
	lePassword->setText(password);
	
	comboProxyType->addItem( tr("Socks5 Proxy"), GetSettingProxy(0) );
	comboProxyType->addItem( tr("Socks4 Proxy"), GetSettingProxy(1) );
	comboProxyType->addItem( tr("Http Proxy"), GetSettingProxy(2) );
	
	index	= GetSettingProxyType();
	djDebug() << "GetSettingProxyType" << index;
	if ( -1 == index )
		index	= 0;
	comboProxyType->setCurrentIndex( index );
	on_comboProxyType_activated( index );
	
	gbProxy->setChecked( GetSettingUseProxy() );
}
UIHallSetup::~UIHallSetup()
{
	djDebug() << "UIHallSetup destructor";
}
void UIHallSetup::on_okButton_clicked()
{
	djDebug() << "on_okButton_clicked";
	accept();
	
	SetSettingMaxImageSize( spinMaxSize->value() );
	SetSettingSameLogin( cbSameLogin->isChecked() );
	SetSettingSound( cbSound->isChecked() );
	SetSettingSaveChat( cbSaveChat->isChecked() );
	SetSettingPopupChat( cbPopupChat->isChecked() );
	SetSettingLanguage( comboLang->itemData(comboLang->currentIndex()) );
	SetSettingUseProxy( gbProxy->isChecked() );
	SetSettingProxyType( comboProxyType->currentIndex() );
	for ( int i = 0; i < 3; i++ ) {
		QVariant	data	= comboProxyType->itemData(i);
		SetSettingProxy( i, data );
	}
	SetSettingProxyAuth(leUsername->text(),lePassword->text());
}

void UIHallSetup::on_cancelButton_clicked()
{
	reject();
}
void UIHallSetup::on_comboProxyType_activated( int index )
{
	djDebug() << "on_comboProxyType_activated" << index;
	QList<QVariant>	data	= comboProxyType->itemData(index).toList();
	
	QString	server	= data.takeFirst().toString();
	QString	port	= data.takeFirst().toString();
	
	leServer->setText( server );
	lePort->setText( port );
	
	if ( 0 == index ) {
		leUsername->setEnabled(true);
		lePassword->setEnabled(true);
	}else {
		leUsername->setEnabled(false);
		lePassword->setEnabled(false);
	}
}
void UIHallSetup::serverFinished()
{
	djDebug() << "serverFinished";
	int index	= comboProxyType->currentIndex();
	QList<QVariant> data	= comboProxyType->itemData(index).toList();
	data[0]	= leServer->text();
	comboProxyType->setItemData(index, data);
}
void UIHallSetup::portFinished()
{
	djDebug() << "portFinished";
	int index	= comboProxyType->currentIndex();
	QList<QVariant> data	= comboProxyType->itemData(index).toList();
	data[1]	= lePort->text();
	comboProxyType->setItemData(index, data);
}
