
local class = {}; --Class("GHI_Position");

local coor1,coor2 = {},{};
local posCache = {};

function class.GetXY(self,unit)
	if not(unit) then unit="player" end
		
	--local t = class:GetPosCache(unit);
	--if not(#(t)==0) then
	--	return unpack(t);
		--return 0,0,0,0,0;
	--end

	local zoneIndex = GetCurrentMapZone();
	local continent = GetCurrentMapContinent();
	SetMapToCurrentZone();
	local currentContinent = GetCurrentMapContinent();
	local inInstance = false;
	if currentContinent == -1 or currentContinent == 3 then
		currentContinent = 0;
		inInstance = true;
	else
		SetMapZoom(0);
	end	
	
	x,y = GetPlayerMapPosition(unit);
	--local scale = self:GetScale(currentContinent);
	
	CONX = CONX or 42419;
	CONY = CONY or 28182
	
	x=x*CONX;--*477.14; -- scale for azeroth
	y=y*CONY;--*212.08;
	
	
	
	
	-- todo: move this part to "On map show" 
	if inInstance == true then
		SetMapToCurrentZone();
	else
		SetMapZoom(continent,zoneIndex); 
	end
	-- en d of todo 

	
	if true then 
		return x,y;	
	end
	
	-- floors
	if self.WFc == 0 then
		local lastX = self.lastX or x;
		local lastY = self.lastY or x;
		
		if not(lastX == x and lastY == y) then	
			coor1[1] = x;
			coor1[2] = y;
			coor2[1] = lastX;
			coor2[2] = lastY;
			--coor2 = {lastX,lastY};
			self.floor = self:GetChangedFloor(c1,c2,self.floor,currentContinent);
		end
	
		local d = sqrt((x-lastX)^2+(y-lastY)^2); 
		self.speed = (d*4)
		self.lastX = x;
		self.lastY = y;
		
		--self:Msg("Floor %s, speed %s m/s.",self.floor,self.speed);
	end

	class:SetPosCache(unit,x,y,currentContinent,(self.floor or 0),(self.speed or 0));
	return x,y,currentContinent,(self.floor or 0),(self.speed or 0);
	
	
end

function class.ClearPosCache(self)
	if not(type(posCache) == "table") then posCache = {}; end
	if not(type(posCache["player"]) == "table") then posCache["player"] = {}; end
	--GHI_PrintArray(posCache["player"]);
	--posCache["player"].cacheCleared = true;
	--["player"] = wipe(posCache["player"]);
	wipe(posCache["player"])-- = {};
	--collectgarbage("collect");
end

function class.SetPosCache(self,unit,x,y,z,c,f,s)
	if not(type(posCache) == "table") then posCache = {}; end
	if not(type(posCache[strlower(unit)]) == "table") then posCache[strlower(unit)] = {}; end
	
	--wipe(posCache[strlower(unit)]);
	--posCache[strlower(unit)] = {x,y,z,c,f,s};
	
	-- gabage free method of adding the new cache
	posCache[strlower(unit)][1] = x;
	posCache[strlower(unit)][2] = y;
	posCache[strlower(unit)][3] = z;
	posCache[strlower(unit)][4] = c;
	posCache[strlower(unit)][5] = f;
	posCache[strlower(unit)][6] = s;
	
	
end


function class.GetPosCache(self,unit)
	if not(type(posCache) == "table") then posCache = {}; end
	return posCache[strlower(unit)] or {};
end


function GHI_Pos() return class; end;



-- /script local x,y = GHI_Pos():GetXY(); if LastX and LastY then print(abs(LastX-x),":",abs(LastY-y)); end LastX = x; LastY = y;

-- /script local x,y = GHI_Pos():GetXY(); local t = GetTime(); if LastX and LastY and LastT then local a,b = sqrt((LastX-x)^2 + (LastY-y)^2),(t-LastT);  print(a/b); end LastX = x; LastY = y; LastT = t;




-- ***************************************** 
--							Map tests
-- *****************************************


function GHU_MapTest()
	local f = CreateFrame("frame",nil,UIParent);
	
	f:SetFrameStrata("BACKGROUND")
	f:SetWidth(1024) -- Set these to whatever height/width is needed 
	f:SetHeight(512) -- for your Texture
	
	
	f.textures = {};
	
	
	
	--local t = f:CreateTexture(nil,"BACKGROUND")
	--t:SetTexture("Interface/WorldMap/Darkshore/RuinsofAuberdine1.png")
	--t:SetPoint("TOPLEFT",f,"TOPLEFT",0,0);
	--table.insert(f.textures,t);
	local xOff,yOff,scale = 31000,22000,2000/1024;
	
	for zone,info in pairs(GHI_MiscData.MapCalc) do -- print("Adding",zone);
		for _,piece in pairs(info) do --print("Map piece",piece.p);
			local t = f:CreateTexture(nil,"BACKGROUND")
			t:SetTexture(piece.p.."1");--strreplace(piece.p,"\\","/"));
			t:SetHeight(piece.h/scale); print((piece.h))
			t:SetWidth(piece.w/scale);
			t:SetPoint("TOPLEFT",f,"TOPLEFT",(piece.x-xOff)/scale,-(piece.y-yOff)/scale); print((piece.x-xOff),(piece.y-yOff));
			t:Show();
			table.insert(f.textures,t);
		end	
	end
	
	f:SetPoint("CENTER",UIParent,"CENTER",0,0)
	f:Show()
	

