local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local db
local zzDataProvider = {};
local coordText,db,mapframe

function zzDataProvider:GetMap()
	return self.owningMap;
end
function child:GetXY()
	local x, y = WorldMapFrame.ScrollContainer:GetNormalizedCursorPosition();
	local adjustedX = x * 100
	local adjustedY = y * 100
	return adjustedX,adjustedY
end
local function update()
	if(not db) then return end
	if(coordText) then
		db['screenX'] = db['screenX'] or 10
		db['screenY'] = db['screenY'] or 5
		db['windowX'] = db['windowX'] or 10
		db['windowY'] = db['windowY'] or 5
		local x,y = child:GetXY()
		local str = ""
		local mapID = zzDataProvider:GetMap():GetMapID()
		if(mapID and db['ShowMapID']) then
			str = format("%s MapID: %i",str,mapID)
		end
		if(not(x == 100 or y==100 or x==0 or y==0)) then
			str = format("%s Mouse: %.1f / %.1f",str,x,y)
		end
		if(mapID) then
			local pmp = C_Map.GetPlayerMapPosition(mapID,"player")
			if(pmp) then
				local x,y = pmp:GetXY()
				if(x and y) then
					str = format("%s Player: %.1f / %.1f",str,x * 100,y * 100)
				end
				pmp = nil
			end
		end
		coordText:SetText(str)
		if(not db['hideMapCoords']) then
			mapframe:SetWidth(coordText:GetStringWidth()+4)
			mapframe:SetPoint("BOTTOMLEFT", WorldMapFrame, "BOTTOMLEFT", db['windowX'], db['windowY'])
			mapframe:Show()
		else
			mapframe:Hide()
		end

	end
end
function zzDataProvider:OnAdded(owningMap)
	self.owningMap = owningMap;
	if(not coordText or not mapframe) then
		mapframe = CreateFrame("Frame", "zz_CoordsMapFrame", WorldMapFrame, BackdropTemplateMixin and "BackdropTemplate" or nil)
		mapframe:SetWidth(600)
		mapframe:SetHeight(20)
		mapframe:SetFrameStrata("HIGH")
		mapframe:SetBackdrop({	
			bgFile = "Interface/Tooltips/UI-Tooltip-Background",
		})
		mapframe:SetBackdropColor(0,0,0,1);
		mapframe.elapsed = 0
		mapframe:SetScript("OnEnter", function(self)
			self:SetBackdropColor(0,0,0,0);
			coordText:Hide()
		end)
		mapframe:SetScript("OnLeave", function(self)
			coordText:Show()
			self:SetBackdropColor(0,0,0,1);
		end)
		mapframe:EnableMouse(1)
		coordText = mapframe:CreateFontString(nil, "OVERLAY","GameFontNormal")
		coordText:SetAllPoints(mapframe)
		coordText:SetJustifyH("LEFT")
		coordText:SetShadowColor(1,1,1,.2)
		coordText:SetShadowOffset(1,-1)
		mapframe:SetScript("OnUpdate", function(self, elapsed)
			self.elapsed = self.elapsed + elapsed
			if(self.elapsed<0.2) then return end
			self.elapsed = 0
			update()
		end)
		child['mapframe'] = mapframe
	end
end

function zzDataProvider:OnRemoved(owningMap)
	assert(owningMap == self.owningMap);
	self.owningMap = nil;
	if self.registeredEvents then
		for event in pairs(self.registeredEvents) do
			owningMap:UnregisterEvent(event);
		end
		self.registeredEvents = nil;
	end
end

function zzDataProvider:RemoveAllData()
	if(mapframe) then
		mapframe:Hide()
		mapframe = nil
		coordText = nil
	end
end

function zzDataProvider:RefreshAllData()
	update()
end

function zzDataProvider:OnShow()
end

function zzDataProvider:OnHide()
end

function zzDataProvider:OnMapInsetSizeChanged(mapInsetIndex, expanded)
end

function zzDataProvider:OnMapInsetMouseEnter(mapInsetIndex)
end

function zzDataProvider:OnMapInsetMouseLeave(mapInsetIndex)
end

function zzDataProvider:OnCanvasScaleChanged()
end

function zzDataProvider:OnCanvasPanChanged()
end

function zzDataProvider:OnCanvasSizeChanged()
end

function zzDataProvider:OnEvent(event, ...)
end

function zzDataProvider:OnGlobalAlphaChanged()
end

function zzDataProvider:OnMapChanged()
	self:RefreshAllData()
end

function zzDataProvider:RegisterEvent(event)
	if not self.registeredEvents then
		self.registeredEvents = {}
	end
	if not self.registeredEvents[event] then
		self.registeredEvents[event] = true;
		self:GetMap():AddDataProviderEvent(event);
	end
end

function zzDataProvider:UnregisterEvent(event)
	if self.registeredEvents and self.registeredEvents[event] then
		self.registeredEvents[event] = nil;
		self:GetMap():RemoveDataProviderEvent(event);
	end
end

function zzDataProvider:SignalEvent(event, ...)
	if self.registeredEvents and self.registeredEvents[event] then
		self:OnEvent(event, ...);
	end
end
if(type(WorldMapFrame.AddDataProvider) == 'function') then
	WorldMapFrame:AddDataProvider(zzDataProvider)
else
	zzDataProvider:OnAdded()
end
local init = {
	Setup = function(self)
		db = child['db']
	end
}
addon.RegisterCallback(init, format("Init%s", childName), 'Setup')