--[[
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 manages the ignorelist

]]

HOLOIGNORE_LIST = {};

local HI_Displayed_Names = 18;
local HI_List_Template = "%1$s: |cffffffff%2$s|r";
local HI_list = {};

local HI_SelectedEntry = -1;
local HI_LastClicked = -1;
local HI_IsGroupSelected = nil;
local HI_Moving_Ignore = nil;
local HI_Target_Ignore = nil;
local HI_LastAddedIgnore = nil;


-- this one hooks our functions and gets the realm list
local function HIF_OnLoad(self)

	-- insert the drop down menu
	HoloIgnore_InsertDropDown();

	-- hide old ignores frame
	IgnoreListFrame:Hide();
	-- and replace with new
	IgnoreListFrame = HoloIgnoreFrame;

	-- modify the ADD_IGNORE popup
	StaticPopupDialogs["ADD_IGNORE"].OnAccept = function(self)
		HoloIgnore_AddIgnore(self.editBox:GetText());
	end
	StaticPopupDialogs["ADD_IGNORE"].EditBoxOnEnterPressed = function(self)
		HoloIgnore_AddIgnore(self:GetParent().editBox:GetText());
		self:GetParent():Hide();
	end

	-- hide the button on the drag item
	HoloIgnoreNameButtonDrag:SetNormalTexture("");
	-- get the correct list
	HI_list = HoloFriendsFuncs_LoadList(HOLOIGNORE_LIST, UnitName("player"));
	self:RegisterEvent("IGNORELIST_UPDATE");
end


-- create and insert a new ignore entry
local function HIF_InsertNewIgnore(name, group)
	if ( not name or name == "" ) then
		return nil;
	end

	local temp = {};
	temp.name = name;
	if ( not group or group == "" ) then
		temp.group = IGNORE;
	else
		temp.group = group;
	end

	-- if it has comment in friendslist, copy it
	local list = HoloFriends_GetList();
	local wasFriend = HoloFriendsFuncs_ContainsPlayer(list, name);
	if ( wasFriend ) then
		temp.comment = HoloFriendsLists_GetComment(list, wasFriend);
	end
	table.insert(HI_list, temp);
end


-- check the local list
local function HIF_CheckLocalList()
	local index = next(HI_list);
	while ( index ) do
		if ( not HI_list[index].name ) then
			table.remove(HI_list, index);
		else
			index = next(HI_list, index);
		end
	end

	-- check if group "ignore" is in our list
	if ( not HoloFriendsFuncs_ContainsGroup(HI_list, IGNORE) ) then
		HIF_InsertNewIgnore("1", IGNORE);
	end
end


-- check if all entries in server ignore list are in our list
local function HIF_CheckServerList()
	local numIgnores = GetNumIgnores();
	-- data we get from server
	local name;
	local srvIgno = {};

	-- check if all ignores are in our list
	for index = 1, numIgnores do
		name = GetIgnoreName(index);
		playerIndex = HoloFriendsFuncs_ContainsPlayer(HI_list, name);
		if ( not playerIndex ) then
			HIF_InsertNewIgnore(name);
		end
	end

	--sort the entries
	table.sort(HI_list, HoloFriendsFuncs_CompareEntry);

	return numIgnores;
end


function HoloIgnore_OnHide()
	-- prevents addition of wrong spelled ignores
	HI_LastAddedIgnore = nil;
end


function HoloIgnore_OnEvent(self, event, ...)
	if ( event == "VARIABLES_LOADED" ) then
		HIF_OnLoad(self);
		self:UnregisterEvent("VARIABLES_LOADED");
		return;
	end
	if ( event == "IGNORELIST_UPDATE" ) then
		if( HI_LastAddedIgnore and (HI_LastAddedIgnore ~= "") and HoloIgnore_IsSelectedEntryValid() ) then
			HIF_InsertNewIgnore(HI_LastAddedIgnore, HI_list[HoloIgnore_GetSelectedEntry()].group);
		end
		HI_LastAddedIgnore = nil;
		HoloIgnore_List_Update();
		return;
	end
end


