local addonName, addon = ...

local GMiniMapAnchor = CreateFrame("Frame", "GMiniMapAnchor", UIParent)
local NSMiniMapAnchor = CreateFrame("Frame", "NSMiniMapAnchor", UIParent)
Minimap:SetBackdropBorderColor(nil, nil, nil, nil)

function addon:MiniMapReset()
	GMiniMapAnchor:ClearAllPoints()
	NSMiniMapAnchor:ClearAllPoints()
	GMiniMapAnchor:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 4)
	NSMiniMapAnchor:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
end

GMiniMapAnchor:SetSize(136, 140)
GMiniMapAnchor:SetMovable(true)
NSMiniMapAnchor:SetSize(200, 200)
NSMiniMapAnchor:SetMovable(true)

addon:MiniMapReset()

function addon:SetGMiniMap()
	if addon.settings.NotSoMinimap then
		Minimap:ClearAllPoints()
		Minimap:SetParent(NSMiniMapAnchor)
		Minimap:SetPoint('CENTER', NSMiniMapAnchor)
		Minimap:SetHeight(200)
		Minimap:SetWidth(200)
		Minimap:SetScale(addon.settings.NSmapScale)
		Minimap:SetAlpha(addon.settings.NSmapAlpha)
		Minimap:SetFrameStrata('BACKGROUND')
		Minimap:SetFrameLevel(1)
		Minimap:SetMaskTexture(addon.settings.NSmapMask)
		_G["GUI_ArtMinimap"]:Hide()
	else
		Minimap:ClearAllPoints()
		Minimap:SetParent(GMiniMapAnchor)
		Minimap:SetPoint('CENTER', GMiniMapAnchor)
		Minimap:SetHeight(136)
		Minimap:SetWidth(140)
		Minimap:SetScale(0.95)
		Minimap:SetAlpha(addon.settings.GmapAlpha)
		Minimap:SetFrameStrata('BACKGROUND')
		Minimap:SetFrameLevel(1)
		Minimap:SetMaskTexture(addon.settings.GmapMask)
		if addon.settings.MapArt == false then
			_G["GUI_ArtMinimap"]:Hide()
		else
			_G["GUI_ArtMinimap"]:Show()
		end
	end
end

function addon:ShowMapArt()
	if addon.settings.MapArt then
		if addon.settings.MapArt == false then
			addon:HideFrame(_G["GUI_ArtMinimap"])
		end
	end
end

function addon:ShowHideMiniMap()
	if addon.settings.ShowMiniMap == false then
		addon:HideFrame(Minimap)
		_G["GUI_ArtMinimap"]:Hide()
	else
		addon:ShowFrame(Minimap)
		if not addon.settings.NotSoMinimap or addon.settings.NotSoMinimap == false then
			_G["GUI_ArtMinimap"]:Show()
		end
	end
end

addon:HideFrame(MinimapCluster)

Minimap:SetScript('OnMouseWheel', function(self, direction)
	self:SetZoom(max(min(self:GetZoom() + direction, self:GetZoomLevels()), 0))
end)
Minimap:EnableMouseWheel(true)

local function GUI_MiniMap_OnEnter(self)
	GameTooltip:SetOwner(self, 'ANCHOR_NONE')
	GameTooltip:SetPoint('BOTTOMLEFT', GUI_AnchorPoint6)
	local pvpType, isSubZonePvP, factionName = GetZonePVPInfo();
	local zoneName = GetZoneText();
	local subzoneName = GetSubZoneText();
		if ( subzoneName == zoneName ) then
			subzoneName = "";	
		end
		GameTooltip:AddLine( zoneName, 1.0, 1.0, 1.0 );
		if ( pvpType == "sanctuary" ) then
			GameTooltip:AddLine( subzoneName, 0.41, 0.8, 0.94 );	
			GameTooltip:AddLine(SANCTUARY_TERRITORY, 0.41, 0.8, 0.94);
		elseif ( pvpType == "arena" ) then
			GameTooltip:AddLine( subzoneName, 1.0, 0.1, 0.1 );	
			GameTooltip:AddLine(FREE_FOR_ALL_TERRITORY, 1.0, 0.1, 0.1);
		elseif ( pvpType == "friendly" ) then
			GameTooltip:AddLine( subzoneName, 0.1, 1.0, 0.1 );	
			GameTooltip:AddLine(format(FACTION_CONTROLLED_TERRITORY, factionName), 0.1, 1.0, 0.1);
		elseif ( pvpType == "hostile" ) then
			GameTooltip:AddLine( subzoneName, 1.0, 0.1, 0.1 );	
			GameTooltip:AddLine(format(FACTION_CONTROLLED_TERRITORY, factionName), 1.0, 0.1, 0.1);
		elseif ( pvpType == "contested" ) then
			GameTooltip:AddLine( subzoneName, 1.0, 0.7, 0.0 );	
			GameTooltip:AddLine(CONTESTED_TERRITORY, 1.0, 0.7, 0.0);
		elseif ( pvpType == "combat" ) then
			GameTooltip:AddLine( subzoneName, 1.0, 0.1, 0.1 );	
			GameTooltip:AddLine(COMBAT_ZONE, 1.0, 0.1, 0.1);
		else
			GameTooltip:AddLine( subzoneName, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b );	
		end
	GameTooltip:AddLine(" ")
	GameTooltip:AddLine("|cffeda55fLeftClick|r to Ping Map.", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fRightClick|r toggle WorldMap", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fCtrl+RightClick|r for Tracking Menu", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fAlt+LeftClick|r toggle Battlefield MiniMap", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fAlt+RightClick|r toggle Not So MiniMap!", 0.2, 1, 0.2)
	GameTooltip:Show()
