Quantcast
-- Grab a copy of the local data
local addonName, addonData = ...
local Translate = addonData.Translate


addonData.CreateInfopanel = function(self)

	-- If nUI isn't loaded then don't worry about this part
	if ( not IsAddOnLoaded("nUI")) then return nil end

	-- Make sure we have access to the nUI tables
	if not nUI_InfoPanels then nUI_InfoPanels = {}; end

	-- Allocate a unique Infopanel ID
	nUI_INFOMODE_MAGEPORTS  = 18;
	nUI_INFOPANEL_MAGEPORTS = "nUI_InfoPanel_XMagePorts";

	-- Set the Infopanel Table values
	nUI_InfoPanels[nUI_INFOPANEL_MAGEPORTS ] =
	{
		enabled   = true,
		desc      = Translate["MAGEPORTS_DESC"],            -- player friendly name/description of the panel
		label     = Translate["MAGEPORTS_LABEL"],	       	-- label to use on the panel selection button face
		rotation  = nUI_INFOMODE_MAGEPORTS,            		-- index or position this panel appears on/in when clicking the selector button
		full_size = true;                              		-- this plugin requires the entire info panel port without the button bag

		options  =
		{
			enabled  = true,
		},
	};

	-- Initialise the Infopanel Plugin Frame
	local plugin    = CreateFrame( "Frame", nUI_INFOPANEL_MAGEPORTS, nUI_Dashboard.Anchor );
	plugin.active   = true;

	-- Infopanel Plugin Event Management
	local function onInfoPanelEvent(self,event,...)
		local arg1 = ...;
		if event == "VARIABLES_LOADED" then
			InfoPanel.active = true;
		end
	end

	plugin:SetScript( "OnEvent", onInfoPanelEvent );
	plugin:RegisterEvent( "VARIABLES_LOADED" );

	-- Initialisation of Infopanel plugin
	plugin.initPanel = function( container, options )
		plugin.container = container;
		plugin.options   = options;
		if options and options.enabled then
			plugin.setEnabled( true );
		end
	end

	-- SizeChange of Infopanel plugin
	plugin.sizeChanged = function( scale, height, width )
		local options  = plugin.options;
		plugin.scale = scale;

		-- Grab addon information and unlock frame for movement
		local MagePortsFrame   = plugin.MagePorts;
		nUI_Movers:lockFrame( MagePortsFrame, false, nil );

		-- Resize and Reposition according to scale and size of new container
		MagePortsFrame:SetWidth( width );
		MagePortsFrame:SetHeight( height );

		-- Rearrange Icons as needed
		addonData:ArrangeLayout(MagePortsFrame)

		-- Lock addon frame for movement
		nUI_Movers:lockFrame( MagePortsFrame, true, nil );
	end

	-- Enabling of the Infopanel plugin
	plugin.setEnabled = function( enabled )
		if plugin.enabled ~= enabled then
			plugin.enabled = enabled;
			if not enabled then
				local MagePorts = plugin.MagePorts;
				local MagePortsOptions = plugin.MagePortsOptions
				if MagePorts.saved_parent then
					nUI_Movers:lockFrame( MagePorts, false, nil );
					MagePortsOptions:SetParent( UIParent );
					MagePorts:SetParent( MagePorts.saved_parent );
					MagePorts:SetBackdropBorderColor( MagePorts.border_color );
					MagePorts:SetBackdropColor( MagePorts.backdrop_color );
				end
			else
				local MagePorts = addonData.XMPFrame;
				local MagePortsOptions = addonData.OptionsButton
				plugin.MagePorts = MagePorts;
				plugin.MagePortsOptions = MagePortsOptions
				if not MagePorts.saved_parent then
					MagePorts.saved_parent   = MagePorts:GetParent();
					MagePorts.border_color   = MagePorts:GetBackdropBorderColor();
					MagePorts.backdrop_color = MagePorts:GetBackdropColor();
				end
				MagePortsOptions:SetParent( plugin.container );
				MagePortsOptions:SetPoint( "TOP", plugin.container, "TOP", 0, 20 );
				MagePorts:SetParent( plugin.container );
				MagePorts:SetPoint( "TOPLEFT", plugin.container, "TOPLEFT", 0, -15 );
				MagePorts:SetPoint( "BOTTOMRIGHT", plugin.container, "BOTTOMRIGHT", 0, 5 );
				MagePorts:SetFrameStrata( plugin.container:GetFrameStrata() );
				MagePorts:SetFrameLevel( plugin.container:GetFrameLevel()+1 );
				MagePorts:SetBackdropBorderColor( 0, 0, 0, 0 );
				MagePorts:SetBackdropColor( 0, 0, 0, 0 );

				nUI_Movers:lockFrame( MagePorts, true, nil );
			end
		end
	end

	-- Selection of the Infopanel plugin frame
	plugin.setSelected = function( selected )
		if selected ~= plugin.selected then
			plugin.selected = selected;
			if selected then
			else
			end
		end
	end

	return plugin;

end