--[[****************************************************************************
  * _NPCScan.Overlay by Saiket                                                 *
  * Modules/Carbonite.lua - Modifies the WorldMap and Minimap modules for      *
  *   compatibility with Carbonite.                                            *
  ****************************************************************************]]


if ( not IsAddOnLoaded( "Carbonite" ) ) then
	return;
end


local CarboniteVersion = GetAddOnMetadata("Carbonite", "Version"):match("^([%d.]+)");
	--_NPCScanOverlayKey:SetMovable(false)
	--_NPCScanOverlayKey:SetScript("OnMouseDown",nil )
	--_NPCScanOverlayKey:SetScript("OnMouseUp", nil )
local FOLDER_NAME, private = ...
L = private.L

local function OverlayToggle()
			-- for LeftButton, toggle the module Enabled or Disabled
			if( IsShiftKeyDown() ) then
				NSO_KeyToggleOnClick()
			else
								-- if Control Key down, toggle stuff on Main World Map
				if ( private.Options.Modules[ "WorldMap" ] ) then
					private.Modules.Disable( "WorldMap" );
				else
					private.Modules.Enable( "WorldMap" );
				end
			end
end


if CarboniteVersion < "5.4.0" then
--Code for users running older version of carbonite

if ( NxData.NXGOpts.MapMMOwn ) then -- Minimap docked into WorldMap
	private.Modules.Unregister( "Minimap" );
end
local WorldMap = private.Modules.List[ "WorldMap" ];
if ( not ( WorldMap and WorldMap.Registered ) ) then
	return;
end

local CarboniteMap = NxMap1.NxMap;
local panel = CreateFrame( "Frame", nil, WorldMap );
private.Modules.Carbonite = panel;




--- Repositions the canvas as the Carbonite map moves.
function panel:OnUpdate ()
	CarboniteMap:ClipZoneFrm( CarboniteMap.Cont, CarboniteMap.Zone, WorldMap );
	WorldMap.RangeRing.Child:SetScale( WorldMap:GetScale() ); -- CarboniteMap:CZF also sets point
	WorldMap.KeyParent:SetAlpha( NxMap1.NxWin.BackgndFade ); -- Obey window's "Fade Out" setting
	WorldMap.KeyParent:SetScale( NxMap1:GetEffectiveScale() )
	WorldMap.KeyParent:SetFrameStrata("High");
end

--- Adjusts the canvas when leaving Carbonite mode to view the default WorldMap.
function panel:WorldMapFrameOnShow ()
	if ( WorldMap.Loaded ) then
		panel:Hide(); -- Stop updating with Carbonite

		-- Undo Carbonite scaling/fading
		WorldMap:SetScale( 1 );
		WorldMap.RangeRing.Child:SetScale( 1 );
		WorldMap.KeyParent:SetAlpha( 1 );

		WorldMap:SetParent( WorldMapDetailFrame );
		WorldMap:SetAllPoints();

		WorldMap.KeyParent:SetParent( WorldMapButton );
		WorldMap.KeyParent:SetAllPoints();
		WorldMap.KeyParent:SetFrameStrata("High");

		WorldMap.RangeRing:SetParent( WorldMapDetailFrame );
		WorldMap.RangeRing:SetAllPoints();
		WorldMap.RangeRingSetTarget( WorldMapPlayerUpper );
	end
end

--- Adjusts the canvas when entering Carbonite mode.
function panel:WorldMapFrameOnHide ()
	if ( WorldMap.Loaded ) then
		panel:Show(); -- Begin updating with Carbonite
		local ScrollFrame = CarboniteMap.TextScFrm;
		WorldMap:SetParent( ScrollFrame:GetScrollChild() );
		WorldMap:ClearAllPoints();

		WorldMap.KeyParent:SetParent( ScrollFrame );
		WorldMap.KeyParent:SetAllPoints();
		WorldMap.KeyParent:SetFrameStrata("High");

		WorldMap.RangeRing:SetParent( ScrollFrame );
		WorldMap.RangeRing:SetAllPoints();
		WorldMap.RangeRingSetTarget( CarboniteMap.PlyrFrm );
	end
