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(2)
		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(2)
		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
			ToggleFrame(WorldMapFrame)
		end
	elseif button == 'LeftButton' then
		if IsAltKeyDown() then
			ToggleBattlefieldMinimap()
		else
			Minimap_OnClick(Minimap)
		end
	end
end)

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

function addon:SkinLFGEye()
	QueueStatusMinimapButtonBorder:SetTexture(nil)
	local LFGEyeArt = CreateFrame("Frame", "LFGEyeArt", QueueStatusMinimapButton)
	LFGEyeArt:SetPoint('CENTER', QueueStatusMinimapButton, 'CENTER', 0, 0)
	LFGEyeArt:SetHeight(1)
	LFGEyeArt:SetWidth(1)
	LFGEyeArt:SetFrameStrata("BACKGROUND")
	LFGEyeArt.texture = LFGEyeArt:CreateTexture()
	LFGEyeArt.texture:SetTexture("Interface\\AddOns\\GrimUI\\Media\\RFDragon")
	LFGEyeArt.texture:SetPoint('CENTER', LFGEyeArt, 'CENTER')
end
addon:SkinLFGEye()

function addon:GarrisonButtonFix()
	GarrisonLandingPageMinimapButton:ClearAllPoints()
	GarrisonLandingPageMinimapButton:SetPoint('TOPRIGHT', UIParent, 'TOPRIGHT', -10, -10)
	GarrisonLandingPageMinimapButton:SetParent(UIParent)
end
addon:GarrisonButtonFix()
--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 / ObjectiveTracker set default position and improve behavior with MoveFrames
-------------------------------------------------------------------------------]]
function addon:setupWatchFrame()
	ObjectiveTrackerFrame:ClearAllPoints()
	ObjectiveTrackerFrame:SetPoint('TOPLEFT', UIParent, 28, 0)
	addon:LockFrame(ObjectiveTrackerFrame)
end
addon:setupWatchFrame()

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

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

ObjectiveTrackerFrame:HookScript('OnMouseUp', AdjustHeight)
ObjectiveTrackerBlocksFrame.QuestHeader:HookScript('OnMouseUp', AdjustHeight)
ObjectiveTrackerFrame:HookScript('OnSizeChanged', AdjustHeight)

--hooksecurefunc('ObjectiveTrackerBlocksFrame_Collapse', function()
	--ObjectiveTrackerFrame:EnableMouse(false)
--end)

--hooksecurefunc('ObjectiveTrackerBlocksFrame_Expand', function()
	--ObjectiveTrackerFrame:EnableMouse(true)
--end)



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)