local width = GetScreenWidth();
local height = GetScreenHeight();
local resolution = (floor(height/width*1000+0.05))/1000;
local UIScale = 1;

function MarsUI_Config_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");
end

function MarsUI_Config_OnEvent()
	if (event == "VARIABLES_LOADED") then
		if (MarsUI_FirstSetup == nil) then
			MarsUI_FirstSetup = true;
		end
		
		if(MarsUI_Default[1]) then	-- 자동스케일기능을 사용할 경우 무조건 스케일 조정
			MarsUI_Config_SetUIscale();
		else				-- 자동스케일기능을 사용안할 경우 제일 처음 설치시에만 조정
			if(not MarsUI_FirstSetup) then MarsUI_Config_SetUIscale(); end
		end

		if (MarsUI_FirstSetup) then
			MarsUI_Config_SetChatFramePosition();
			MarsUI_Config_SetUIOption();
			MarsUI_FirstSetup = false;
		end
	end
end

function MarsUI_Config_SetUIscale()
	if(resolution >= 0.625) then
		SetCVar("useUiScale", "1", 9);
		if(resolution == 0.625) then
			UIScale = 0.98;
			SetCVar("uiScale", "0.98");
			return;
		elseif(resolution == 0.64) then
			UIScale = 0.96;
			SetCVar("uiScale", "0.96");
			return;
		elseif(resolution == 0.75) then
			UIScale = 0.82;
			SetCVar("uiScale", "0.82");
			return;
		elseif(resolution == 0.8) then
			UIScale = 0.77;
			SetCVar("uiScale", "0.77");
			return;
		end
	end
end

function MarsUI_Config_SetChatFramePosition()
	if(resolution >= 0.625 and resolution <= 0.8) then
		local	OffsetY = ceil(height/UIScale) - MarsUI_Default[2]["Height"] - MarsUI_Default[2]["OffsetY"];

		ChatFrame1:ClearAllPoints();
		ChatFrame1:StartMoving();
		ChatFrame1:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", MarsUI_Default[2]["OffsetX"], -OffsetY);
		ChatFrame1:SetWidth(MarsUI_Default[2]["Width"]);
		ChatFrame1:SetHeight(MarsUI_Default[2]["Height"]);
		ChatFrame1:StopMovingOrSizing();
	end
end

function MarsUI_Config_SetUIOption()
	SetCVar("statusBarText", "1", 2);
	SetCVar("profanityFilter", "0", 5);
	SetCVar("cameraDistanceMaxFactor", "2");
	setglobal("QUEST_FADING_DISABLE", "1");
	setglobal("SHOW_BUFF_DURATIONS", "1");
	setglobal("SHOW_TARGET_OF_TARGET", "1");
end