--
-- @file WindMinimap.lua
-- @desc 미니맵과 미니맵 버튼을 설정한다.
-- @author Wind(Jaeyoung Lee), Hexa(Bongsu Kang)
-- @last-date 2007.08.03
--

-- 사용자정의 버튼 설정값을 저장할지 여부를 결정하는 변수
-- 한번 설정 후 잘 안변한다거나, 위치를 잡기위해 reloadui() 의 사용만으로 결과를 보고 싶을때 
-- 이 변수를 false로 설정.
local SaveExtBtnConfig = false;

-- function WindMinimap_OnLoad
-- @Description 애드온이 로드 되었을 때 수행
function WindMinimap_OnLoad()
	GameTimeFrame:Hide();
	MinimapToggleButton:Hide();
	MinimapZoomIn:Hide();
	MinimapZoomOut:Hide();
	MinimapBorderTop:Hide();

	Minimap:SetParent("WindMinimap");
	MinimapZoneTextButton:SetParent("Minimap");
	MinimapBackdrop:SetPoint("TOPLEFT", "Minimap", "TOPLEFT", -34, 3);

	MinimapZoneTextButton:SetFrameStrata("LOW");
	MiniMapTracking:SetFrameStrata("LOW");
	MiniMapMeetingStoneFrame:SetFrameStrata("LOW");
	MiniMapMailFrame:SetFrameStrata("LOW");
	MiniMapBattlefieldFrame:SetFrameStrata("LOW");

	WindMinimapBorderTop:Hide(); 
	WindCommon_setPoint(Minimap, "CENTER", WindMinimap, "TOP", 9, -92);

	WindMinimap:RegisterEvent("VARIABLES_LOADED");
end

-- function WindMinimap_OnEvent
-- @Description 미니맵 관련 이벤트 발생시 수행
function WindMinimap_OnEvent()
	if (event == "VARIABLES_LOADED") then
		if (not WindMinimapConfigAll) then WindMinimapConfigAll = WindMinimapDefault; end
		WindMinimapBody_initConfig();
		WindMinimapBody_setPoint();
		WindMinimapBody_setScale();
		WindMinimapBody_setTexture();
		WindMinimapBody_setColor();
		WindMinimapBody_setCoords();
		WindMinimapBody_setForm();
		
		WindMinimapButton_initConfig();
		local BtnNums = #(WindMinimapConfigAll["BlzButtons"]);
		for i=1, BtnNums do	
			WindMinimapButton_setPoint(i);
			WindMinimapButton_setScale(i);
			WindMinimapButton_setVisible(i);
		end

		WindMinimapExtraButton_setConfig();
	end
end

-- ***************** 미니맵 관련 함수 *****************
function WindMinimapBody_initConfig()
	if (not WindMinimapConfigAll["Body"]) then WindMinimapConfigAll["Body"] = WindMinimapDefault["Body"]; end

	local config = WindMinimapConfigAll["Body"];
	if (config[8] == false) then config[8] = 1; end
	if (config[8] == true) then config[8] = 2; end

	if (IsAddOnLoaded("WindConfig")) then 
		WindMinimapConfigBody_show();
		WindMinimapConfigBody_setColor(color); 
	end
end

function WindMinimapBody_setPoint()
	local config = WindMinimapConfigAll["Body"];
	WindCommon_setPoint(WindMinimap, config[1], UIParent, config[1], config[2], config[3]);
end

function WindMinimapBody_setScale()
	local config = WindMinimapConfigAll["Body"];
	Minimap:SetScale(config[4]);
	WindMinimapBorder:SetScale(config[4]);
	WindMinimapZoneTextBorder:SetScale(config[4]);
end

function WindMinimapBody_setTexture()
	local config = WindMinimapConfigAll["Body"];
	if (config[8] == 1) then 
		if (config[5]) then 
			WindMinimapBorderTop:ClearAllPoints();
			WindMinimapBorderTop:SetDrawLayer("BACKGROUND");
			WindMinimapBorderTop:SetTexture("Interface\\AddOns\\WindMinimap\\images\\minimapbg");
			WindMinimapBorderTop:SetWidth(192);
			WindMinimapBorderTop:SetHeight(192);
			WindMinimapBorderTop:SetParent("Minimap");
			WindMinimapBorderTop:SetPoint("TOPLEFT", Minimap, "TOPLEFT", -26, 25);
			WindMinimapBorderTop:SetTexCoord(0,1,1,0);
			WindMinimapBorderTop:Show();
		else 
			WindMinimapBorderTop:Hide(); 
		end
	elseif (config[8] == 2) then
		WindMinimapBorderTop:Hide(); 
		WindMinimapBorder:SetBackdrop(WindCommon_ColorBorder);
		if (config[5]) then 
			WindMinimapZoneTextBorder:SetBackdrop(WindCommon_ColorBorder);
		else 
			WindMinimapZoneTextBorder:SetBackdrop(nil);
		end
	end
