//
// Copyright (C) 2003	 Grzegorz Jaskiewicz <gj at pointblue.com.pl>
//
// gadupubdir.cpp
//  Gadu-Gadu Public directory contains people data, using it you can search friends
// different criteria
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
//

#include "gadupubdir.h"
#include "gadueditcontact.h"
#include "gaducontactlist.h"
#include "gaduaccount.h"
#include "gaduprotocol.h"

#include <tqwidgetstack.h>
#include <tqlistview.h>
#include <tqptrlist.h>
#include <tqradiobutton.h>
#include <tqspinbox.h>
#include <tqcheckbox.h>

#include <kcombobox.h>
#include <krestrictedline.h>
#include <klineedit.h>
#include <tdelistview.h>
#include <tdelocale.h>

GaduPublicDir::GaduPublicDir( GaduAccount* account, TQWidget* parent, const char* name )
: KDialogBase( parent, name, false, TQString(), User1|User2|User3|Cancel, User2 )
{
	mAccount = account;
	createWidget();
	initConnections();

	show();
}

GaduPublicDir::GaduPublicDir( GaduAccount* account, int searchFor, TQWidget* parent, const char* name )
: KDialogBase( parent, name, false, TQString(), User1|User2|User3|Cancel, User2 )
{
	ResLine rs;

	mAccount = account;
	createWidget();
	initConnections();

	kdDebug( 14100 ) << "search for Uin: " << searchFor << endl;

	mMainWidget->listFound->clear();
	show();

	if ( searchFor == 0 ) {
		return;
	}

	mMainWidget->pubsearch->raiseWidget( 1 );
	mMainWidget->radioByUin->setChecked( true );

	setButtonText( User2, i18n( "Search &More..." ) );
	showButton( User3, true );
	showButton( User1, true );
	enableButton( User3, false );
	enableButton( User2, false );

	// now it is time to switch to Right Page(tm)
	rs.uin = searchFor;
	
	fName	=  fSurname =  fNick = fCity = TQString();
	fUin	= searchFor;
	fGender	= fAgeFrom = fAgeTo = 0;
	fOnlyOnline = false;

	mAccount->pubDirSearch( rs, fAgeFrom, fAgeTo, fOnlyOnline );

}

void
GaduPublicDir::createWidget()
{
	setCaption( i18n( "Gadu-Gadu Public Directory" ) );

	mMainWidget = new GaduPublicDirectory( this );
	setMainWidget( mMainWidget );

	mMainWidget->UIN->setValidChars( "1234567890" );

	setButtonText( User1, i18n( "&New Search" ) );
	setButtonText( User2, i18n( "S&earch" ) );
	setButtonText( User3, i18n( "&Add User..." ) );
	setButtonText( Cancel, i18n( "&Close" ) );

	showButton( User1, false );
	showButton( User3, false );
	enableButton( User2, false );

	mMainWidget->radioByData->setChecked( true );

	mAccount->pubDirSearchClose();

}

void
GaduPublicDir::slotAddContact()
{
	GaduContactsList::ContactLine* cl = new GaduContactsList::ContactLine;
	TQListViewItem* item = mMainWidget->listFound->currentItem();

	cl->ignored	= false;
	cl->firstname	= item->text( 1 );
	cl->uin		= item->text( 5 );
	cl->nickname 	= item->text( 2 );

	cl->surname	= fSurname;

//	GaduEditContact *ed = 
	new GaduEditContact( mAccount, cl, this );
}

void
GaduPublicDir::slotListSelected(  )
{
	TQListViewItem* item = mMainWidget->listFound->currentItem();
	if ( item ) {
		enableButton( User3, true );
	}
	else {
		enableButton( User3, false );
	}
}

