local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local events = child['events']
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local db
local delayOnUpdate = .5
LibStub("zzHelper"):Embed(child)
local function prepareDB()
	db = addon['db']['profile'][childName]
	child['db'] = db
	addon['callbacks']:Fire(format("Init%s", childName))
	child:ZoneEvent()
end
local function setPlayerCoords()
	local playerCoords = "-, -"
	local mapID = C_Map.GetBestMapForUnit("player")
	if(mapID) then
		local pmp = C_Map.GetPlayerMapPosition(mapID,"player")
		if(pmp) then
			local x,y = pmp:GetXY()
			if(x and y) then
				playerCoords = format("%.1f, %.1f", x*100, y*100)
			end
		end
	end
	child['playerCoords'] = playerCoords
end
local colorlist = {
	['friendly'] = "00ff00",
	['arena'] = "ffff00",
	['sanctuary'] = "9999ff",
}
local function pvpColor(txt)
	local col
	local pvpType, isFFA, faction = GetZonePVPInfo()
	if(colorlist[pvpType]) then
		col = colorlist[pvpType]
	elseif(pvpType == "hostile" or pvpType == "combat" or pvpType=="contested") then
		col = faction == select(2,UnitFactionGroup('player')) and "886600" or "ff0000"
	else
		return txt
	end
	return child:colorize(txt, col)
end
local function OnDataUpdate()
	setPlayerCoords()
	if(child['zoneText'] and child['zoneText']~="") then
		local coordsline = db['hideLDBCoords'] and "%s" or "%s (%s)"
		child:OnText(childName, format(coordsline,(db['colorLDB'] and pvpColor(child['zoneText']) or child['zoneText']) or "", child['playerCoords'] or ""))
	else
		if(db['hideLDBCoords']) then
			child:OnText(childName, "")
		else
			child:OnText(childName, child['playerCoords'] or "")
		end
	end
	if(child['moving']) then
		C_Timer.After(delayOnUpdate, OnDataUpdate)
	end
end

local function zzCoords_MinimapToggle()
	if(MinimapCluster:IsVisible()) then
		MinimapCluster:Hide()
	else
		MinimapCluster:Show()
	end
end
local function limitLength(info, maxlength)
	if(info and maxlength) then
		if(maxlength > 0 and info:len() > maxlength) then
			info = format("%s~",info:sub(1,maxlength))
		end
	end
	return info
end
function child:ZoneEvent()
	local zoneText = db['ShowZone'] and GetRealZoneText() or ""
	if(db['ShowSubZone']) then
		if(GetSubZoneText() and GetSubZoneText()~="") then
			if(db['ShowZone']) then
				zoneText = zoneText ~= GetSubZoneText() and format("%s, %s",zoneText,GetSubZoneText()) or zoneText
			else
				zoneText = GetSubZoneText()
			end
		elseif(not db['ShowZone']) then
			zoneText = GetRealZoneText()
		end
	end
	child['zoneText'] = limitLength(zoneText, db['maxLength'])
	if(not db['hideMapCoords']) then
		child['mapframe']:Show()
	else
		child['mapframe']:Hide()
	end
	OnDataUpdate()
end

function child:MoveEvent(lib, event)
	child['moving'] = event == "PLAYER_STARTED_MOVING" and true or nil
	OnDataUpdate()
end
child['specs']  = {
	['name'] = childName,
	['sv'] = true,
	['cfg'] = true,
	['cfgReset'] = prepareDB,
	['ldb'] = child['ldb'],
	['defaults'] = {
		['profile'] = {
			[childName] = {
				["colorLDB"] = true,
				["ShowZone"] = false,
				["ShowSubZone"] = true,
				["ShowTooltip"] = false,
				["windowX"] = 10,
				["windowY"] = 5
			}
		}
	}
}
function events:PLAYER_ENTERING_WORLD(event)
	child:UnregisterEvent('PLAYER_ENTERING_WORLD')
	local m = CreateFrame("BUTTON", "zzCoords_MinimapToggleFrame")
	m:SetScript("OnClick", zzCoords_MinimapToggle)
	if(db['toggleMap'] and db['toggleMap'] ~= '') then
		SetBinding(db['toggleMap'], "CLICK zzCoords_MinimapToggleFrame:LeftButton")
	end
	child:RegisterEvent({"ZONE_CHANGED_NEW_AREA","ZONE_CHANGED","ZONE_CHANGED_INDOORS","NEW_WMO_CHUNK"}, child.ZoneEvent)
	child:RegisterEvent({"PLAYER_STARTED_MOVING", "PLAYER_STOPPED_MOVING"}, child.MoveEvent)
end
child:NewAddOn(...)