end

function WindMinimapBody_setColor()
	local config = WindMinimapConfigAll["Body"];
	if (config[8] == 1) then 
		WindMinimapBorderTop:SetGradientAlpha("vertical",config[6][1].r, config[6][1].g, config[6][1].b, config[6][1].a, config[6][2].r, 
												config[6][2].g, config[6][2].b, config[6][2].a);
	elseif (config[8] == 2) then
		WindMinimapBorder:SetBackdropBorderColor(config[6][1].r, config[6][1].g, config[6][1].b, config[6][1].a);
		WindMinimapBorder:SetBackdropColor(config[6][2].r, config[6][2].g, config[6][2].b, config[6][2].a);
		WindMinimapZoneTextBorder:SetBackdropBorderColor(config[6][1].r, config[6][1].g, config[6][1].b, config[6][1].a);
		WindMinimapZoneTextBorder:SetBackdropColor(config[6][2].r, config[6][2].g, config[6][2].b, config[6][2].a);
	end
end

function WindMinimapBody_resetColor()
	WindMinimapConfigAll["Body"][6] = WindMinimapDefault["Body"][6];
	WindMinimapBody_setColor();
end

function WindMinimapBody_setCoords()
	if (WindMinimapConfigAll["Body"][7]) then WindMinimapCoordsMiniMapFrame:Show();
	WindMinimapCoordsWorldMapFrame:Show();
	else WindMinimapCoordsMiniMapFrame:Hide();
	WindMinimapCoordsWorldMapFrame:Hide(); end
end

function WindMinimapBody_setForm()
	local config = WindMinimapConfigAll["Body"];
	if (config[8] == 1) then
		WindMinimapBorder:Hide();
		WindMinimapZoneTextBorder:Hide();
		Minimap:SetMaskTexture("Textures\\MinimapMask");
		MinimapBorder:Show();
		MinimapZoneTextButton:SetPoint("TOP","Minimap","TOP", 0, 20);
		MinimapZoneText:SetPoint("TOP","MinimapZoneTextButton","TOP");
		MinimapZoneText:SetFont("Fonts\\K_Damage.TTF",18);
		MinimapZoneText:SetShadowOffset(1,-1); 
	elseif (config[8] == 2) then
		WindMinimapBorder:Show();
		WindMinimapZoneTextBorder:Show();
		Minimap:SetMaskTexture("Interface\\AddOns\\WindMinimap\\images\\mask");
		MinimapBorder:Hide();
		MinimapZoneTextButton:SetPoint("TOP","Minimap","TOP", 0, 27);
		MinimapZoneText:SetPoint("TOP","MinimapZoneTextButton","TOP");
		MinimapZoneText:SetFont("Fonts\\2002.TTF",14);
	end
end

-- ***************** 블리자드 기본 미니맵 버튼 관련 함수 *****************
-- 미니맵 버튼 index 는 WindMinimapConfig["Buttons"] 요소의 index 값에 따라 간다.
-- 1:Mail, 2:Tracking, 3: Battlefield, 4:MeetingStone, 5:WorldMap
function WindMinimapButton_initConfig()
	if(not WindMinimapConfigAll["BlzButtons"]) then WindMinimapConfigAll["BlzButtons"] = WindMinimapDefault["BlzButtons"]; end
end

function WindMinimapButton_setPoint(index)
	local curBtnConfig = WindMinimapConfigAll["BlzButtons"][index];
	local target = getglobal("MiniMap"..curBtnConfig[5]..curBtnConfig[6]);

	target:ClearAllPoints();
	WindCommon_setPoint(target, curBtnConfig[1], Minimap, curBtnConfig[1], curBtnConfig[2], curBtnConfig[3]);
end

function WindMinimapButton_setScale(index)
	local curBtnConfig = WindMinimapConfigAll["BlzButtons"][index];
	local target = getglobal("MiniMap"..curBtnConfig[5]..curBtnConfig[6]);

	target:SetScale(curBtnConfig[4]);
end

function WindMinimapButton_setVisible(index)
	local curBtnConfig = WindMinimapConfigAll["BlzButtons"][index];
	local target = getglobal("MiniMap"..curBtnConfig[5]..curBtnConfig[6]);
	
	-- -1 값이면 반드시 보여야 하는 버튼이므로 종료한다.
	if curBtnConfig[7] == -1 then 
		return; 
	else	
		-- 이벤트 발생시에 보여지는 버튼들이 많으므로 숨기는 것만 수행한다. 
		if curBtnConfig[7] == 0 then target:Hide(); end
	end
end