void
GaduPublicDir::initConnections()
{
	connect( this, TQ_SIGNAL( user2Clicked() ), TQ_SLOT( slotSearch() ) );
	connect( this, TQ_SIGNAL( user1Clicked() ), TQ_SLOT( slotNewSearch() ) );
	connect( this, TQ_SIGNAL( user3Clicked() ), TQ_SLOT( slotAddContact() ) );

	connect( mAccount, TQ_SIGNAL( pubDirSearchResult( const SearchResult&, unsigned int ) ),
				TQ_SLOT( slotSearchResult( const SearchResult&, unsigned int ) ) );

	connect( mMainWidget->nameS,		TQ_SIGNAL( textChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->surname,		TQ_SIGNAL( textChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->nick,		TQ_SIGNAL( textChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->UIN,		TQ_SIGNAL( textChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->cityS,		TQ_SIGNAL( textChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->gender,		TQ_SIGNAL( activated( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->ageFrom,		TQ_SIGNAL( valueChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->ageTo,		TQ_SIGNAL( valueChanged( const TQString &) ), TQ_SLOT( inputChanged( const TQString & ) ) );
	connect( mMainWidget->radioByData,	TQ_SIGNAL( toggled( bool ) ), TQ_SLOT( inputChanged( bool ) ) );

	connect( mMainWidget->listFound,	TQ_SIGNAL( selectionChanged () ), TQ_SLOT( slotListSelected() ) );

}

void
GaduPublicDir::inputChanged( bool )
{
	inputChanged( TQString() );
}

void
GaduPublicDir::inputChanged( const TQString& )
{
	if ( validateData() == false ) {
		enableButton( User2, false );
	}
	else {
		enableButton( User2, true );
	}
}

void
GaduPublicDir::getData()
{
	fName		= mMainWidget->nameS->text();
	fSurname	= mMainWidget->surname->text();
	fNick		= mMainWidget->nick->text();
	fUin		= mMainWidget->UIN->text().toInt();
	fGender		= mMainWidget->gender->currentItem();
	fOnlyOnline	= mMainWidget->onlyOnline->isChecked();
	fAgeFrom	= mMainWidget->ageFrom->value();
	fAgeTo		= mMainWidget->ageTo->value();
	fCity		= mMainWidget->cityS->text();
}

// return true if not empty
#define CHECK_STRING(A) { if ( !A.isEmpty() ) { return true; } }
#define CHECK_INT(A) { if ( A ) { return true; } }

bool
GaduPublicDir::validateData()
{
	getData();

	if ( mMainWidget->radioByData->isChecked() ) {
		CHECK_STRING( fCity );
		CHECK_STRING( fName );
		CHECK_STRING( fSurname );
		CHECK_STRING( fNick );
		CHECK_INT( fGender );
		CHECK_INT( fAgeFrom );
		CHECK_INT( fAgeTo );
	}
	else {
		fSurname = TQString();
		CHECK_INT( fUin );
	}
	return false;
}

// Move to GaduProtocol someday
TQPixmap
GaduPublicDir::iconForStatus( uint status )
{
	TQPixmap n;

	if ( GaduProtocol::protocol() ) {
		return GaduProtocol::protocol()->convertStatus( status ).protocolIcon();
	}
	return n;
}

void
GaduPublicDir::slotSearchResult( const SearchResult& result, unsigned int )
{
	TQListView* list = mMainWidget->listFound;

	kdDebug(14100) << "searchResults(" << result.count() <<")" << endl;

	TQListViewItem* sl;

	SearchResult::const_iterator r;

	for ( r = result.begin(); r != result.end() ; ++r ){
		kdDebug(14100) << "adding" << (*r).uin << endl;
		sl= new TQListViewItem(
					list, TQString::fromAscii(""),
					(*r).firstname,
					(*r).nickname,
					(*r).age,
					(*r).city,
					TQString::number( (*r).uin ).ascii()
						);
		sl->setPixmap( 0, iconForStatus( (*r).status ) );
	}

	// if not found anything, obviously we don't want to search for more
	// if we are looking just for one UIN, don't allow search more - it is pointless

	if ( result.count() && fUin==0 ) {
		enableButton( User2, true );
	}

	enableButton( User1, true );
	enableButton( User3, false );
	mMainWidget->pubsearch->setDisabled( false );

}

void
GaduPublicDir::slotNewSearch()
{
	mMainWidget->pubsearch->raiseWidget( 0 );

	setButtonText( User2, i18n( "S&earch" ) );

	showButton( User1, false );
	showButton( User3, false );
	enableButton( User2, false );
 	inputChanged( TQString() );
 	mAccount->pubDirSearchClose();
}

void
GaduPublicDir::slotSearch()
{

	mMainWidget->listFound->clear();
	TQString empty;

	// search more, or search ?
	if ( mMainWidget->pubsearch->id( mMainWidget->pubsearch->visibleWidget() ) == 0 ) {
		kdDebug(14100) << "start search... " << endl;
		getData();

		// validate data
		if ( validateData() == false ) {
			return;
		}

		// go on
		mMainWidget->pubsearch->raiseWidget( 1 );

	}
	else{
		kdDebug(14100) << "search more... " << endl;
		// Search for more
	}
	mMainWidget->pubsearch->setDisabled( true );
	setButtonText( User2, i18n( "Search &More..." ) );
	showButton( User3, true );
	showButton( User1, true );
	enableButton( User3, false );
	enableButton( User2, false );

	ResLine rs;
	rs.firstname	= fName;
	rs.surname	= fSurname;
	rs.nickname	= fNick;
	rs.uin		= fUin;
	rs.city		= fCity;
	
	if ( fGender == 1 ) {
		rs.gender =  GG_PUBDIR50_GENDER_MALE;
	}
	if ( fGender == 2 ) {
		rs.gender = GG_PUBDIR50_GENDER_FEMALE;
	}
	
	
	if ( mMainWidget->radioByData->isChecked() ) {
		mAccount->pubDirSearch( rs, fAgeFrom, fAgeTo, fOnlyOnline );
	}
	else {
		mAccount->pubDirSearch( rs, 0, 0, fOnlyOnline );
	}
}

#include "gadupubdir.moc"
