--== Settings  ==--
stSettings.Friends = true
stSettings.Guild = true
stSettings.Friends_Size = nil
stSettings.Friends_Outline = "DEFAULT"
stSettings.Guild_Size = nil
stSettings.Guild_Outline = "DEFAULT"

--== Coding ==--
local f = CreateFrame("Frame",nil,UIParent)
f:RegisterEvent"VARIABLES_LOADED"
-- Colors 
local pClass = select(2, UnitClass("player"))
local CColor = { RAID_CLASS_COLORS[pClass].r, RAID_CLASS_COLORS[pClass].g, RAID_CLASS_COLORS[pClass].b }
local db = stSettings
local color
if db.ColorClass then
	color = CColor
else
	color = db.Color
end

--== Utils ==--
local function Hex(r, g, b)
	if type(r) == "table" then
		if r.r then r, g, b = r.r, r.g, r.b else r, g, b = unpack(r) end
	end
	return string.format("|cff%02x%02x%02x", r*255, g*255, b*255)
end
local colorCode = Hex(color)

local function ColorGradient(perc, ...)
	if (perc > 1) then
		local r, g, b = select(select('#', ...) - 2, ...)
		return r, g, b
	elseif (perc < 0) then
		local r, g, b = ...
		return r, g, b
	end
	
	local num = select('#', ...) / 3

	local segment, relperc = math.modf(perc*(num-1))
	local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)

	return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
end

