/***************************************************************************
 *   Copyright (C) 2004 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.             *
 ***************************************************************************/
#ifndef LIBEVAFRIENDLIST_H
#define LIBEVAFRIENDLIST_H
#include <map>
#include <list>
#include "evadefines.h"
#include "evafriend.h"
#include "evaonlinestatus.h"
#include "evauserinfo.h"


#define BUDDY_INVALID_FIELD -1
// class QQFriend try to encapculate all information about your buddy,
// and provides enough methods to update them from time to time
class QQFriend{
public:
	QQFriend();
	QQFriend(const int qqid, char qqFace);
	QQFriend(const QQFriend &rhs);
	
	const int getQQ() const { return qqNum; }
	const char getFace() const { return face; }
	const char getAge() const { return age; }
	const char getGender() const { return gender; }
	const std::string &getNick() const { return nick; } 
	const char getExtFlag() const { return extFlag; }
	const char getCommonFlag() const { return commonFlag; }
	const long getLoginTime() const { return loginTime; }
	const long getIdleTime() const { return idleTime; }
	const long getLastRefreshTime() const { return lastRefreshTime; }
	
	void setAge(const char age) { this->age = age; }
	void setGender(const char gender) { this->gender = gender; }
	void setNick(const std::string nick) { this->nick = nick; }
	void setExtFlag(const char flag) { extFlag = flag; }
	void setCommonFlag(const char flag) {commonFlag = flag; }
	
	bool isMember() { return (commonFlag & 0x2) != 0; }
	bool isBoy() { return gender == QQ_FRIEND_GENDER_GG;}           // aboves from FriendItem

	const int getIP() const { return IP; }
	const short getPort() const { return port;}
	const char getStatus() const { return status;}
	const char getUnknown1_4() const { return unknown4;}    // bit 4
	const char getUnknown2_11() const { return unknown11;}   // bit 11
	const short getUnknown3_13_14() const { return unknown13_14;}  // bit 13-14
	const unsigned char * getUnknownKey() const { return unknownKey;}   
	const short getUnknown4_31_32() const { return unknown31_32; }
	const char getOnlineExtFlag() const { return onlineExtFlag; }
	const char getOnlineCommFlag() const { return onlineCommonFlag; }
	const short getUnknown5_35_36() const { return unknown35_36; }      // aboves from FriendOnlineEntry
	
	
	void setFace( const char faceCode ) { face = faceCode; } // these 7 funcs update status of a 
	void setIP( const int ip ) { IP = ip; }                  // buddy whenever her/him send msg 
	void setPort( const short p) { port = p; }                 // or change online status
	void setStatus( const char s) { status = s; }
	void setUnknown1_4( const char un) { unknown4 = un; }
	void setUnknown2_11( const char un) { unknown11 = un; }
	void setUnknown3_13_14( const short un) { unknown13_14 = un; }
	void setUnknownKey( const unsigned char *key) { memcpy(unknownKey, key, 16); }
	
	const short getVersion() const { return clientVersion; }    
	void setVersion( const short version) { clientVersion = version; } // set it once got msg from buddy
	const unsigned char *getFileSessionKey() const { return fileSessionKey; }
	void setFileSessionKey( const unsigned char *key)                // set it once got msg from buddy
		{ memcpy(fileSessionKey, key, 16); }
	
	void setFriendItem( const FriendItem &item);               // when u get friend list, you set this
	void setFriendOnlineEntry( const FriendOnlineEntry &entry);  // when u get online friend you set this
	
	const ContactInfo &getUserInformation() const { return userInfo; }
	void setUserInformation( const ContactInfo &info);   // once got user info, set it and update some info
	
	void setGroupIndex( const int index) { groupIndex = index; }
	const int getGroupIndex() const { return groupIndex; }
	
	enum sortItem { Sort_QQ, Sort_Nick, Sort_Status};
	const sortItem getSortField() const { return field2Sort; }
	static void setSortField(const sortItem field) { field2Sort = field; }
	QQFriend &operator=(const QQFriend &rhs);
	int operator==(const QQFriend &rhs) const ; // note: we only test qqNum
	int operator<(const QQFriend &rhs) const ;
private:
	int qqNum;
	char face;
	char age;
	char gender;
	std::string nick;	
	char extFlag;  // bit1: if has qqshow, the rest are unknown  
	char commonFlag;  //  bit1: memeber,   bit4: TCP mode,   bit5 : mobile QQ, 
			  //  bit6: mobile binding,   bit7: webcam
	long loginTime;
	long idleTime;
	long lastRefreshTime;
		
	char unknown4;
	int IP;  // 4 bytes
	short port;
	char unknown11;
	char status;
	short unknown13_14;
	unsigned char unknownKey[QQ_KEY_LENGTH];
	short unknown31_32;
	char onlineExtFlag;
	char onlineCommonFlag;
	short unknown35_36;
	
	short clientVersion;
	
	unsigned char fileSessionKey[QQ_KEY_LENGTH];
	
	ContactInfo userInfo;
	
	static sortItem field2Sort;
	
	int groupIndex;  // 0: default "My Buddies",  maximum is "Black List", the last two is "Anonymous"

	void initalizeBuddy(const int qqid = 0, char qqFace = 0);
};

class FriendList{
public:
	FriendList() {}
	~FriendList() { privateList.clear(); }	
	const bool hasFriend(const int id);
	const QQFriend *getFriend(const int id);
	bool deleteFriend(const int id);
	void addFriend(const QQFriend &frd);
	void updateFriend( const QQFriend &frd);  // if frd dosen't exists, add a new one
	void clearFriendList() { privateList.clear(); }
	
	bool updateFriendIP(const int id, const int ip);
	bool updateFriendPort(const int id, const short p);
	bool updateFriendFace(const int id, const char face);
	bool updateFriendStatus(const int id, const char status);
	bool updateFriendFileSessionKey(const int id, const unsigned char *key); 
	bool updateFriendGroupIndex(const int id, const int index);
	
	bool addFriendItemTo( const int id, const FriendItem &item);
	bool addOnlineFriendEntryTo( const int id, const FriendOnlineEntry & entry);
	bool addContactInfoTo( const int id, const ContactInfo &info);
	
	const int numberOfFriends() const { return privateList.size(); }
	std::list<QQFriend> getFriendsInGroupIndexOf( const int index);
        std::list<QQFriend> getAllFriends();
	
	const std::map<int, QQFriend> &getFriendList() const { return privateList; }	
	FriendList &operator=(const FriendList &rhs) { privateList = rhs.getFriendList(); return *this;}
private:
	std::map<int, QQFriend> privateList;
	std::map<int, QQFriend>::iterator getFriendPosition(const int id);
};

#endif


 
