/***************************************************************************
 *   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.             *
 ***************************************************************************/
 
#include "evauser.h"
#include "evausersetting.h"
#include "evautil.h"
#include <string.h>


std::string EvaUser::anonymousName = "Anonymous";
std::string EvaUser::blackName = "Black List";

EvaUser::EvaUser(const int id, const std::string &password)
{
	char *tmp = (char *)malloc(password.length() * sizeof(char));
        memcpy(tmp, password.c_str(), password.length());
	qqNum = id;        
	md5Password = (char *)malloc(16 * sizeof(char));
	memcpy(md5Password, EvaUtil::doMd5Md5(tmp, password.length()), 16);
	status = Eva_Offline;
	groupNames.clear();
	setting = new EvaUserSetting(id);
	setting->loadSettings();
	isBuddyListLoaded = false;
}

EvaUser::EvaUser(const int id, const char *md5Password)
{
	qqNum = id;
	this->md5Password = (char *)malloc(16 * sizeof(char));
	memcpy(this->md5Password, md5Password, 16);
	status = Eva_Offline;
	groupNames.clear();
	setting = new EvaUserSetting(id);
}

EvaUser::~EvaUser()
{
	free(md5Password);
	delete setting;
}

char EvaUser::getStatusCode(const UserStatus status) 
{
	char statusCode;
	switch(status){
	case Eva_Online:
		statusCode = QQ_FRIEND_STATUS_ONLINE;
		break;
	case Eva_Offline:
		statusCode = QQ_FRIEND_STATUS_OFFLINE;
		break;
	case Eva_Invisible:
		statusCode = QQ_FRIEND_STATUS_INVISIBLE;
		break;
	case Eva_Leave:
		statusCode = QQ_FRIEND_STATUS_LEAVE;
		break;
	default:
		statusCode = QQ_FRIEND_STATUS_OFFLINE;
	}
	return statusCode;
}

const bool EvaUser::newGroup( const std::string & name )
{
	if(name == "") return false;
	groupNames.push_back(name);
	//saveGroupedBuddyList();
	return true;
}

void EvaUser::clearGroupNames()
{
	std::string buddy = groupNameAtIndex(0); 
	groupNames.clear();
	groupNames.push_back(buddy);	
}

void EvaUser::removeGroupName(const int index)
{
	if(index == 0 ) return; // the first group name cannot be removed
	if((uint)index >= groupNames.size()) return; 
	std::list<std::string>::iterator iter;
	iter = groupNames.begin();
	for(int i=0; i<index; i++)
		iter++;
	groupNames.erase(iter);
	//saveGroupedBuddyList();
}

const int EvaUser::getGroupIndexOf(const std::string &name)
{
	std::list<std::string>::iterator iter;
	int i=0;	
	for(iter=groupNames.begin(); iter != groupNames.end(); ++iter){
		if(name== *iter) break;
		++i;
	}
	if((uint)i>= groupNames.size()) 
		return -1;
	else 
		return i;	
}

void EvaUser::updateGroupName(const std::string &newName, const int index)
{
	if(index == 0 ) return; // the first group name cannot be removed
	if((uint)index >= groupNames.size()) return; 
	std::list<std::string>::iterator iter;
	iter = groupNames.begin();
	int i;
	for(i=0; i<index; i++){
		iter++;
	}
	groupNames.insert(iter, newName);
	groupNames.erase(iter);
	//saveGroupedBuddyList();
}

const std::string EvaUser::groupNameAtIndex( const int index )
{
	if(index == anonymousIndex) return anonymousName;
	if(index == blackIndex) return blackName;
	
	if((uint)index>= groupNames.size()) return "My Buddy";
	
	std::list<std::string>::iterator iter;
	int i=0;	
	for(iter=groupNames.begin(); iter != groupNames.end(); ++iter){
		if(i==index) break;
		++i;
	}
	return *iter;
}

const bool EvaUser::loadGroupedBuddyList()
{
	if(setting->loadBuddyList(groupNames, details, myFriends))
		isBuddyListLoaded = true;
	else
		isBuddyListLoaded = false;
	return isBuddyListLoaded;
}

const bool EvaUser::saveGroupedBuddyList()
{	
	return setting->saveBuddyList(groupNames, details, myFriends);
}