function HoloIgnore_OnUpdate()
	if ( HI_Moving_Ignore ) then
		local slot;
		HI_Target_Ignore = nil;
		for index = 1, HI_Displayed_Names, 1 do
			slot = getglobal("HoloIgnoreNameButton"..index);
			if ( MouseIsOver(slot) ) then
				slot:LockHighlight();
				HI_Target_Ignore = slot;
				local pos = HoloIgnoreScrollFrame:GetVerticalScroll();
				if ( index == 1 and pos >= FRIENDS_FRAME_IGNORE_HEIGHT ) then
					HoloIgnoreScrollFrameScrollBar:SetValue(pos - FRIENDS_FRAME_IGNORE_HEIGHT);
				end
				if ( index == HI_Displayed_Names and
				     (pos < FRIENDS_FRAME_IGNORE_HEIGHT*(table.getn(HI_list) - HI_Displayed_Names)) )
				then
					HoloIgnoreScrollFrameScrollBar:SetValue(pos + FRIENDS_FRAME_IGNORE_HEIGHT);
				end
			else
				slot:UnlockHighlight();
			end
		end
		HI_Moving_Ignore:UnlockHighlight();
	end
end


-- add a ignore
function HoloIgnore_AddIgnore(name)
	if ( name and name ~= "" ) then
		HI_LastAddedIgnore = name;
		AddIgnore(HI_LastAddedIgnore);
	elseif ( UnitIsPlayer("target") and UnitCanCooperate("player", "target") ) then
		HI_LastAddedIgnore = UnitName("target");
		AddIgnore(HI_LastAddedIgnore);
	else
		StaticPopup_Show("ADD_IGNORE");
	end
end


-- remove the selected name
function HoloIgnore_RemoveIgnore()
	if ( not HI_IsGroupSelected and HoloIgnore_IsSelectedEntryValid() ) then
		-- remove from original list
		DelIgnore(HI_list[HI_SelectedEntry].name);
		-- remove from our list
		table.remove(HI_list, HI_SelectedEntry);
		HI_SelectedEntry = -1;
	end
end


-- update scrollframe & buttons
function HoloIgnore_List_Update()
	local visibleCnt, sumCounter;

	-- check the local list
	HIF_CheckLocalList();

	-- check if server list has more entries than our
	sumCounter = HIF_CheckServerList();

	-- get visible index table
	local ShownIndexTable = {};
	ShownIndexTable = HoloFriendsFuncs_ShownListTab(HI_list, HI_Displayed_Names);

	-- get some counters
	local numElementsTable = table.getn(ShownIndexTable);
	local visibleCnt = 0;
	if ( numElementsTable > HI_Displayed_Names ) then
		visibleCnt = numElementsTable;
	else
		local numElementsList = table.getn(HI_list);
		for ii = 1, numElementsTable do
			if ( ShownIndexTable[ii] <= numElementsList ) then
				visibleCnt = visibleCnt + 1;
			end
		end
	end

	-- enable/disable remove-buttons
	if ( HoloIgnore_IsSelectedEntryValid() ) then
		if ( HoloFriendsFuncs_IsGroup(HI_list, HI_SelectedEntry) ) then
			if ( HoloFriendsLists_GetGroup(HI_list, HI_SelectedEntry) ~= IGNORE ) then
				HoloIgnoreRemoveGroupButton:Enable();
				HoloIgnoreStopIgnoreButton:Disable();
			else
				HoloIgnoreRemoveGroupButton:Disable();
				HoloIgnoreStopIgnoreButton:Disable();
			end
		else
			HoloIgnoreRemoveGroupButton:Disable();
			HoloIgnoreStopIgnoreButton:Enable();
		end
	else
		HoloIgnoreRemoveGroupButton:Disable();
		HoloIgnoreStopIgnoreButton:Disable();
	end

	-- ScrollFrame stuff
	local ScrollBarSize = 0;
	if ( visibleCnt > HI_Displayed_Names ) then ScrollBarSize = 20; end 
	FauxScrollFrame_Update(HoloIgnoreScrollFrame, visibleCnt, HI_Displayed_Names, 16);
	-- scrollframe offset
	local offset = FauxScrollFrame_GetOffset(HoloIgnoreScrollFrame);

	-- button and textures
	local button, buttonText;

	local entry = {};
	local line = 1;

	-- set values / textures for all buttons
	while ( line <= HI_Displayed_Names ) do
		local index = ShownIndexTable[line + offset];

		entry = HI_list[index];

		button = getglobal("HoloIgnoreNameButton"..line);
		button:SetID(index);

		if ( line > visibleCnt ) then
			button:Hide();
		else
			button:Show();
		end

		if ( entry ) then
			if ( index == HI_SelectedEntry ) then
				button:LockHighlight();
			else
				button:UnlockHighlight();
			end

			-- get textfield
			buttonText = getglobal("HoloIgnoreNameButton"..line.."Name");

			-- group entry ?
			if ( entry.name == "1" or entry.name == "0" ) then
				buttonText:SetText(entry.group);
				buttonText:ClearAllPoints();
				buttonText:SetPoint("TOPLEFT", "HoloIgnoreNameButton"..line, "TOPLEFT", 20, 0);

				if ( entry.name == "1" ) then
					button:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-Up");
				else
					button:SetNormalTexture("Interface\\Buttons\\UI-PlusButton-Up");
				end
			else -- no, we got a player
				buttonText:SetText(format(TEXT(HI_List_Template),
							  entry.name,
							  HoloFriendsLists_GetComment(HI_list, index)));
				button:SetNormalTexture("");
			end

			-- Set the button text with
			button:SetWidth(315 - ScrollBarSize);
			buttonText:SetWidth(276 - ScrollBarSize);
		end

		line = line + 1;
	end
