--[[
HoloFriends addon created by Holo, continued by Zappster, followed by Andymon

Get the latest version at wow.curse.com

See HoloFriends_change.log for more informations  
]]

--[[

This file defines getters/setters of lists ( i.e. friends list, ignore list)

]]

local HF_Class_Colors = {
	["WARRIOR"]     = {RAID_CLASS_COLORS.WARRIOR.r, RAID_CLASS_COLORS.WARRIOR.g, RAID_CLASS_COLORS.WARRIOR.b},
	["MAGE"]        = {RAID_CLASS_COLORS.MAGE.r, RAID_CLASS_COLORS.MAGE.g, RAID_CLASS_COLORS.MAGE.b},
	["ROGUE"]       = {RAID_CLASS_COLORS.ROGUE.r, RAID_CLASS_COLORS.ROGUE.g, RAID_CLASS_COLORS.ROGUE.b},
	["DRUID"]       = {RAID_CLASS_COLORS.DRUID.r, RAID_CLASS_COLORS.DRUID.g, RAID_CLASS_COLORS.DRUID.b},
	["HUNTER"]      = {RAID_CLASS_COLORS.HUNTER.r, RAID_CLASS_COLORS.HUNTER.g, RAID_CLASS_COLORS.HUNTER.b},
	["SHAMAN"]      = {RAID_CLASS_COLORS.SHAMAN.r, RAID_CLASS_COLORS.SHAMAN.g, RAID_CLASS_COLORS.SHAMAN.b},
	["PRIEST"]      = {RAID_CLASS_COLORS.PRIEST.r, RAID_CLASS_COLORS.PRIEST.g, RAID_CLASS_COLORS.PRIEST.b},
	["WARLOCK"]     = {RAID_CLASS_COLORS.WARLOCK.r, RAID_CLASS_COLORS.WARLOCK.g, RAID_CLASS_COLORS.WARLOCK.b},
	["PALADIN"]     = {RAID_CLASS_COLORS.PALADIN.r, RAID_CLASS_COLORS.PALADIN.g, RAID_CLASS_COLORS.PALADIN.b},
	["DEATHKNIGHT"] = {RAID_CLASS_COLORS.DEATHKNIGHT.r, RAID_CLASS_COLORS.DEATHKNIGHT.g, RAID_CLASS_COLORS.DEATHKNIGHT.b}
};


--[[
  returns the group of the entry or UNKNOWN
  @param list - Table
  @param index - int
]]
function HoloFriendsLists_GetGroup(list, index)
	if ( not HoloFriendsFuncs_IsListIndexValid(list, index) ) then
		return UNKNOWN;
	end;

	return list[index].group;
end


--[[
  returns the name of the entry if it is a player, or group name if it is a group
  @param list - Table
  @param index - int
  @param setcolor - bolean
]]
function HoloFriendsLists_GetName(list, index, setcolor)
	if ( not HoloFriendsFuncs_IsListIndexValid(list, index) ) then
		return UNKNOWN;
	end;

	if ( HoloFriendsFuncs_IsGroup(list, index) ) then
		return list[index].group;
	else
		local class = list[index].class;
		local colors;
		if ( class and setcolor ) then
			colors = HF_Class_Colors[class];
		end
		if ( colors ) then
			local red = string.format("%x",math.floor(colors[1]*255));
			if strlen(red) == 1 then red = "0"..red; end
			local green = string.format("%x",math.floor(colors[2]*255));
			if strlen(green) == 1 then green = "0"..green; end
			local blue = string.format("%x",math.floor(colors[3]*255));
			if strlen(blue) == 1 then blue = "0"..blue; end
			return "|cff"..red..green..blue..list[index].name.."|r";
		else
			return list[index].name;
		end
	end
end


--[[
  returns true if connected flag is set
  @param list - Table
  @param index - int
]]
function HoloFriendsLists_IsConnected(list, index)
	return ( HoloFriendsFuncs_IsListIndexValid(list, index) and list[index].connected );
end


--[[
  returns the level of the entry, or HOLOFRIENDS_TOOLTIPUNKNOWN if no data is available
  @param list - Table
  @param index - int
]]
function HoloFriendsLists_GetLevel(list, index)
	if ( HoloFriendsFuncs_IsListIndexValid(list, index) and list[index].level ) then
		return list[index].level;
	else
		return HOLOFRIENDS_TOOLTIPUNKNOWN;
	end
end


--[[
  returns the class of the entry, or UNKNOWN if no data is available
  @param list - Table
  @param index - int
]]
function HoloFriendsLists_GetLCClass(list, index)
	if ( HoloFriendsFuncs_IsListIndexValid(list, index) and list[index].lc_class ) then
		return list[index].lc_class;
	else
		return UNKNOWN;
	end
