
local _
local CUI = CUI
local L = CreateFrame("Frame")
local Location = CreateFrame("Frame")
local EdgeBottom = CreateFrame("Frame")
local Fps = CreateFrame("Frame")
local Ping = CreateFrame("Frame")
local LocationInit, LocationFonts
local ClassColor, LastPlayerCoordinates
local LOCATIONPANEL_BACKGROUND_TEXTURE, EDGETILE_BOTTOM_TEXTURE, LOCATION_UPDATE_FREQUENCY

	LOCATIONPANEL_BACKGROUND_TEXTURE	= [[Interface\AddOns\CUI\Textures\layout\LocationBg]]
	EDGETILE_BOTTOM_TEXTURE				= [[Interface\AddOns\CUI\Textures\layout\EdgeTileBottom]]
	LOCATION_UPDATE_FREQUENCY			= 0.1
	
	L.RangeTimer = 0
	LastPlayerCoordinates = {["x"] = 0, ["y"] = 0}
	DataFonts = {
		["CoordX"] 		= {"Location", "", 9, "LEFT", {80,2}},
		["CoordY"] 		= {"Location", "", 9, "RIGHT", {-80,2}},
		["Zone"] 		= {"Location", "", 10, "CENTER", {0,2}},
		["Fps"] 		= {"EdgeBottom", "Left", 10, "BOTTOMLEFT", {5,5}},
		["Ping"] 		= {"EdgeBottom", "Right", 10, "BOTTOMRIGHT", {-5,5}},
	}

function LT:InitDataPanels()
	local x,y=CUI.GetPlayerMapPosition("player")
	
	Location:ClearAllPoints()
	Location:SetPoint("CENTER", E.Parent, "CENTER")
	
	Location.Tex = LT:CreateTexture(nil, "BACKGROUND")
	Location.Tex:SetSize(512,26)
	Location.Tex:SetPoint("TOP", E.Parent, "TOP")
	Location.Tex:SetTexture(LOCATIONPANEL_BACKGROUND_TEXTURE)
	Location.Tex:SetBlendMode("BLEND")
	Location.Tex:SetAlpha(0.75)
	Location.Tex:SetVertexColor(ClassColor[1],ClassColor[2],ClassColor[3])
	
	L.Location = Location
	
	FontInit = function(Object, Parent, SubParent, FontSize, Point, Offset)
		local Instance = L[Parent]
		if SubParent ~= "" then
			Instance = L[Parent][SubParent]
		end
		
		Instance[Object] = LT:CreateFontString(nil, "ARTWORK")
		E:InitializeFontFrame(Instance[Object], "ARTWORK", CUI.Media:Fetch("font", self.db.location["fontType"]), FontSize, {0.8,0.8,0.8}, 1, {0,0}, "", 0, 0, L, Point, {0,0})
		Instance[Object]:SetFont(CUI.Media:Fetch("font", self.db.location["fontType"]), FontSize, "")
		Instance[Object]:ClearAllPoints()
		if Parent == "Location" then
			Instance[Object]:SetPoint(Point, Location.Tex, Point, Offset[1], Offset[2])
		elseif Parent == "EdgeBottom" then
			Instance[Object]:SetPoint(Point, Instance, Point, Offset[1], Offset[2])
			if SubParent == "Left" then
				Instance[Object]:SetJustifyH("LEFT")
			else
				Instance[Object]:SetJustifyH("RIGHT")
			end
		end
	end
	for k,v in pairs(DataFonts) do
		FontInit(k, v[1],v[2],v[3],v[4],v[5])
	end
	
	Location:RegisterEvent("ZONE_CHANGED")
	Location:RegisterEvent("ZONE_CHANGED_INDOORS")
	Location:RegisterEvent("ZONE_CHANGED_NEW_AREA")
	Location:SetScript("OnEvent", function(self, event, ...)
		LT:UpdateLocationZone()
	end)
	LT:SetScript("OnUpdate", function(self, elapsed)
		local RangeTimer = self.RangeTimer;
		if ( RangeTimer ) then
			RangeTimer = RangeTimer - elapsed;

			if ( RangeTimer <= 0 ) then
				LT:UpdateLocationCoords()
				LT:UpdateSystemValues()
				RangeTimer = LOCATION_UPDATE_FREQUENCY;
			end

			self.RangeTimer = RangeTimer;
		end
	end)
	
	LT:UpdateLocationZone()
end