end


local info;
function GHU_CalcMap()
		
	if not(info) or not(info.zone == GetRealZoneText()) then
		local x,y = GHI_Pos():GetXY();
		SetMapToCurrentZone();
		local x2,y2 = GetPlayerMapPosition("player");
		info = {
			x_w = x,
			y_w = y,
			x_z = x2*1.50,
			y_z = y2*1.00,
			zone = GetRealZoneText(),
		}
		print("first point selected");		
	else	
		local x_w1,y_w1 = GHI_Pos():GetXY();
		SetMapToCurrentZone();
		local x,y = GetPlayerMapPosition("player");
		local x_z1,y_z1 = x*1.50,y*1.00;
		
		local t = info;
		local x_w2,y_w2,x_z2,y_z2 = t.x_w,t.y_w,t.x_z,t.y_z;			
		
		-- x_w = a*x_z + b
		local scaleX = (-x_w2+x_w1)/(-x_z2+x_z1);
		local offX = x_w1-(-x_w2+x_w1)*x_z1/(-x_z2+x_z1);
		
		local scaleY = (-y_w2+y_w1)/(-y_z2+y_z1);
		local offY = y_w1-(-y_w2+y_w1)*y_z1/(-y_z2+y_z1);
				
		local scale = max(scaleX,scaleY) - 0.5*(abs(scaleX-scaleY));
		print("zone width",scale*1.5);
		print("off",offX,offY);
																		
		local t = {};			
		for i = 1, GetNumMapOverlays() do
			local p,w,h,x,y = GetMapOverlayInfo(i);
			
			local sx,sy = WorldMapOverlay1:GetParent():GetWidth(),WorldMapOverlay1:GetParent():GetHeight();
			if string.len(p) > 0 then
				table.insert(t,{
					p = p,
					w = 1.5*w*scale/sx, --((w*scale/1024)),
					h = h*scale/sy,--((h*scale/768)),
					x = 1.5*x*scale/sx + offX, --((x*scale/1024)+offX),
					y = y*scale/sy + offY, --((y*scale/768)+offY),
				});
			end
		end
		
		local z = info.zone;
		GHI_MiscData.MapCalc = GHI_MiscData.MapCalc or {}
		GHI_MiscData.MapCalc[z] = t;
		print("done",z);
	end
end	







































