Quantcast

-- Grab a copy of the local data
local addonName, addonData = ...
local Translate = addonData.Translate
local expansionLevel = GetExpansionLevel()

-- Option Data which we need to customize the layout somewhat
addonData.Options = {
	FrameWidth = 325,
	FrameHeight = 150,
	IsMovable = true,
	HasBorder = true,
}

-- Create the Portal and Teleport Buttons
addonData.CreateButtons = function(self,frame)

	self.PortalButtons = {};
	self.TeleportButtons = {};
	--local Faction = UnitFactionGroup("player");
	local engFaction, Faction = UnitFactionGroup("player")

	-- First the Portals
	for i,v in pairs(self.PortalDB) do
		if ( string.find(v.faction,Faction) ) then
			local Button = self:CreateSecureSpellButton(v.spellID,"PORTAL");
			if ( Button ) then
				Button:SetScript("OnEnter",function(self)
					addonData:ShowTooltip(self)
				end )
				Button:SetScript("OnLeave",function(self)
					addonData:HideTooltip(self)
				end )
				Button:SetParent(nil)
				Button:SetParent(frame);
				Button.levelRequired = v.levelRequired
				Button.expansionRequired = v.expansionRequired
				Button.dropItem = v.dropItem
				Button.questReward = v.questReward
				Button:Show()
				table.insert(self.PortalButtons,Button);
			end
		end
	end

	-- Now the Teleports
	for i,v in pairs(self.TeleportDB) do
		if ( string.find(v.faction,Faction) ) then
			local Button = self:CreateSecureSpellButton(v.spellID,"TELEPORT");
			if ( Button ) then
				Button:SetScript("OnEnter",function(self)
					addonData:ShowTooltip(self)
				end )
				Button:SetScript("OnLeave",function(self)
					addonData:HideTooltip(self)
				end )
				Button:SetParent(nil)
				Button:SetParent(frame);
				Button.levelRequired = v.levelRequired
				Button.expansionRequired = v.expansionRequired
				Button.dropItem = v.dropItem
				Button.questReward = v.questReward
				Button:Show()
				table.insert(self.TeleportButtons,Button);
			end
		end
	end

	return frame
end

-- Update the Title Texts
addonData.UpdateText = function(self,frame)
	local fontPath, fontSize, fontFlags
	if ( XMagePortsDB.DisplayText ) then
		fontPath, fontSize, fontFlags = frame.PortalTitle:GetFont()
		frame.PortalTitle:SetFont(fontPath,XMagePortsDB.FontSize,fontFlags);
		frame.PortalTitle:SetText(Translate["PORTALS"]);
		fontPath, fontSize, fontFlags = frame.TeleportTitle:GetFont()
		frame.TeleportTitle:SetFont(fontPath,XMagePortsDB.FontSize,fontFlags);
		frame.TeleportTitle:SetText(Translate["TELEPORTS"]);
	else
		frame.PortalTitle:SetText("");
		frame.TeleportTitle:SetText("");
	end
end

addonData.CreateText = function(self,frame)

	-- Create the Portal Heading
	frame.PortalHeading = CreateFrame("Frame","XMP_PortalHeading",frame);

	-- create the Portal Title
   	frame.PortalTitle = frame.PortalHeading:CreateFontString("XMP_PortalTitle", 'BACKGROUND', "GameFontNormal");
	frame.PortalTitle:SetPoint( "LEFT" );

	-- size frame to suit parent
	frame.PortalHeading:SetWidth(frame:GetWidth());
	frame.PortalHeading:SetHeight(frame.PortalTitle:GetStringHeight() + 5 );

	-- Create the Teleport Heading Frame
	frame.TeleportHeading = CreateFrame("Frame","XMP_TeleportHeading",frame);

	-- Create the Teleport Title
    frame.TeleportTitle = frame.TeleportHeading:CreateFontString("MagePorts_Frame_TeleportTitle", 'BACKGROUND', "GameFontNormal");
	frame.TeleportTitle:SetPoint( "LEFT" );
	frame.TeleportTitle:SetText("");

	self:UpdateText(frame)

	-- Size Frame to suit parent width and text height
	frame.TeleportHeading:SetWidth(frame:GetWidth());
	frame.TeleportHeading:SetHeight(frame.TeleportTitle:GetStringHeight() + 5 );

	-- Show the frames
	frame.PortalHeading:Show()
	frame.TeleportHeading:Show()
	frame.PortalTitle:Show()
	frame.TeleportTitle:Show()

	-- Return the created frame
	return frame