function LT:UpdateSystemValues()
	local Fps = L.EdgeBottom.Left.Fps
	local Ping = L.EdgeBottom.Right.Ping
	local Down, Up, LagHome, LagWorld = GetNetStats()
	
	Fps:SetText("FPS: " .. E:Round(GetFramerate(),1))
	Ping:SetText("Ping: " .. LagWorld .. "ms")
end

function LT:UpdateLocationCoords()
	local posX, posY = CUI.GetPlayerMapPosition("player")
	if posX and posY then
		if posX ~= LastPlayerCoordinates["x"] or posY ~= LastPlayerCoordinates["y"] then
		
			L.Location.CoordX:SetText(format("%.2f",	posX*100))
			L.Location.CoordY:SetText(format("%.2f",	posY*100))
			
			LastPlayerCoordinates["x"] = posX
			LastPlayerCoordinates["y"] = posY
		end
	else
		L.Location.CoordX:SetText("--.--")
		L.Location.CoordY:SetText("--.--")
	end
end

function LT:UpdateLocationZone()
	local pvpType, isSubZonePvP, factionName = GetZonePVPInfo();

	if ( pvpType == "sanctuary" ) then
		L.Location.Zone:SetTextColor(0.41, 0.8, 0.94);
	elseif ( pvpType == "arena" ) then
		L.Location.Zone:SetTextColor(1.0, 0.1, 0.1);
	elseif ( pvpType == "friendly" ) then
		if (factionName and factionName ~= "") then
			L.Location.Zone:SetTextColor(0.1, 1.0, 0.1);
		end
		L.Location.Zone:SetTextColor(0.1, 1.0, 0.1);
	elseif ( pvpType == "hostile" ) then
		if (factionName and factionName ~= "") then
			L.Location.Zone:SetTextColor(1.0, 0.1, 0.1);
		end
		L.Location.Zone:SetTextColor(1.0, 0.1, 0.1);
	elseif ( pvpType == "contested" ) then
		L.Location.Zone:SetTextColor(1.0, 0.7, 0);
	elseif ( pvpType == "combat" ) then
		L.Location.Zone:SetTextColor(1.0, 0.1, 0.1);
	else
		L.Location.Zone:SetTextColor(1.0, 0.9294, 0.7607);
	end
	
	local Zone = GetZoneText()
	local SubZone = GetSubZoneText()
	
	if Zone == SubZone or SubZone == "" then
		L.Location.Zone:SetText(format("%s",Zone))
	else
		L.Location.Zone:SetText(format("%s, %s",Zone, SubZone))
	end
end

function LT:InitWindowEdgeStyle()
	EdgeBottom.Left = EdgeBottom:CreateTexture(nil, "BACKGROUND")
	EdgeBottom.Left:SetTexture(EDGETILE_BOTTOM_TEXTURE)
	EdgeBottom.Left:SetSize(275,40)
	EdgeBottom.Left:SetPoint("BOTTOMLEFT", E.Parent, "BOTTOMLEFT")
	EdgeBottom.Left:SetTexCoord(0,1,0,1)
	EdgeBottom.Left:SetBlendMode("BLEND")
	EdgeBottom.Left:SetAlpha(0.75)
	
	EdgeBottom.Right = EdgeBottom:CreateTexture(nil, "BACKGROUND")
	EdgeBottom.Right:SetTexture(EDGETILE_BOTTOM_TEXTURE)
	EdgeBottom.Right:SetSize(275,40)
	EdgeBottom.Right:SetPoint("BOTTOMRIGHT", E.Parent, "BOTTOMRIGHT")
	EdgeBottom.Right:SetTexCoord(1,0,0,1)
	EdgeBottom.Right:SetBlendMode("BLEND")
	EdgeBottom.Right:SetAlpha(0.75)
	
	EdgeBottom.Left:SetVertexColor(ClassColor[1],ClassColor[2],ClassColor[3])
	EdgeBottom.Right:SetVertexColor(ClassColor[1],ClassColor[2],ClassColor[3])
	
	L.EdgeBottom = EdgeBottom
end

function LT:Init()
	self.db = CUI.db.profile.layout
	ClassColor = CUI:GetUnitClassColor("player")
	
	LT:SetPoint("CENTER", E.Parent, "CENTER")
	
	LT:InitWindowEdgeStyle()
	LT:InitDataPanels()
end

CUI:AddModule("Layout", L)


-- GameTooltip:SetOwner(self, "ANCHOR_LEFT");
						-- local pvpType, isSubZonePvP, factionName = GetZonePVPInfo();
						-- Minimap_SetTooltip( pvpType, factionName );
-- GameTooltip:Show();