--[=[
--	Module Name: GPS
--	Purpose: To display the Zone and Co-ordinates of the players current location
--	Version: 0.1
--]=]

local tooltip = nil

-- Update frequency 1 second.
local UPDATEPERIOD = 2

local GPSEvents = {
	"PLAYER_LOGIN",
	"ZONE_CHANGED",
	"ZONE_CHANGED_INDOORS",
	"ZONE_CHANGED_NEW_AREA",
	"PLAYER_ENTERING_WORLD",
	"ADDON_LOADED"
}

local gpsLoc = {
	zone = " ",
	color = "white",
	pvp = "contested",
}

local C = Broker_Everything.color

local f = CreateFrame("frame")

-- Function to get the GPS co-ordinates
local function getCoordinates()
	local location = ""
	local x, y = GetPlayerMapPosition("player")

	if x ~= 0 and y ~= 0 then
		location = string.format("%.1f, %.1f", x * 100, y * 100)
	end

	return location
end

-- Function to get the current Zone or subzone
local function getZone()
	local subZone = GetSubZoneText() or ""
	local zone = GetRealZoneText() or ""

	if subZone == nil  or subZone == "" then
		return zone
	else
		return subZone
	end
end

-- Function ot return the pvp zone information and the related color to color text.
local function getPVP()
	local p, _, f = GetZonePVPInfo()
	local color = "white"

	if p == "combat" or p == "arena" or p == "hostile" then
		color = "red"
	elseif p == "contested" or p == nil then
		color = "dkyellow"
		p = "contested"
	elseif p == "friendly" then
		color = "dkgreen"
	elseif p == "sanctuary" then
		color = "ltblue"
	end

	return p, color
end

-- Event Handler
local function gps_OnEvent(self, event)
	if (event == "PLAYER_LOGIN") or (event == "ZONE_CHANGED") or (event == "ZONE_CHANGED_INDOORS") or (event == "ZONE_CHANGED_NEW_AREA") or (event == "PLAYER_ENTERING_WORLD") or (event == "ADDON_LOADED") then
		local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName("GPS")
		gpsLoc.zone = getZone()
		gpsLoc["pvp"], gpsLoc["color"] = getPVP()
		dataobj.text = C[gpsLoc.color](string.format("%s (%s)", gpsLoc.zone, getCoordinates()))	
	end
end

-- Function to Populate the GPS Tooltip
local function drawGPSTip()
	local pvp, color = getPVP()
        pvp = gsub(pvp,"^%l", string.upper)

	tooltip:AddHeader(C.dkyellow(L["GPS"]))
	tooltip:AddLine(" ")

	tooltip:AddLine(C.ltblue(L["GPST_HEADER"]))
	tooltip:AddSeparator()
	tooltip:AddLine(C.ltyellow(L["ZONE"] .. ":"), GetRealZoneText())
	tooltip:AddLine(C.ltyellow(L["SUBZONE"] .. ":"), GetSubZoneText())
	tooltip:AddLine(C.ltyellow(L["PVPTYPE"] .. ":"), C[color](pvp))
	tooltip:AddLine(C.ltyellow(L["CO-ORDINATES"] .. ":"), getCoordinates())
	tooltip:AddLine(" ")

	tooltip:AddLine(C.ltblue(L["HOME"]))
	tooltip:AddSeparator()
	tooltip:AddLine(C.ltyellow(L["INN"]), GetBindLocation())
	tooltip:AddLine(" ")
	
	local line, column = tooltip:AddLine()
	tooltip:SetCell(line, 1, C.dkgreen(L["GPSHINT"]), nil, nil, 2)	
end

local gps_init = function(obj)
	if not obj then return end

	obj = obj.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName("GPS")

	obj.OnLeave = function ()
		if tooltip then
			LibQTip:Release(tooltip)
			tooltip = nil
		end
	end

	obj.OnEnter = function(self)
		tooltip = LibQTip:Acquire("GPSTT", 2, LEFT, RIGHT) 
		tooltip:SetScale(1)

		drawGPSTip()

		tooltip:SmartAnchorTo(self)
		--tooltip:UpdateScrolling()
		tooltip:Show()
	end
end


local function gps_OnUpdate(self)
	local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["GPS"])
	dataobj.text = C[gpsLoc.color](string.format("%s (%s)", gpsLoc.zone, getCoordinates()))
end

local gps_onclick = function()
	if GUILockDown == nil then 
		ToggleFrame(WorldMapFrame)
	elseif GUILockDown == 1 then 
		return 
	end
end

Broker_Everything:Register("GPS",
	GetItemIcon(11105),
	gps_init,
	gps_OnEvent,
	GPSEvents,
	gps_onclick,
	0,
	gps_OnUpdate,
	UPDATEPERIOD)