/***************************************************************************
 *   Copyright (C) 2005 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   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 "evasearchwindow.h"
#include "evaresource.h"
#include <qlabel.h>
#include <qwidgetstack.h>
#include <qpushbutton.h>
#include <qradiobutton.h>
#include <qlineedit.h>
#include <qtable.h>
#include <qcheckbox.h>
#include <qtoolbutton.h>
#include <qgroupbox.h>
#include <qbuttongroup.h>
#include <qimage.h>
#include <qtextcodec.h>
#include <kmessagebox.h>
#include <klocale.h>

EvaSearchWindow::EvaSearchWindow(EvaImageResource *res, const int onlineUsers, QWidget* parent, 
		const char* name, WFlags fl)
	: EvaSearchUIBase(parent,name, fl),
	idMin(0),
	idMax(3),
	searchFinished(false),
	showAllUsers(false),
	p2CurrentPage(0),
	currentPage(0),
	selectedP0Id(1)
{
	images = res;
	rbP0Customer->setChecked(true);
	lblOnlineNum->setText(QString::number(onlineUsers));
	QHeader *vheader = tblP2Users->verticalHeader();
	vheader->hide();
	tblP2Users->setLeftMargin(0);
	lblLogo->setPixmap(*images->getIcon("SEARCH_LOGO"));
	QObject::connect(tblP2Users, SIGNAL(clicked(int,int,int,const QPoint&)), this, SLOT(slotUserSelected(int)));
	QObject::connect( pbCancel, SIGNAL( clicked() ), this, SLOT( close() ) );
	QObject::connect( pbPrev, SIGNAL( clicked() ), this, SLOT( slotPrevClicked() ) );
	QObject::connect( pbNext, SIGNAL( clicked() ), this, SLOT( slotNextClicked() ) );
	QObject::connect( pbP2All, SIGNAL( clicked() ), this, SLOT( slotPbP2AllClicked() ) );
	QObject::connect( pbP2Prev, SIGNAL( clicked() ), this, SLOT( slotPbP2PreClicked() ) );
	QObject::connect( pbP2Next, SIGNAL( clicked() ), this, SLOT( slotPbP2NextClicked() ) );
	QObject::connect( pbP2Details, SIGNAL( clicked() ), this, SLOT( slotPbP2DetailsClicked() ) );
	QObject::connect( pbP3P0Send, SIGNAL( clicked() ), this, SLOT( slotPbP3P0SendClicked() ) );
	QObject::connect( bgP0, SIGNAL( clicked(int) ), this, SLOT( slotBgP0Clicked(int) ) );
	resetWidgets( );
}

EvaSearchWindow::~EvaSearchWindow()
{
}

void EvaSearchWindow::slotBgP0Clicked(int chbId)
{
	selectedP0Id = chbId;
}

void EvaSearchWindow::slotPbP3P0SendClicked()
{
	pbPrev->setEnabled(false);
	pbNext->setEnabled(false);
	gbP3P1Tip->setTitle(i18n("Add Buddy"));
	lblP3P1Tip->setText(i18n("Sending authorization request, please wait..."));
	wsP3Add->raiseWidget(1);
	emit requestAddAuthBuddy(selectedUser.getQQ(), leP3P0Msg->text());
}

void EvaSearchWindow::slotPbP2DetailsClicked()
{
	if(selectedUser.getQQ()==-1) return;
	emit requestUserInfo(selectedUser.getQQ());
}

void EvaSearchWindow::slotPbP2NextClicked()
{    
	if((uint)p2CurrentPage == userPages.size()-1){
		if(!searchFinished){	
			showAllUsers = false;
			pbP2Prev->setEnabled(false);
			pbP2Next->setEnabled(false);
		
			QString pageNo = QString::number(p2CurrentPage++);
			if(selectedP0Id == 1){
				emit requestSearchUsers(false, pageNo, id, nick, email, cbP1Match->isChecked());
			}else{			
				emit requestSearchUsers(true, pageNo, "", "", "", false);
			}
		}
	return;
	}
	showAllUsers = false;
	p2CurrentPage++;
	displayUsers( );
}

void EvaSearchWindow::slotPbP2PreClicked()
{
	if(p2CurrentPage <= 0 ) return;
	showAllUsers = false;
	p2CurrentPage--;	
	displayUsers( );
}

void EvaSearchWindow::slotPbP2AllClicked()
{
	showAllUsers = true;
	displayUsers( );
}

void EvaSearchWindow::slotNextClicked()
{
	if(currentPage>=idMax)
		close();
	
	processCurrentPage();  
	resetWidgets();
}

void EvaSearchWindow::slotPrevClicked()
{
	if(currentPage<=idMin) return;
	currentPage = actionList.pop();
	resetWidgets();
}

void EvaSearchWindow::resetWidgets( )
{
	pbPrev->setEnabled((currentPage==idMin)?false:true);
		
	if(currentPage<idMax){
		pbNext->setText(i18n("&Next"));
		pbNext->setEnabled(true);
		pbCancel->setEnabled(true);
	}else{
		pbNext->setText(i18n("&Finish"));
		}
		
	wsMain->raiseWidget(currentPage);
}

void EvaSearchWindow::processCurrentPage( )
{
	switch(currentPage){
	case 0:
		processPage0();
		break;
	case 1:
		processPage1();
		break;
	case 2:
		processPage2();
		break;
	case 3:
		processPage3();
		break;
	}
}

void EvaSearchWindow::processPage0( )
{
	if(selectedP0Id == -1){
		KMessageBox::messageBox((QWidget *) 0,KMessageBox::Information,
					i18n("Please select one of the following options."),
					i18n("Eva - Search/Add"));
		return;
	}
	
	if(selectedP0Id == 2){
		KMessageBox::messageBox((QWidget *) 0,KMessageBox::Information,
					i18n("Qun are not supported at the moment."),
					i18n("Eva - Search/Add"));
		return;
	}
	
	actionList.push(currentPage);
	userPages.clear();
	
	if(selectedP0Id == 0 ){
		pbPrev->setEnabled(false);
		pbNext->setEnabled(false);
		emit requestSearchUsers(true, "0", "", "", "", false);
		return;
	}
	
	if(selectedP0Id == 1){
		currentPage = 1;
		return;
	}
}

void EvaSearchWindow::processPage1( )
{
	id = leP1QQ->text().stripWhiteSpace();
	nick = leP1Nick->text();
	email = leP1Email->text().stripWhiteSpace();
	
	if(id.isEmpty() && nick.isEmpty() && email.isEmpty()){
		KMessageBox::messageBox((QWidget *) 0,KMessageBox::Information,
                              i18n("Search conditions can not be empty."),
                              i18n("Eva - Search/Add"));
		leP1QQ->setFocus();
		return;
	}
	bool ok;
	id.toInt(&ok);
	if(!ok && !id.isEmpty()){
		KMessageBox::messageBox((QWidget *) 0,KMessageBox::Information,
                              i18n("QQ number is incorrect."),
                              i18n("Eva - Search/Add"));
		leP1QQ->setFocus();
		return;
	}
	
	actionList.push(currentPage);
	currentPage=2;
	pbPrev->setEnabled(false);
	pbNext->setEnabled(false);
	emit requestSearchUsers(false, "0", id, nick, email, cbP1Match->isChecked());
}

void EvaSearchWindow::processPage2( )
{
	if(selectedUser.getQQ()==-1){
		KMessageBox::messageBox((QWidget *) 0,KMessageBox::Information,
                              i18n("Please select one user."),
                              i18n("Eva - Search/Add"));
	 	return;
	}
	
	actionList.push(currentPage);
	currentPage=3;
	gbP3P1Tip->setTitle(i18n("Add Buddy"));
	lblP3P1Tip->setText(i18n("adding friend, please wait..."));
	QTextCodec *codec = QTextCodec::codecForName("GBK");
        lblP3NickTip->setText(codec->toUnicode(selectedUser.getNick().c_str()));
	lblP3Tip->setPixmap(*images->getFace(images->getFaceFileIndex((uchar)(selectedUser.getFace())), true));
	pbPrev->setEnabled(false);
	pbNext->setEnabled(false);
	wsP3Add->raiseWidget(1);
	
	emit requestAddBuddy(selectedUser.getQQ());
}

void EvaSearchWindow::processPage3( )
{
}


void EvaSearchWindow::slotSearchUsersReady(const bool finished, const std::list<OnlineUser> list)
{
	std::map<int, std::list<OnlineUser> >::iterator iter = userPages.find(p2CurrentPage);
	if( iter != userPages.end()) return ;
	
	userPages[p2CurrentPage] = list;	
	searchFinished = finished;
	displayUsers(); 
}

void EvaSearchWindow::displayUsers( )
{
	for(int row=0; row<tblP2Users->numRows(); row++){
		for(int col=0; col<tblP2Users->numCols(); col++){
			tblP2Users->clearCell(row, col);
		}
	}
	tblP2Users->setNumRows(0);
	int start=0, end=1;
	
	if(showAllUsers){
		end = userPages.size();
	}else{
		start = p2CurrentPage;
		end = start+1;
	}
	std::list<OnlineUser>::iterator iter;
	QTextCodec *codec = QTextCodec::codecForName("GBK");
	for(int i=start; i<end; i++){
		for(iter = userPages[i].begin(); iter!=userPages[i].end(); ++iter){
			tblP2Users->setNumRows(tblP2Users->numRows()+1);
			QImage img = images->getFace(images->getFaceFileIndex((uchar)(iter->getFace())), true)->convertToImage();
			tblP2Users->setPixmap(tblP2Users->numRows()-1, 0, QPixmap(img.smoothScale(16,16)));
			tblP2Users->setText(tblP2Users->numRows()-1, 0, QString::number(iter->getQQ()));
			tblP2Users->setText(tblP2Users->numRows()-1, 1,codec->toUnicode(iter->getNick().c_str()));
			tblP2Users->setText(tblP2Users->numRows()-1, 2,codec->toUnicode(iter->getProvince().c_str()));
		}
	}

	pbP2Prev->setEnabled(p2CurrentPage==0?false:true);
	pbP2Next->setEnabled(searchFinished?false:true);
	currentPage = 2;
	if(!showAllUsers)
		lblP2Tip->setText(QString(i18n("%1/%2 pages")).arg(p2CurrentPage+1).arg(userPages.size()));
	else
		lblP2Tip->setText(QString(i18n("All %1 pages are displayed")).arg((userPages.size())));
	resetWidgets( );
}

void EvaSearchWindow::slotUserSelected( int row)
{
	std::list<OnlineUser>::iterator iter = userPages[p2CurrentPage].begin();
	for(int i=0; i<row; i++)
		iter++;
	selectedUser = *iter;
}

void EvaSearchWindow::slotAddBuddyReady()
{
	gbP3P1Tip->setTitle(i18n("Buddy Added"));
	lblP3P1Tip->setText(i18n("The user you selected has been added into your \"Buddy\", click \"Prev\" to add more, or \"Finish\" to close this dialog."));
	wsP3Add->raiseWidget(1);
	pbNext->setEnabled(true);
	pbCancel->setEnabled(false);
	pbPrev->setEnabled(true);
}

void EvaSearchWindow::slotAddBuddyRejected()
{
	gbP3P1Tip->setTitle(i18n("Adding Failed"));
	lblP3P1Tip->setText(i18n("The user you selected refuses your request, click \"Prev\" to add more, or \"Finish\" to close this dialog."));
	wsP3Add->raiseWidget(1);
	pbNext->setEnabled(true);
	pbCancel->setEnabled(false);
	pbPrev->setEnabled(true);
}

void EvaSearchWindow::slotBuddyNeedAuth()
{
	wsP3Add->raiseWidget(0);
	pbCancel->setEnabled(true);
	pbPrev->setEnabled(true);
}

void EvaSearchWindow::slotAddBuddySentToServer( bool ok)
{			
	
	if(!ok) {
		gbP3P1Tip->setTitle(i18n("Sending Failed"));
		lblP3P1Tip->setText(i18n("Authorization request sending failed, click \"Prev\" to add more, or click \"Finish\" to close this dialog."));	
	} else {
		gbP3P1Tip->setTitle(i18n("Sent Successfully"));
		lblP3P1Tip->setText(i18n("Authorization request sent successfully, click \"Prev\" to add more, or click \"Finish\" to close this dialog."));	
	}
	pbPrev->setEnabled(true);	
	pbNext->setEnabled(true);
	pbCancel->setEnabled(false);
}