end

local function OnUnload ()
	panel.OnUpdate = nil;
	if ( WorldMap.Loaded ) then
		panel:SetScript( "OnUpdate", nil );
	end
end

local function OnLoad ()
	panel:SetScript( "OnUpdate", panel.OnUpdate );

	-- Give the canvas an explicit size so it paints correctly in Carbonite mode
	WorldMap:SetSize( WorldMapDetailFrame:GetSize() );

	-- Hooks to swap between Carbonite's map mode and the default UI map mode
	WorldMapFrame:HookScript( "OnShow", panel.WorldMapFrameOnShow );
	WorldMapFrame:HookScript( "OnHide", panel.WorldMapFrameOnHide );
	panel[ WorldMapFrame:IsVisible() and "WorldMapFrameOnShow" or "WorldMapFrameOnHide" ]( WorldMapFrame );
	--adds 
		Nx.Button.TypeData["OverlayToggle"] ={
		Up = "$INV_Misc_EngGizmos_20",
		Dn = "$INV_Misc_EngGizmos_20",
		SizeUp = 22,
		SizeDn = 22,
	}
		CarboniteMap.ToolBar:AddButton("OverlayToggle", L.BUTTON_TOOLTIP_LINE2.."\n"..L.BUTTON_TOOLTIP_LINE3, nil, OverlayToggle, false)
		CarboniteMap.ToolBar:Update()
		CarboniteMap:UpdateToolBar()
	
end

--- Sets a module's handler, or hooks the old one if it exists.
local function HookHandler ( Name, Handler )
	local Backup = WorldMap[ Name ];
	WorldMap[ Name ] = not Backup and Handler or function ( ... )
		Backup( ... );
		Handler( ... );
	end;
end

if ( WorldMap.Loaded ) then
	OnLoad();
else
	HookHandler( "OnLoad", OnLoad );
end

HookHandler( "OnUnload", OnUnload );

else

--Newer carbonite 5.4 alpha 2

local WorldMap = private.Modules.List[ "WorldMap" ];
if ( not ( WorldMap and WorldMap.Registered ) ) then
	return;
end

local CarboniteMap 
local panel = CreateFrame( "Frame", nil, WorldMap );
private.Modules.Carbonite = panel;




--- Repositions the canvas as the Carbonite map moves.
function panel:OnUpdate ()	
	CarboniteMap:ClipZoneFrm( CarboniteMap.Cont, CarboniteMap.Zone, WorldMap );
	WorldMap.RangeRing.Child:SetScale( WorldMap:GetScale() ); -- CarboniteMap:CZF also sets point		
	--WorldMap.KeyParent:SetAlpha( CarboniteMap.BackgndAlpha ); -- Obey window's "Fade Out" setting				
	WorldMap.KeyParent:SetAlpha( NxMap1.NxWin.BackgndFade ); -- Obey window's "Fade Out" setting
	WorldMap.KeyParent:SetScale( NxMap1:GetEffectiveScale() )
	WorldMap.KeyParent:SetFrameStrata("High");
end




--- Adjusts the canvas when leaving Carbonite mode to view the default WorldMap.
function panel:WorldMapFrameOnShow ()
	if ( WorldMap.Loaded ) then
		panel:Hide(); -- Stop updating with Carbonite

		-- Undo Carbonite scaling/fading
		WorldMap:SetScale( 1 );
		WorldMap.RangeRing.Child:SetScale( 1 );
		WorldMap.KeyParent:SetAlpha( 1 );

		WorldMap:SetParent( WorldMapDetailFrame );
		WorldMap:SetAllPoints();

		WorldMap.KeyParent:SetParent( WorldMapButton );
		WorldMap.KeyParent:SetAllPoints();
		WorldMap.KeyParent:SetFrameStrata("High");

		WorldMap.RangeRing:SetParent( WorldMapDetailFrame );
		WorldMap.RangeRing:SetAllPoints();
		WorldMap.RangeRingSetTarget( WorldMapPlayerUpper );
	end
