-- -------------------------------------------------------------------------- --
-- InfoFrame for La Vendetta Boss Mods written by LV|Nitram & Destiny|Tandanu --
-- -------------------------------------------------------------------------- --
--  v1.00:	-- from InfoFrame
--  	Added functions:
--		myInfoFrame = LVBMGui:CreateInfoFrame(title, text)
--		myInfoFrame:SetTitle(title)
--		myInfoFrame:SetText(text)
--
--  	myInfoFrameStatusBar = myInfoFrame:CreateStatusBar(min, max, value, title)
--  	myInfoFrameStatusBar:SetValue(value)
--  	myInfoFrameStatusBar:SetTitle(title)
--
--  	myInfoFrameTextField = myInfoFrame:CreateTextField(text)
--  	myInfoFrameTextField:SetText(text)
--
--		these methods are also available for status bars and text fields
--		myInfoFrame:Show()
--		myInfoFrame:Hide()
--  	myInfoFrame:GetObject() - returns the frame object
--  	myInfoFrame:Delete() - you can not delete frames, but this function hides the frame and moves it to a "trash can"...the frame will be re-used next time you create a frame of this type
--
--
--  	Added Function LVBMGui.InfoFrameRestoreFind(ftype)	(DO NOT CALL)		-- search in Trash
--		Added Function LVBMGui.InfoFrameGetNewName()		(DO NOT CALL)		-- used to get a new FrameName
--
--  	Added Function LVBM_Gui_CreateDistanceFrame() for a in Range Frame (C'Thun,...)
--  	Added Function LVBM_Gui_DistanceFrame() to show/hide the Range Frame
--  	Added Function LVBM_InfoFrameTemplate_OnUpdate() 	-- allways called by Frame:OnUpdate
--
-- -------------------------------------------------------------------- --



LVBMGui = {		-- Create table
--	[1] = {
--		["Ident"] = "MyFrame",
--		["Frame"] = "<frame xxxx>",
--		["SubFrames"] = {
--			[1] = {
--				["Ident"] = "MyFrameBar1",
--				["Type"] = "StatusBar",			-- StatusBar // TextFrame
--				["Frame"] = "<frame xxx>",
--			}
--		}
--	},
	["Trash"] = {			-- Unused Frames moved there for recycling 
--		[1] = {
--			["Frame"] = "<frame xxx>",
--			["Type"] = "StatusBar",
--		}
	},
	["FrameID"] = 1;	-- hmm i can't find a function to change the Name of a Frame so i use my own with ID
};

LVBMGuiStatusBar = setmetatable({}, {["__index"] = LVBMGui});
LVBMGuiTextField = setmetatable({}, {["__index"] = LVBMGui});

function LVBM_InfoFrameTemplate_OnUpdate(frame, elapsed, forceUpdate)
	frame.elapsed = frame.elapsed + elapsed;
	if( frame.elapsed > 1 ) or forceUpdate then
		frame.elapsed = 0;
		frame.myTemp = 0;
		frame.myTempHeight = 0;
					
		frame.myWidth = getglobal(frame:GetName().."Text"):GetStringWidth();
		if( frame.myWidth < 84 ) then	frame.myWidth = 84;	end

		frame.myHeight = string.getnum( getglobal(frame:GetName().."Text"):GetText(), "\n" );
		frame.myHeight = frame.myHeight * 16;
		if( frame.myHeight < 16 ) then	frame.myHeight = 16;	end

		local subframes = { frame:GetChildren() };
		for _,child in ipairs(subframes) do

			child:SetPoint("TOP", frame, "TOP", 0, ((frame.myTempHeight + frame.myHeight + 23 + 10 )*(-1)));

			frame.myTemp = child:GetWidth();
			frame.myTempHeight = frame.myTempHeight + child:GetHeight();
			if( frame.myTemp > frame.myWidth ) then
				frame.myWidth = frame.myTemp;
			end
		end

		frame:SetWidth( frame.myWidth + 16 );
		frame:SetHeight( frame.myTempHeight + frame.myHeight + 23 + 16 );		-- Childs + Text + Title + Border 
	end
end

function LVBMGui.InfoFrameGetNewName()
	LVBMGui["FrameID"] = LVBMGui["FrameID"] + 1;
	return "LVBMInfoFrameID"..(LVBMGui["FrameID"] - 1);
end

LVBMGuiMetatable = {
	["__index"] = LVBMGui,
};

LVBMGuiStatusBarMetatable = {
	["__index"] = LVBMGuiStatusBar,
};

LVBMGuiTextFieldMetatable = {
	["__index"] = LVBMGuiTextField,
};

function LVBMGui:CreateInfoFrame(iTitle, iText)	
	local tempFrame;
	tempFrame = LVBMGui.InfoFrameRestoreFind("MainFrame");
	if( tempFrame ) then
		tempFrame:SetParent("UIParent");
		tempFrame:ClearAllPoints();
	else 
		tempFrame = CreateFrame("Frame", LVBMGui.InfoFrameGetNewName(), UIParent, "LVBMInfoFrameTemplate");
	end

	if( LVBM.Options.Gui.InfoFramePointX < 0 or LVBM.Options.Gui.InfoFramePointX > UIParent:GetWidth() ) then
		LVBM.Options.Gui.InfoFramePointX = UIParent:GetWidth() / 2;
	end
	if( LVBM.Options.Gui.InfoFramePointY > 0 or LVBM.Options.Gui.InfoFramePointY < (UIParent:GetHeight()*(-1)) ) then
		LVBM.Options.Gui.InfoFramePointY = (UIParent:GetHeight() / (-2));
	end

	tempFrame:SetPoint("CENTER", "UIParent", "TOPLEFT", LVBM.Options.Gui.InfoFramePointX, LVBM.Options.Gui.InfoFramePointY);
	tempFrame:SetBackdropColor(0, 0, 1, 0.5);
	if( iTitle ~= nil ) then
		getglobal(tempFrame:GetName().."Title"):SetText(iTitle);
	end
	if( iText ~= nil ) then
		getglobal(tempFrame:GetName().."Text"):SetText(iText);
	end
	tempFrame:Show();
	return setmetatable({
		["Frame"] = tempFrame,
		["SubFrames"] = {},
	}, LVBMGuiMetatable);
end

function LVBMGui:GetObject()
	return self.Frame;
end

function LVBMGui:Hide()
	self.Frame:Hide();
end

function LVBMGui:Show()
	self.Frame:Show();
end

function LVBMGui:SetTitle(iTitle)
	getglobal(self.Frame:GetName().."Title"):SetText(iTitle);
end

function LVBMGui:SetText(iText)
	getglobal(self.Frame:GetName().."Text"):SetText(iText);
end

function LVBMGui:CreateStatusBar(iMin, iMax, iValue, iTitle)
	local tempFrame;
	tempFrame = LVBMGui.InfoFrameRestoreFind("StatusBar");
	if( tempFrame ) then
		tempFrame:SetParent(self.Frame);
		tempFrame:ClearAllPoints();
	else 
		tempFrame = CreateFrame("StatusBar", LVBMGui.InfoFrameGetNewName(), self.Frame, "LVBMInfoFrameTemplateBar");
	end
	tempFrame:SetWidth(140);
	tempFrame:SetHeight(16);
	tempFrame:SetPoint("TOP", self.Frame, "TOP", 0, 20);
	tempFrame:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
	tempFrame:SetStatusBarColor(1, 0.7, 0);
	tempFrame:SetMinMaxValues(tonumber(iMin), tonumber(iMax));
	tempFrame:SetValue(tonumber(iValue));
	if( iTitle ) then	getglobal(tempFrame:GetName().."Text"):SetText(iTitle);
	else			getglobal(tempFrame:GetName().."Text"):SetText("");
	end
	tempFrame:Show();

	table.insert(self.SubFrames, {
		["Type"] = "StatusBar",
		["Frame"] = tempFrame,
	});
	return setmetatable(self.SubFrames[table.getn(self.SubFrames)], LVBMGuiStatusBarMetatable);
end

function LVBMGuiStatusBar:SetValue(iValue)
	self.Frame:SetValue(tonumber(iValue));
end

function LVBMGuiStatusBar:SetTitle(iTitle)
	getglobal(self.Frame:GetName().."Text"):SetText(iTitle);
end

function LVBMGui:CreateTextField(iText)
	local tempFrame;
	tempFrame = LVBMGui.InfoFrameRestoreFind("TextFrame");
	if( tempFrame ) then
		tempFrame:SetParent(self.Frame);
		tempFrame:ClearAllPoints();
	else 
		tempFrame = CreateFrame("Frame", LVBMGui.InfoFrameGetNewName(), self.Frame, "LVBMInfoFrameTemplateTextField");
	end
	tempFrame:SetPoint("TOP", self.Frame, "TOP", 0, 20);
	tempFrame:Show();

	if( iText ~= nil ) then
		getglobal(tempFrame:GetName().."Text"):SetText(iText);
	end

	table.insert(self.SubFrames, {
		["Ident"] = iSub,
		["Type"] = "TextFrame",
		["Frame"] = tempFrame,
	});	
	return setmetatable(self.SubFrames[table.getn(self.SubFrames)], LVBMGuiTextFieldMetatable);
end

function LVBMGuiTextField:SetText(iText)
	getglobal(self.Frame:GetName().."Text"):SetText(iText);
end

------------------
-- FRAME KILLER --
------------------
function LVBMGui:Delete()	-- iSub optional
	if( self.SubFrames ) then	-- delete MainFrame
		for index, value in self.SubFrames do		-- first delete all sub Frames
			value:Delete();
		end
	end
	self.Frame:ClearAllPoints();
	self.Frame:SetParent("LVBMInfoFrameTrash");
	self.Frame:SetPoint("TOPLEFT", 0, 0);
	self.Frame:UnregisterAllEvents();
	if getglobal(self.Frame:GetName().."Title") then
		getglobal(self.Frame:GetName().."Title"):SetText("");
	end
	getglobal(self.Frame:GetName().."Text"):SetText("");
	if self.Type == "StatusBar" then
		self.Frame:SetValue(0);
	end
	self.Frame:Hide();
	table.insert(LVBMGui["Trash"], { 
		["Type"] = self.Type or "MainFrame",
		["Frame"] = self.Frame,
	});
	self = nil;
end

function LVBMGui.InfoFrameRestoreFind(ftype) 		-- please DO NOT CALL THIS FUNCTION
	if( ftype == nil ) then return false; end
	for index, value in LVBMGui.Trash do
		if( value.Type == ftype ) then
			LVBMGui.Trash[index] = nil;
			return value.Frame;
		end
	end
	return false;
end

--------------------------------------------------------------------------------
--
--   DistanceFrame - Usage via  /distance  loaded by GUI on "PLAYER_LOGIN"
--
--------------------------------------------------------------------------------


function LVBM_Gui_DistanceFrame(moption)
	if( LVBMDistanceFrame ) then
		local mframe = LVBMDistanceFrame:GetObject();
		if( mframe:IsShown() and not moption ) then	LVBMDistanceFrame:Hide();
		else LVBMDistanceFrame:Show(); end
	else LVBM_Gui_CreateDistanceFrame();
	end
end

 function LVBM_Gui_CreateDistanceFrame()
	LVBMDistanceFrame = LVBMGui:CreateInfoFrame(LVBMGUI_DISTANCE_FRAME_TITLE, LVBMGUI_DISTANCE_FRAME_TEXT);
	if( not LVBMDistanceFrame ) then 
		LVBM.AddMsg("Can't get Frame from CreateInfoFrame()");
		return false; 
	end
	local MainFrame = LVBMDistanceFrame:GetObject();
	MainFrame:SetScript("OnUpdate", function() LVBM_InfoFrameTemplate_OnUpdate(this, arg1); LVBM_Gui_CreateDistanceFrame_OnUpdate(); end);

	if( LVBM.Options.Gui.DistanceFramePointX > UIParent:GetWidth() ) then
		LVBM.Options.Gui.DistanceFramePointX = UIParent:GetWidth() / 2;
	end
	if( LVBM.Options.Gui.DistanceFramePointY > 0 or LVBM.Options.Gui.DistanceFramePointY < (UIParent:GetHeight()*(-1)) ) then
		LVBM.Options.Gui.DistanceFramePointY = (UIParent:GetHeight() / (-2));
	end
	MainFrame:ClearAllPoints();
	MainFrame:SetPoint("CENTER", "UIParent", "TOPLEFT", LVBM.Options.Gui.DistanceFramePointX, LVBM.Options.Gui.DistanceFramePointY);

	LVBMDistanceFramePlayer1 = LVBMDistanceFrame:CreateTextField("");
	getglobal(LVBMDistanceFramePlayer1:GetObject():GetName().."Text"):SetTextColor(1, 0, 0);
	LVBMDistanceFramePlayer2 = LVBMDistanceFrame:CreateTextField("");
	getglobal(LVBMDistanceFramePlayer2:GetObject():GetName().."Text"):SetTextColor(1, 0, 0);
	LVBMDistanceFramePlayer3 = LVBMDistanceFrame:CreateTextField("");
	getglobal(LVBMDistanceFramePlayer3:GetObject():GetName().."Text"):SetTextColor(1, 0, 0);
	LVBMDistanceFramePlayer4 = LVBMDistanceFrame:CreateTextField("");
	getglobal(LVBMDistanceFramePlayer4:GetObject():GetName().."Text"):SetTextColor(1, 0, 0);
	LVBMDistanceFramePlayer5 = LVBMDistanceFrame:CreateTextField("");
	getglobal(LVBMDistanceFramePlayer5:GetObject():GetName().."Text"):SetTextColor(1, 0, 0);
end

function LVBM_Gui_CreateDistanceFrame_OnUpdate()
	if( this.elapsed == nil ) then this.elapsed = 0; end
	this.elapsed = this.elapsed + arg1;
	if( this.elapsed > 0.5 ) then
		local x=1;

		for i=1, GetNumRaidMembers(), 1 do
			if( x <= 5 and CheckInteractDistance("raid"..i, 3) 
			and not UnitIsDeadOrGhost("raid"..i) 
			and UnitName("raid"..i) ~= UnitName("player") ) then

				getglobal("LVBMDistanceFramePlayer"..x):SetText(UnitName("raid"..i));
				x = x + 1;

			end
		end

		if( x <= 5 ) then
			for i=x, 5, 1  do
				getglobal("LVBMDistanceFramePlayer"..i):SetText("");
			end
		end
	end
end