end

addonData.ArrangeLayout = function(self,frame)

	-- If the Portal Buttons table not created
	if ( not addonData.PortalButtons) then return end

	local teleCount = #self.TeleportButtons;
	local portCount = #self.PortalButtons;
	local cellCount = math.max(teleCount,portCount)

	-- if there are no items in the table
	if ( cellCount < 1 ) then return end

	local cellSize = addonData.PortalButtons[1]:GetWidth();
	local cellGap = cellSize * 0.25;

	frame.PortalHeading:SetPoint( "BOTTOMLEFT", addonData.PortalButtons[1], "TOPLEFT", -cellGap , cellGap);
	frame.PortalHeading:SetPoint( "BOTTOMRIGHT", addonData.PortalButtons[portCount], "TOPRIGHT", cellGap , cellGap);
	frame.TeleportHeading:SetPoint( "BOTTOMLEFT", addonData.TeleportButtons[1], "TOPLEFT", -cellGap , cellGap);
	frame.TeleportHeading:SetPoint( "BOTTOMRIGHT", addonData.TeleportButtons[teleCount], "TOPRIGHT", cellGap , cellGap);


	local cellWidth = ( frame:GetWidth() / cellCount );
	local btnSize = cellWidth * 1.0;
	local btnGap = cellWidth * 0.1;
	local verticalOffsetPortal =   frame:GetHeight() / 5;
	local verticalOffsetTeleport = frame:GetHeight() / 4;

	for i=1,portCount do
		self.PortalButtons[i]:SetWidth( btnSize );
		self.PortalButtons[i]:SetHeight( btnSize );
	end
	for i = 1,teleCount do
		self.TeleportButtons[i]:SetWidth( btnSize );
		self.TeleportButtons[i]:SetHeight( btnSize );
	end

	local odd = ( cellCount % 2 ) == 1;
	local midwayLeft = nil;
	local midwayRight = nil;
	if ( odd ) then
		midwayLeft = math.ceil( cellCount / 2 );
	else
		midwayLeft = math.floor( cellCount / 2 );
		midwayRight = midwayLeft + 1;
	end

	-- Reposition Portal Buttons and Fontstrings
	if ( not midwayRight ) then
		self.PortalButtons[midwayLeft]:SetPoint( "CENTER", frame, "CENTER", btnGap/2, verticalOffsetPortal);
		self.TeleportButtons[midwayLeft]:SetPoint( "CENTER", frame, "CENTER", btnGap/2, -verticalOffsetTeleport);
	else
	    self.PortalButtons[midwayLeft]:SetPoint( "RIGHT", frame, "CENTER", -btnGap/2, verticalOffsetPortal );
		self.PortalButtons[midwayRight]:SetPoint( "LEFT", frame, "CENTER", btnGap/2, verticalOffsetPortal );
	    self.TeleportButtons[midwayLeft]:SetPoint( "RIGHT", frame, "CENTER", -btnGap/2, -verticalOffsetTeleport );
		self.TeleportButtons[midwayRight]:SetPoint( "LEFT", frame, "CENTER", btnGap/2, -verticalOffsetTeleport );
	end
	for i = 1, midwayLeft - 1 do
		self.PortalButtons[i]:SetPoint( "RIGHT", self.PortalButtons[i+1], "LEFT", -btnGap, 0 );
		self.TeleportButtons[i]:SetPoint( "RIGHT", self.TeleportButtons[i+1], "LEFT", -btnGap, 0 );
	end
	for i = midwayLeft + 1, portCount do
		self.PortalButtons[i]:SetPoint( "LEFT", self.PortalButtons[i-1], "RIGHT", btnGap, 0 );
	end
	for i = midwayLeft + 1, teleCount do
		self.TeleportButtons[i]:SetPoint( "LEFT", self.TeleportButtons[i-1], "RIGHT", btnGap, 0 );
	end

	return frame
end


addonData.CreateFrame = function(self)
	if ( IsAddOnLoaded("nUI") ) then
		self.Options.HasBorder = false
		self.Options.IsMovable = false
	end

	local frame = self:BuildFrame("XMPFrame",UIParent,self.Options)		-- Changed this from addonData: to self:

	frame = self:CreateButtons(frame)

	frame = self:CreateText(frame)

	frame = self:ArrangeLayout(frame)

	return frame

end