end

local function GUI_MinimapPOITipFix(self, elapsed)
    if GameTooltip:IsShown() then
        return
    end
    GUI_MiniMap_OnEnter(self)
end

Minimap:HookScript("OnEnter", function(self)
    GUI_MiniMap_OnEnter(self)
	Minimap:HookScript("OnUpdate", GUI_MinimapPOITipFix)
end)

Minimap:HookScript("OnLeave", function(self)
    GameTooltip:Hide()
	Minimap:SetScript("OnUpdate", nil)
end)

function addon:MapToggleFunc()
	if WorldMapFrame:IsVisible() then
		HideUIPanel(WorldMapFrame)
	else
		ShowUIPanel(WorldMapFrame)
	end
end

Minimap:SetScript('OnMouseUp', function(self, button)
	if button == 'RightButton' then
		if IsControlKeyDown() then
			ToggleDropDownMenu(1, nil, MiniMapTrackingDropDown, self)
		elseif IsAltKeyDown() then
			if addon.settings.NotSoMinimap == false then
				addon.settings.NotSoMinimap = true
			elseif addon.settings.NotSoMinimap == true then
				addon.settings.NotSoMinimap = false
			end
			addon:SetGMiniMap()
		else
			if not addon.settings.MapButStyle or addon.settings.MapButStyle == 1 then
				addon:SafeCall("MapToggleFunc")
			elseif addon.settings.MapButStyle == 2 then
				addon:MapToggleFunc()
			elseif addon.settings.MapButStyle == 3 then
				ToggleFrame(WorldMapFrame)
			end
		end
	elseif button == 'LeftButton' then
		if IsAltKeyDown() then
			ToggleBattlefieldMinimap()
		else
			Minimap_OnClick(Minimap)
		end
	end
end)

--[[-----------------------------------------------------------------------------
Minimap Dungeon Dificulty and LFG Buttons
-------------------------------------------------------------------------------]]
function addon:LFGEyeFix()
	QueueStatusMinimapButton:ClearAllPoints()
	QueueStatusMinimapButton:SetPoint('BOTTOMRIGHT', addon.skin, 'TOPRIGHT', -10, 250)
	QueueStatusMinimapButton:SetParent(UIParent)
end
addon:LFGEyeFix()

--QueueStatusMinimapButton:SetAlpha(0) -- Check this!!??

local function InstanceDifficultyFlag()
	MiniMapInstanceDifficulty:ClearAllPoints()
	MiniMapInstanceDifficulty:SetParent(UIParent)
	MiniMapInstanceDifficulty:SetPoint('BOTTOMRIGHT', addon.skin, 'TOPRIGHT', -10, 210)
	MiniMapInstanceDifficulty.SetParent = addon.DoNothing
	MiniMapInstanceDifficulty.SetPoint = addon.DoNothing
end
InstanceDifficultyFlag()

--[[-----------------------------------------------------------------------------
BattleGround/PVP Frame
-------------------------------------------------------------------------------]]
function addon:MiniMapPVPFix()
	BattlefieldMinimap:ClearAllPoints()
	BattlefieldMinimap:SetPoint('BOTTOMRIGHT', addon.skin, 'TOPRIGHT', -10, 250)
end
--addon:MiniMapPVPFix()

--[[-----------------------------------------------------------------------------
Coords Frame
-------------------------------------------------------------------------------]]
local GUI_CoordsFrame = CreateFrame('Frame', "GUI_CoordsFrame", UIParent)
GUI_CoordsFrame:SetPoint('BOTTOM')
GUI_CoordsFrame:SetHeight(11)
GUI_CoordsFrame:SetWidth(80)
local GUI_CoordsBG = GUI_CoordsFrame:CreateTexture(nil, 'BACKGROUND')
GUI_CoordsBG:SetTexture(0, 0, 0, 1)
GUI_CoordsBG:SetAllPoints()
local GUI_CoordsTxt = GUI_CoordsFrame:CreateFontString("GUI_CoordsTxt","OVERLAY")
GUI_CoordsTxt:SetPoint('CENTER', GUI_CoordsFrame, 'CENTER', 0, 0)
GUI_CoordsTxt:SetFont([[Fonts\FRIZQT__.TTF]], 11)
GUI_CoordsTxt:SetTextColor(1, 1, 1, 1)
GUI_CoordsTxt:SetJustifyH('CENTER')
GUI_CoordsTxt:SetText("n/a");
GUI_CoordsFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
GUI_CoordsFrame:RegisterEvent("ZONE_CHANGED")
GUI_CoordsFrame:RegisterEvent("ZONE_CHANGED_INDOORS")

