/***************************************************************************
 *   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 "evaqunlist.h"

 


TmpQun::TmpQun( )
	: qunID(0), parentID(0)
{
}

TmpQun::TmpQun( const int pid )
	: qunID(0), parentID(pid)
{
}

TmpQun &TmpQun::operator=(const TmpQun &rhs)
{
	qunID = rhs.getQunID();
	parentID = rhs.getParentID();
	name = rhs.getName();
	memberList = rhs.getMemberQQList();
	return *this;
}

void TmpQun::addMember( const int qqNum )
{
	memberList.push_back(qqNum);
}

const bool TmpQun::removeMember( const int qqNum )
{
	std::list<int>::iterator iter;
	for(iter=memberList.begin(); iter!=memberList.end(); ++iter){
		if( *iter == qqNum){
			memberList.erase(iter);
			return true;
		}
	}
	return false;
}




/*************************************************************************************************************************************/


Qun::Qun( const int id )
	: qunID(id), cardName(""), cardGender(0), cardPhone(""), cardEmail(""), cardMemo(""),
	msgType(Notify)
{
}

Qun &Qun::operator=(const Qun &rhs)
{
	qunID = rhs.getQunID();
	info = rhs.getDetails();
	numberList = rhs.getQQList();
	memberList = rhs.getMembers();
	tmpQunList = rhs.getTmpQunList();
	cardName = rhs.getCardName();
	cardGender = rhs.getCardGender();
	cardPhone = rhs.getCardPhone();
	cardEmail = rhs.getCardEmail();
	cardMemo = rhs.getCardMemo();
	
	return *this;
}

void Qun::setMembers(const std::list<FriendItem> &l)
{
	std::list<FriendItem> list = l;
	std::list<FriendItem>::iterator iter;
	for(iter = list.begin(); iter!= list.end(); ++iter){
		std::list<FriendItem>::iterator iter1;
		for(iter1 = memberList.begin(); iter1!= memberList.end(); ++iter1){
			if(iter1->getQQ() == iter->getQQ()){
				memberList.erase(iter1);
				break;
			}
		}
	}

	std::map<int, QunMember>::iterator it;
	
	for(iter = list.begin(); iter!= list.end(); ++iter){
		it = memberArgs.find(iter->getQQ());
		if(it != memberArgs.end()){
			iter->setQunGroupIndex(it->second.groupIndex);
			iter->setQunAdminValue(it->second.admin);
		}
		memberList.push_back(*iter);
	}
}

void Qun::updateOnlineMembers(const std::list<int> &list)
{
	std::list<int> onlist = list;
	std::list<FriendItem>::iterator iter;
	for(iter = memberList.begin(); iter!= memberList.end(); ++iter){
		iter->setOnline(false);
		std::list<int>::iterator iter1;
		for(iter1 = onlist.begin(); iter1 != onlist.end(); ++iter1){
			if(iter->getQQ() == *iter1)
				iter->setOnline(true);
		}
	}
	//numberList = list;
}

const FriendItem *Qun::getMemberDetails( const int qqNum ) 
{
	std::list<FriendItem>::iterator iter;
	for(iter = memberList.begin(); iter!= memberList.end(); ++iter){
		if(iter->getQQ() == qqNum)
			return &(*iter);
	}
	return NULL;
}

void Qun::setMember( const FriendItem & fi )
{
	FriendItem item = fi;
	std::list<FriendItem>::iterator iter;
	for(iter = memberList.begin(); iter!= memberList.end(); ++iter){
		if(iter->getQQ() == item.getQQ()){
			memberList.erase(iter);
			break;
		}
	}
	
	std::map<int, QunMember>::iterator it = memberArgs.find(item.getQQ());
	if(it != memberArgs.end()){
		item.setQunGroupIndex(it->second.groupIndex);
		item.setQunAdminValue(it->second.admin);
	}
	memberList.push_back(item);
}

void Qun::addTmpQun( const TmpQun & q )
{
	tmpQunList.push_back(q);
}

void Qun::setMemberArgs(const std::map<int, QunMember> &list)
{
	memberArgs = list;
	numberList.clear();
	memberList.clear();
	
// 	std::map<int, QunMember>::iterator it;
// 	std::list<FriendItem>::iterator iter;
// 	for(iter = memberList.begin(); iter!= memberList.end(); ++iter){
// 		it = memberArgs.find(iter->getQQ());
// 		if(it != memberArgs.end()){
// 			iter->setQunGroupIndex(it->second.groupIndex);
// 			iter->setQunAdminValue(it->second.admin);
// 		}
// 	}
}

const  bool Qun::isAdmin( const int id)
{
	std::list<FriendItem>::iterator iter;
	for(iter = memberList.begin(); iter!= memberList.end(); ++iter){
		if(iter->getQQ() == id){
			if(iter->getQunAdminValue() & QUN_TYPE_ADMIN)
				return true;
			break;
		}
	}	
	return false;
}

const bool Qun::hasMember(const int id)
{
	std::list<FriendItem>::iterator iter;
	for(iter = memberList.begin(); iter!= memberList.end(); ++iter){
		if(iter->getQQ() == id){
			return true;
		}
	}	
	return false;
}

/*************************************************************************************************************************************/

QunList &QunList::operator=(const QunList &rhs)
{
	qunList = rhs.getQunList();
	return *this;
}

void QunList::add( const Qun & q )
{
	Qun *qun = getQun(q.getQunID());
	if(!qun)
		qunList.push_back(q);
}

bool QunList::remove( const int id )
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			qunList.erase(iter);
			return true;
		}
	}
	return false;
}

void QunList::update( const Qun & q )
{
	if(remove(q.getQunID()))
		add(q);
}

Qun *QunList::getQun( const int id )
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!=qunList.end(); ++iter){
		if(iter->getQunID() == id){
			return &(*iter);
		}
	}
	return NULL;
}

void QunList::setDetails( const QunInfo & info )
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == info.getQunID()){
			iter->setDetails(info);
			return;
		}
	}
}

void QunList::setMember( const int id, const FriendItem & fi )
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			iter->setMember(fi);
			return;
		}
	}	
}

void QunList::setMembers( const int id, const std::list< FriendItem > & list )
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			iter->setMembers(list);
			return;
		}
	}
}

void QunList::setOnlineMembers(const int id, const std::list<int> &list)
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			iter->updateOnlineMembers(list);
			return;
		}
	}
}

void QunList::setQQList(const int id, const std::list<int> &list)
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			iter->setQQList(list);
			return;
		}
	}
}

void QunList::setMemberArgs(const int id, const std::map<int, QunMember> &list)
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			iter->setMemberArgs(list);
			return;
		}
	}
}

void QunList::setMyQunCardInfo( const int id, const std::string name, const unsigned char gender, 
			const std::string phone, const std::string email, const std::string memo )
{
	std::list<Qun>::iterator iter;
	for(iter = qunList.begin(); iter!= qunList.end(); ++iter){
		if(iter->getQunID() == id){
			iter->setCardName(name);
			iter->setCardGender(gender);
			iter->setCardPhone(phone);
			iter->setCardEmail(email);
			iter->setCardMemo(memo);
			return;
		}
	}	
}