end


-- drag and drop function
function HoloIgnore_NameButton_OnDragStart(self)
	if ( HoloFriendsFuncs_IsGroup(HI_list, self:GetID()) ) then
		return;
	end

	local cursorX, cursorY = GetCursorPosition();
	cursorX = cursorX / self:GetScale();
	cursorY = cursorY / self:GetScale();

	HI_Moving_Ignore = HoloIgnoreNameButtonDrag;
	HI_Moving_Ignore:SetID(self:GetID());
	HoloIgnoreNameButtonDragName:SetText(getglobal(self:GetName().."Name"):GetText());
	HI_Moving_Ignore:ClearAllPoints();
	HI_Moving_Ignore:SetPoint("TOPLEFT", "UIParent", "BOTTOMLEFT", cursorX+5, cursorY);
	HI_Moving_Ignore:Show();
	HI_Moving_Ignore:StartMoving();
end


-- drag and drop function
function HoloIgnore_NameButton_OnDragStop()
	if ( not HI_Moving_Ignore ) then
		return;
	end

	HI_Moving_Ignore:StopMovingOrSizing();
	HI_Moving_Ignore:Hide();
	HI_Moving_Ignore:ClearAllPoints();

	if ( not HI_Target_Ignore ) then
		return;
	end

	local targetIndex = HI_Target_Ignore:GetID();
	if ( targetIndex > table.getn(HI_list) ) then
		targetIndex = table.getn(HI_list);
	end
	HI_list[HI_Moving_Ignore:GetID()].group = HI_list[targetIndex].group;
	HI_Moving_Ignore = nil;
	HoloIgnore_List_Update();
end


-- if we clicked on a header, toggle state and select header, otherwise just select entry
function HoloIgnore_NameButton_OnClick(self, button)
	HI_LastClicked = self:GetID();
	if ( not HI_LastClicked ) then
		return;
	end

	if ( button == "LeftButton" ) then
		HI_SelectedEntry = HI_LastClicked;
		if ( HI_list[HI_SelectedEntry].name == "0" ) then
			HI_IsGroupSelected = 1;
			HI_list[HI_SelectedEntry].name = "1";
		elseif ( HI_list[HI_SelectedEntry].name == "1" ) then
			HI_IsGroupSelected = 1;
			HI_list[HI_SelectedEntry].name = "0";
		else
			HI_IsGroupSelected = nil;
		end
		HoloIgnore_List_Update();
	else
		HoloIgnore_ShowListDropdown(HI_LastClicked);
	end
end

-- END OF GUI FUNCTIONS


-- BEGIN OF GETTERS/SETTERS

-- returns the id of the last clicked entry
function HoloIgnore_GetLastClickedIndex()
	return HI_LastClicked;
end


-- returns the selected entry
function HoloIgnore_GetSelectedEntry()
	return HI_SelectedEntry;
end


-- returns true if the selected entry is valid
function HoloIgnore_IsSelectedEntryValid()
	return HoloFriendsFuncs_IsListIndexValid(HI_list, HI_SelectedEntry);
end


function HoloIgnore_DeselectEntry()
	HI_SelectedEntry = -1;
end


function HoloIgnore_GetList()
	return HI_list;
end

-- END OF GETTER/SETTERS

-- BEGIN OF ITEM TOOLTIP

function HoloIgnore_NameButton_SetTooltip(self)
	local index = self:GetID();
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT");
	GameTooltip:SetText(HoloFriendsLists_GetName(HI_list, index), 1.0, 1.0, 1.0);
	if ( HoloFriendsLists_GetComment(HI_list, index) ~= "" ) then
		GameTooltip:AddLine(HoloFriendsLists_GetComment(HI_list, index),
				    NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 1);
	end
	GameTooltip:Show();
end

-- END OF ITEM TOOLTIP