local friends = {}
local onlineFriends
local function ParseFriends()
	onlineFriends = 0
		for i = 1, GetNumFriends() do			
			local f = {}
			f.name, f.level, f.class ,f.loc, f.online, f.status, f.note = GetFriendInfo(i)
			if f.online then
				onlineFriends = onlineFriends + 1
				for k,v in pairs(LOCALIZED_CLASS_NAMES_MALE) do if f.class == v then f.class = k end end
				if GetLocale() ~= "enUS" then -- feminine class localization (unsure if it's really needed)
					for k,v in pairs(LOCALIZED_CLASS_NAMES_FEMALE) do if f.class == v then f.class = k end end
				end
			end
			friends[i] = f
		end
end
local guild = {}
local onlineRoster, numTotal
local function ParseGuild()
		onlineRoster, numTotal = 0, GetNumGuildMembers(true)
		for i = 1, numTotal do			
			local m = {}
			m.name, _, _, m.level, m.class, m.loc, _,_, m.online, m.status, _ = GetGuildRosterInfo(i)
			if m.online then onlineRoster = onlineRoster + 1 end
			for k,v in pairs(LOCALIZED_CLASS_NAMES_MALE) do if m.class == v then m.class = k end end
			if GetLocale() ~= "enUS" then -- feminine class localization (unsure if it's really needed)
				for k,v in pairs(LOCALIZED_CLASS_NAMES_FEMALE) do if m.class == v then m.class = k end end
			end	
			guild[i] = m
		end
end		
	

f:SetScript("OnEvent",function()
	if(db.Friends) then
		local f_frame, f_text, fbg = stCore:CreateText"Friends"
		local fmoving, fmovable = false, false
		
		f_frame.Slash = function()
			if fbg:IsShown() then 
				fbg:Hide() 
				fmovable = false
				print("|cff00efffstText: |rFriends module locked")
			else 
				fbg:Show()
				fmovable = true
				print("|cff00efffstText: |rFriends module unlocked")
			end
		end

		local function UpdateFriends() ParseFriends() f_text:SetText(colorCode.."Friends:|r"..onlineFriends) end
		UpdateFriends()
		
		local ftip = CreateFrame("Button","st_FriendsButton",f_frame)
		ftip:SetAllPoints()
	
		ftip:SetScript("OnMouseDown",function(self) if(fmovable) then  if (IsAltKeyDown()) then f_frame:ClearAllPoints(); f_frame:StartMoving(); fmoving = true; GameTooltip:Hide()  end end end)
		ftip:SetScript("OnMouseUp",function(self) f_frame:StopMovingOrSizing(); fmoving = false; stTextDB.Friends = {f_frame:GetPoint()} end)
		
		f_frame:RegisterEvent"FRIENDLIST_UPDATE"
		f_frame:SetScript("OnEvent",UpdateFriends)
		
		local lcolor, fColor
		ftip:SetScript("OnEnter", function(self) if not fmoving and not InCombatLockdown() then
			local showBelow = select(2, self:GetCenter()) > UIParent:GetHeight()/2
			GameTooltip:SetOwner( self, "ANCHOR_NONE" )
			GameTooltip:SetPoint( showBelow and "TOP" or "BOTTOM", self, showBelow and "BOTTOM" or "TOP" )
			collectgarbage()
		
			ParseFriends()
			GameTooltip:AddDoubleLine("Friends", "Online: "..onlineFriends.." / "..GetNumFriends(),color.r, color.g, color.b,color.r, color.g, color.b)
			for _,friend in ipairs(friends) do
				if friend.online then
					lcolor = Hex(GetQuestDifficultyColor(friend.level))
--~ 					print(friend.class)
					fColor = { RAID_CLASS_COLORS[friend.class].r, RAID_CLASS_COLORS[friend.class].g, RAID_CLASS_COLORS[friend.class].b }
					fColor = " "..Hex(fColor)
--~ 					fColor = "|r"
					GameTooltip:AddDoubleLine(lcolor..friend.level..fColor..friend.name.."|r "..friend.status, "|cff999999"..friend.loc)
				end
			end
			GameTooltip:Show()
		end
		end)

		ftip:SetScript("OnLeave", function()  GameTooltip:Hide() end)	
			
		
	end
	if(db.Guild) then
		local g_frame, g_text, gbg = stCore:CreateText"Guild"
		local gmoving, gmovable = false, false
		
		g_frame.Slash = function()
			if gbg:IsShown() then 
				gbg:Hide() 
				gmovable = false
				print("|cff00efffstText: |rGuild module locked")
			else 
				gbg:Show()
				gmovable = true
				print("|cff00efffstText: |rGuild module unlocked")
			end
		end

		function UpdateGuild() ParseGuild() g_text:SetText(colorCode.."Guild:|r"..onlineRoster) end
		UpdateGuild()
		
		local gtip = CreateFrame("Button","st_GuildButton",g_frame)
		gtip:SetAllPoints()
	
		gtip:SetScript("OnMouseDown",function(self) if(gmovable) then  if (IsAltKeyDown()) then g_frame:ClearAllPoints(); g_frame:StartMoving(); gmoving = true; GameTooltip:Hide()  end end end)
		gtip:SetScript("OnMouseUp",function(self) g_frame:StopMovingOrSizing(); gmoving = false; stTextDB.Guild = {g_frame:GetPoint()} end)
		
		g_frame:RegisterEvent"GUILD_ROSTER_UPDATE"
		g_frame:RegisterEvent"PLAYER_GUILD_UPDATE"
		g_frame:RegisterEvent"PLAYER_ENTERING_WORLD"
		g_frame:SetScript("OnEvent",UpdateGuild)
		
		local glcolor, gColor
		gtip:SetScript("OnEnter", function(self) if not gmoving and not InCombatLockdown() then
			local showBelow = select(2, self:GetCenter()) > UIParent:GetHeight()/2
			GameTooltip:SetOwner( self, "ANCHOR_NONE" )
			GameTooltip:SetPoint( showBelow and "TOP" or "BOTTOM", self, showBelow and "BOTTOM" or "TOP" )
			collectgarbage()
		
			ParseGuild()
			GameTooltip:AddDoubleLine("Guild", "Online: "..onlineRoster.." / "..numTotal,color.r, color.g, color.b,color.r, color.g, color.b)
			GameTooltip:AddLine(format("%s: |cffffffff%s",GUILD_MOTD,GetGuildRosterMOTD()),.75,.9,1,1)
			for _,member in ipairs(guild) do
				if member.online then
					glcolor = Hex(GetQuestDifficultyColor(member.level))
--~ 					print(friend.class)
					gColor = { RAID_CLASS_COLORS[member.class].r, RAID_CLASS_COLORS[member.class].g, RAID_CLASS_COLORS[member.class].b }
					gColor = " "..Hex(gColor)
--~ 					fColor = "|r"
					GameTooltip:AddDoubleLine(glcolor..member.level..gColor..member.name.."|r "..member.status, "|cff999999"..member.loc)
				end
			end
			GameTooltip:Show()
		end
		end)

		gtip:SetScript("OnLeave", function()  GameTooltip:Hide() end)	
			
		
	end
end)
		