end
--- Adjusts the canvas when entering Carbonite mode.
function panel:WorldMapFrameOnHide ()
	if not Nx.Initialized then
		return
	end
	if ( WorldMap.Loaded ) then		
		panel:Show(); -- Begin updating with Carbonite

		local ScrollFrame = CarboniteMap.TextScFrm;
		WorldMap:SetParent( ScrollFrame:GetScrollChild() );
		WorldMap:ClearAllPoints();

		WorldMap.KeyParent:SetParent( ScrollFrame );
		WorldMap.KeyParent:SetAllPoints();
		WorldMap.KeyParent:SetFrameStrata("High");

		WorldMap.RangeRing:SetParent( ScrollFrame );
		WorldMap.RangeRing:SetAllPoints();
		WorldMap.RangeRingSetTarget( CarboniteMap.PlyrFrm );
	end
end

local function OnUnload ()
	panel.OnUpdate = nil;
	if ( WorldMap.Loaded ) then
		panel:SetScript( "OnUpdate", nil );
	end
end


local function OnLoad ()
	panel:SetScript( "OnUpdate", panel.OnUpdate );	
	-- Give the canvas an explicit size so it paints correctly in Carbonite mode
	WorldMap:SetSize( WorldMapDetailFrame:GetSize() );

	-- Hooks to swap between Carbonite's map mode and the default UI map mode
	WorldMapFrame:HookScript( "OnShow", panel.WorldMapFrameOnShow );
	WorldMapFrame:HookScript( "OnHide", panel.WorldMapFrameOnHide );	
	WorldMapFrame:RegisterEvent("ZONE_CHANGED");
	WorldMapFrame:RegisterEvent("ZONE_CHANGED_INDOORS");
	WorldMapFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA");
	WorldMapFrame:HookScript( "OnEvent", panel.WorldMapFrameEventHandler);
	panel[ WorldMapFrame:IsVisible() and "WorldMapFrameOnShow" or "WorldMapFrameOnHide" ]( WorldMapFrame );
	if CarboniteMap then
		CarboniteMap:ClipZoneFrm( CarboniteMap.Cont, CarboniteMap.Zone, WorldMap);
	
		Nx.Button.TypeData["NSO"] ={
		Up = "$INV_Misc_EngGizmos_20",
		Dn = "$INV_Misc_EngGizmos_20",
		SizeUp = 22,
		SizeDn = 22,
	}
		CarboniteMap.ToolBar:AddButton("NSO", L.BUTTON_TOOLTIP_LINE2.."\n"..L.BUTTON_TOOLTIP_LINE3, nil, OverlayToggle, false)
		CarboniteMap.ToolBar:Update()
		CarboniteMap:UpdateToolBar()
	end
end

--- Sets a module's handler, or hooks the old one if it exists.
local function HookHandler ( Name, Handler )
	local Backup = WorldMap[ Name ];
	WorldMap[ Name ] = not Backup and Handler or function ( ... )
		Backup( ... );
		Handler( ... );
	end;
end

function panel:WorldMapFrameEventHandler (event, ...)
	if ( not Nx.Initialized ) then
		return
	end
	if not CarboniteMap then		
		if ( Nx.db.profile.MiniMap.Own ) then -- Minimap docked into WorldMap
			private.Modules.Unregister( "Minimap" );
		end
		CarboniteMap = NxMap1.NxMap
		OnLoad()
	end
	if event == "ZONE_CHANGED" or event == "ZONE_CHANGED_INDOORS" or event == "ZONE_CHANGED_NEW_AREA" then				
		if (Nx.Map:IsMicroDungeon(Nx.Map:GetCurrentMapId())) then				
			WorldMap:SetAlpha(0);
			CarboniteMap:ClipZoneFrm( CarboniteMap.Cont, CarboniteMap.Zone, WorldMap);
		else
			WorldMap:SetAlpha(CarboniteMap.BackgndAlpha);
			CarboniteMap:ClipZoneFrm( CarboniteMap.Cont, CarboniteMap.Zone, WorldMap);			
		end
	end
end


if ( WorldMap.Loaded ) then
	OnLoad();
else
	HookHandler( "OnLoad", OnLoad );
end
HookHandler( "OnUnload", OnUnload );
end