local addonName, addon = ...



--[[-----------------------------------------------------------------------------
Minimap
-------------------------------------------------------------------------------]]

Minimap:SetBackdropBorderColor(nil, nil, nil, nil)

local GMiniMapAnchor = CreateFrame("Frame", "GMiniMapAnchor", UIParent)
local NSMiniMapAnchor = CreateFrame("Frame", "NSMiniMapAnchor", UIParent)

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

GMiniMapAnchor:SetHeight(132)
GMiniMapAnchor:SetWidth(140)
GMiniMapAnchor:SetMovable(true)

NSMiniMapAnchor:SetHeight(200)
NSMiniMapAnchor:SetWidth(200)
NSMiniMapAnchor:SetMovable(true)

addon:MiniMapReset()

local function NSMiniMapShape()

	Minimap:SetMaskTexture(addon.settings.NSmapMask)
end

local function GMiniMapShape()
	
	Minimap:SetMaskTexture(addon.settings.GmapMask)
end

function addon:SetGMiniMap()
	if addon.settings.NotSoMinimap == true 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)
		NSMiniMapShape()
		
		_G["GrimUIcoreArtMM"]:Hide()
	else
		Minimap:ClearAllPoints()
		
		Minimap:SetParent(GMiniMapAnchor)
		Minimap:SetPoint('CENTER', GMiniMapAnchor)
		Minimap:SetHeight(132)
		Minimap:SetWidth(140)
		Minimap:SetScale(0.95)
		
		Minimap:SetAlpha(addon.settings.GmapAlpha)
		
		Minimap:SetFrameStrata('BACKGROUND')
		Minimap:SetFrameLevel(1)
		GMiniMapShape()
		_G["GrimUIcoreArtMM"]:Show()
	
	
	end
	
end

function addon:ShowMapArt()
	if addon.settings.MapArt == true then
		addon:ShowFrame(_G["GrimUIcoreArtMM"])
	else 
		addon:HideFrame(_G["GrimUIcoreArtMM"])
	end
end


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


addon.RegisterEvent("Minimap-Initialize", 'ADDON_LOADED', function(self, event)
	addon:ShowMapArt()
	addon:SetGMiniMap()
	addon:ShowHideMiniMap()
end)

addon.RegisterEvent("Minimap-InitializeAnchors", 'PLAYER_ENTERING_WORLD', function(self, event)
	Minimap:Hide()
	GMiniMapAnchor:Hide()
	
	NSMiniMapAnchor:Hide()
	
	
	GMiniMapAnchor:Show()
	NSMiniMapAnchor:Show()
	Minimap:Show()
end)

addon.RegisterEvent("MiniMap-SaveState", 'PLAYER_LOGOUT', function()
	addon.settings.ShowMiniMap = Minimap:IsShown() or false
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 OnEnter(self)
	if self.tooltip then
		GameTooltip:ClearLines()
	else
		GameTooltip:SetOwner(self, 'ANCHOR_NONE')
		GameTooltip:SetPoint('BOTTOMLEFT', GUIAnchorPoint6)
	end
	--
	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)
	if not self.tooltip then
		GameTooltip:Show()
		self.tooltip = true
	end
end
Minimap:HookScript('OnEnter', OnEnter)
Minimap:HookScript('OnLeave', addon.HideTooltip)

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 addon.settings.MapButStyle == true then
				addon:SafeCall("MapToggleFunc")
			else
				addon:MapToggleFunc()
			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()
MiniMapLFGFrame:ClearAllPoints()
MiniMapLFGFrame:SetPoint('BOTTOMRIGHT', addon.skin, 'TOPRIGHT', -10, 250)
MiniMapLFGFrame:SetParent(UIParent)
end
addon:LFGEyeFix()

MiniMapLFGFrameBorder: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()
	MiniMapBattlefieldFrame:ClearAllPoints()
	MiniMapBattlefieldFrame:SetPoint('BOTTOMRIGHT', addon.skin, 'TOPRIGHT', -10, 250)
end
addon:MiniMapPVPFix()

--[[-----------------------------------------------------------------------------
Coords Frame
-------------------------------------------------------------------------------]]
local coordsFrame = CreateFrame('Frame', nil, UIParent)
coordsFrame:SetPoint('BOTTOM')
coordsFrame:SetHeight(11)
coordsFrame:SetWidth(80)

local coordsBG = coordsFrame:CreateTexture(nil, 'BACKGROUND')
coordsBG:SetTexture(0, 0, 0, 1)
coordsBG:SetAllPoints()

local coords = coordsFrame:CreateFontString(nil, 'OVERLAY', 'GameFontNormalSmall')
coords:SetPoint('CENTER', coordsFrame, 'CENTER', 0, 1)
coords:SetTextColor(1, 1, 1, 1)
coords:SetFont([[Fonts\FRIZQT__.TTF]], 10, 'NORMAL')
coords:SetJustifyH('CENTER')

local timer, oldX, oldY = 0
coordsFrame:SetScript('OnUpdate', function(self, elapsed)
	timer = timer + elapsed
	if timer < 0.1 then return end
	timer = 0
	local x, y = GetPlayerMapPosition('player')
	if x ~= oldX or y ~= oldY then
		coords:SetFormattedText("%.2f  %.2f", x * 100, y * 100)
		oldX, oldY = x, y
	end
end)

--[[-----------------------------------------------------------------------------
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
	elseif event == 'PLAYER_ENTERING_WORLD' then
		ShowUIPanel(WorldMapFrame)
		HideUIPanel(WorldMapFrame)
	end
end

addon.RegisterEvents("WorldMapFix", addon.WorldMapFix, 'PLAYER_ENTERING_WORLD', 'PLAYER_REGEN_DISABLED', 'PLAYER_REGEN_ENABLED')  