end


--[[
  returns the area of the entry, or UNKNOWN if no data is available
  @param list - Table
  @param index - int
  @param setcolor - bolean
]]
function HoloFriendsLists_GetArea(list, index, setcolor)
	if ( HoloFriendsFuncs_IsListIndexValid(list, index) and list[index].area ) then
		local area = list[index].area
		if ( setcolor ) then
			if ( GetRealZoneText() == area ) then
				area = "|cff55bb00"..area.."|r"
			end
			if ( list[index].onstate and (list[index].connected == 1) ) then
				area = area.." "..list[index].onstate;
			end
		end
		return area;
	else
		return UNKNOWN;
	end
end


--[[
  returns the lastSeen value of the entry, or UNKNOWN if no data is available
  @param list - Table
  @param index - int
]]
function HoloFriendsLists_GetLastSeen(list, index)
	if ( HoloFriendsFuncs_IsListIndexValid(list, index) and list[index].lastSeen ) then
		return list[index].lastSeen;
	else
		return UNKNOWN;
	end
end


--[[
  get the notify flag of a player (player in server friends list)
  @param list - Table
  @param index - int
]]
function HoloFriendsLists_GetNotify(list, index)
	if ( HoloFriendsFuncs_IsListIndexValid(list, index) ) then
		return list[index].notify;
	else
		return nil;
	end
end


--[[
  returns the comment of the entry
  @param list - Table (list or ignoreList)
  @param index - int
]]
function HoloFriendsLists_GetComment(list, index)
	if ( HoloFriendsFuncs_IsListIndexValid(list, index) and list[index].comment ) then
		return list[index].comment;
	else
		return "";
	end
end


--[[
  set the notify flag of a player
  @param list - Table
  @param index - int
  @param notify - bol
  @param silent - bol
]]
function HoloFriendsLists_SetNotify(list, index, notify, silent, runwhat)
	if ( not HoloFriendsFuncs_IsListIndexValid(list, index) or
	     HoloFriendsFuncs_IsGroup(list, index) )
	then
		return;
	end

	list[index].notify = notify;
	if ( notify ) then
		local skip = false;
		for idx = 1, GetNumFriends() do
			local name = GetFriendInfo(idx);
			if ( name == list[index].name ) then skip = true; end
		end

		-- add to server list
		if ( not skip ) then
			HoloFriends_SetNotifyOn = true;
			HoloFriends_ignoreAddFriendsSysMsg = true;
			HoloFriends_RunListUpdate = runwhat;
			AddFriend(list[index].name);
			if ( not silent ) then
				local msg = format(HOLOFRIENDS_MSGFRIENDONLINEENABLED, list[index].name);
				HoloFriendsFuncs_SystemMessage(msg);
			end
			return;
		end
	else
		local skip = true;
		for idx = 1, GetNumFriends() do
			local name = GetFriendInfo(idx);
			if ( name == list[index].name ) then skip = false; end
		end

		-- add to server list
		if ( not skip ) then
			-- remove from server list
			HoloFriends_SetNotifyOff = true;
			HoloFriends_ignoreRemoveFriendsSysMsg = true;
			HoloFriends_RunListUpdate = runwhat;
			RemoveFriend(list[index].name);
			if ( not silent ) then
				local msg = format(HOLOFRIENDS_MSGFRIENDONLINEDISABLED, list[index].name);
				HoloFriendsFuncs_SystemMessage(msg);
			end
			return;
		end
	end

	if ( runwhat == "RunUpdate" ) then
		HoloFriends_List_Update();
	end
	if ( runwhat == "RunLocal" ) then
		HoloFriends_CheckLocalList();
	end
	if ( runwhat == "RunServer" ) then
		HoloFriends_CheckServerList();
	end
end


--[[
  set the comment of a player
  @param list - Table (list or ignoreList)
  @param index - int
  @param comment - str
  @param FL - bolean
]]
function HoloFriendsLists_SetComment(list, index, comment, FL)
	if ( (not HoloFriendsFuncs_IsListIndexValid(list, index)) or (not comment) ) then
		return;
	end
	list[index].comment = comment;

	if ( FL ) then list[index].mergecnt = nil; end
	if ( FL and list[index].notify ) then
		local name = list[index].name;
		local numNotifyFriends = GetNumFriends();
		for inum = 1, numNotifyFriends do
			local iname = GetFriendInfo(inum);
			if ( iname == name ) then
				HoloFriends_SetFriendNotes = true;
				SetFriendNotes(inum, comment);
				return;
			end
		end
	end
end