GUI_CoordsFrame:HookScript("OnUpdate",function(self)
	local x,y=GetPlayerMapPosition("player");
	if x>0 or y>0 then
		GUI_CoordsTxt:SetText(string.format("%0.2f - %0.2f",x*100, y*100));
	else
		GUI_CoordsTxt:SetText("0.0 - 0.0");
	end
end);

-- Come back and change the bliz function for worldmap buttons in order to fix the cords reading off when you have the worldmap open and view a zone other then the one you are currently in.
GUI_CoordsFrame:SetScript("OnEvent",function(self,event,...)
	if GameTooltip:GetOwner() == Minimap then
		GUI_MiniMap_OnEnter(Minimap)
	end
	if event == "ZONE_CHANGED_NEW_AREA" then
		addon:FuncDelay(.04, SetMapToCurrentZone)
	end
end)

WorldMapFrame:HookScript("OnShow",SetMapToCurrentZone);

--[[-----------------------------------------------------------------------------
Watch frame set default position and improve behavior with MoveFrames
-------------------------------------------------------------------------------]]
function addon:setupWatchFrame()
	WatchFrame:ClearAllPoints()
	WatchFrame:SetPoint('TOPLEFT', UIParent, 28, 0)
	addon:LockFrame(WatchFrame)
end
addon:setupWatchFrame()

local processing
function addon:AdjustWatchFrameHeight()
	if processing then return end
	processing = true
	WatchFrame:SetHeight(min(WatchFrame:GetTop(), UIParent:GetHeight() / 2))
	processing = nil
end

local function AdjustHeight()
	addon:SafeCall("AdjustWatchFrameHeight")
end

WatchFrame:HookScript('OnMouseUp', AdjustHeight)
WatchFrameHeader:HookScript('OnMouseUp', AdjustHeight)
WatchFrame:HookScript('OnSizeChanged', AdjustHeight)

hooksecurefunc('WatchFrame_Collapse', function()
	WatchFrame:EnableMouse(false)
end)

hooksecurefunc('WatchFrame_Expand', function()
	WatchFrame:EnableMouse(true)
end)

--[[-----------------------------------------------------------------------------
WorldMapBlobFrame and combat taint on WorldMap toggle fix
-------------------------------------------------------------------------------]]
local currentTrackState
function addon.WorldMapFix(frame, event)
	if event == 'PLAYER_REGEN_DISABLED' then
		if addon.settings.MapButStyle == false then
			currentTrackState = WorldMapQuestShowObjectives:GetChecked()
			HideUIPanel(WorldMapFrame)
			WorldMap_ToggleSizeDown()
			WatchFrame.showObjectives = nil
			WorldMapQuestShowObjectives:SetChecked(false)
			addon:HideFrame(WorldMapBlobFrame)
			addon:HideFrame(WorldMapFrameSizeUpButton)
			addon:HideFrame(WorldMapPOIFrame)
			addon:HideFrame(WorldMapQuestShowObjectives)
			addon:HideFrame(WorldMapTitleButton)
			addon:HideFrame(WorldMapTrackQuest)
			WatchFrame_Update()
		end
	elseif event == 'PLAYER_REGEN_ENABLED' then
		if addon.settings.MapButStyle == false then
			addon:ShowFrame(WorldMapBlobFrame)
			addon:ShowFrame(WorldMapFrameSizeUpButton)
			addon:ShowFrame(WorldMapPOIFrame)
			addon:ShowFrame(WorldMapQuestShowObjectives)
			addon:ShowFrame(WorldMapTitleButton)
			addon:ShowFrame(WorldMapTrackQuest)
			if currentTrackState then
				WatchFrame.showObjectives = true
				WorldMapQuestShowObjectives:SetChecked(true)
				WatchFrame_Update()
			else
				WatchFrame.showObjectives = nil
				WorldMapQuestShowObjectives:SetChecked(false)
			end
		end
	end
	if event == 'PLAYER_LOGIN' then
		ShowUIPanel(WorldMapFrame)
		HideUIPanel(WorldMapFrame)
	end
end
addon.RegisterEvents("WorldMapFix", addon.WorldMapFix, 'PLAYER_LOGIN', 'PLAYER_REGEN_DISABLED', 'PLAYER_REGEN_ENABLED')  

addon.RegisterEvent("Minimap-InitializeAnchors", 'PLAYER_LOGIN', function(self, event)
	addon.UnregisterEvent(self, event)
	addon:SetGMiniMap()
	
	Minimap:Hide()
	GMiniMapAnchor:Hide()
	NSMiniMapAnchor:Hide()
	
	GMiniMapAnchor:Show()
	NSMiniMapAnchor:Show()
	Minimap:Show()
	
	addon:ShowHideMiniMap()
	
	SetMapToCurrentZone(); 
end)

addon.RegisterEvent("MiniMap-SaveState", 'PLAYER_LOGOUT', function()
	addon.settings.ShowMiniMap = Minimap:IsShown() or false
end)