-- ***************** 사용자 정의 미니맵 버튼 관련 함수 *****************
-- 버튼 index 는 WindMinimapConfig["ExtraButtons"][index] 요소의 index 값에 따라 간다.
-- 1:자기 기준점, 2:X 변위, 3:Y 변위, 4:Scale, 5: 버튼명, 6: 위치기준 프레임명
function WindMinimapExtraButton_setConfig()
	local config = nil;
	if(not WindMinimapConfigAll["ExtraButtons"]) then WindMinimapConfigAll["ExtraButtons"] = WindMinimapDefault["ExtraButtons"]; end

	-- 사용자정의 버튼 설정값을 저장할 것인지 결정. 저장안하면 게임로딩시 Default값을 계속 사용한다.
	if SaveExtBtnConfig then 
		config = WindMinimapConfig["ExtraButtons"];
	else 
		config = WindMinimapDefault["ExtraButtons"];
	end

	-- 사용자 정의 버튼이 없으면 종료한다.
	if(config==nil) then return; end

	local ExtBtnNums = #config;

	if ExtBtnNums > 0 then
		for i=1, ExtBtnNums do
			local curBtnConfig = config[i];
			local target = getglobal(curBtnConfig[5])
			if(target) then
				target:ClearAllPoints();
				WindCommon_setPoint(target, curBtnConfig[1], curBtnConfig[6], curBtnConfig[1], curBtnConfig[2], curBtnConfig[3]);
				target:SetScale(curBtnConfig[4]);
			end
		end
	end
end

-- ***************** 미니맵 기타 설정 함수 *****************
function WindMinimap_OnMouseWheel(arg1)
	if ( arg1 < 0 ) then
		if (Minimap:GetZoom() ~= 0 ) then
			Minimap:SetZoom(Minimap:GetZoom() - 1);
			PlaySound("igMinimapZoomOut");
		end
	elseif ( arg1 > 0 ) then
		if (Minimap:GetZoom() ~= 5 ) then
			Minimap:SetZoom(Minimap:GetZoom() + 1);
			PlaySound("igMinimapZoomIn");
		end
	end
end

function WindMinimapCoords_OnLoad()
	this:RegisterEvent("ZONE_CHANGED_NEW_AREA");
end

function WindMinimapCoords_OnEvent()
	if (event == "ZONE_CHANGED_NEW_AREA") then
		SetMapToCurrentZone();
	end
end

function WindMinimapCoordsMinimap_OnUpdate()
	local output = "";	
	local mult = 10^0;
	if Minimap:IsVisible() then
		local px, py = GetPlayerMapPosition("player");
		if (px > 0 and py  > 0) then
			WindMinimapCoords:Show();
			output = math.floor(((px * 100) * mult + 0.5) / mult)..","..math.floor(((py * 100) * mult + 0.5) / mult);					
			WindMinimapCoords:SetText(output);
			WindMinimapCoords:SetTextColor(0.5, 1, 0.5);
		else
			WindMinimapCoords:Show();
			WindMinimapCoords:SetText("인던");
			WindMinimapCoords:SetTextColor(1, 0.5, 0.5);
		end
	else
		WindMinimapCoords:Hide();
	end
end

function WindMinimapCoordsWorldMap_OnUpdate()
	local OFFSET_X = 0.0022;
	local OFFSET_Y = -0.0262;
	local mult = 10^0;
	local playerCoordsText = "";
	local cursorCoordsText = "";
	local color;
	if WorldMapFrame:IsVisible() then
		WindMinimapCoordsCursorCoords:Show();
		WindMinimapCoordsPlayerCoords:Show();
		local px, py = GetPlayerMapPosition("player");
		if (px > 0 and py  > 0) then
			this.px, this.py = px, py;
			playerCoordsText = format("%d,%d", math.floor(((this.px * 100) * mult + 0.5) / mult), math.floor(((this.py * 100) * mult + 0.5) / mult));
			color = "|cff88ff88";		--r, g, b = 0.5, 1, 0.5
		else
			playerCoordsText = "인던";
			color = "|cffff8888";		--r, g, b = 1, 0.5, 0.5
		end
		WindMinimapCoordsPlayerCoords:SetText("|cffffffff플레이어 (X,Y): "..color..format("%s", playerCoordsText));

		local x, y = GetCursorPosition();
		x = x / WorldMapFrame:GetScale();
		y = y / WorldMapFrame:GetScale();
		local centerX, centerY = WorldMapFrame:GetCenter();
		local width = WorldMapButton:GetWidth();
		local height = WorldMapButton:GetHeight();
		local adjustedX = (x - (centerX - (width/2))) / width;
		local adjustedY = (centerY + (height/2) - y ) / height;
		local cx = (adjustedX + OFFSET_X);
		local cy = (adjustedY + OFFSET_Y);
		cursorCoordsText = format("%d,%d", 100 * cx, 100 * cy);
		WindMinimapCoordsCursorCoords:SetText("|cffffffff커서 (X,Y): ".."|cff88ff88"..format("%s", cursorCoordsText));
	end
end
