-------------------------------------------------------------------------------
--
--  Mod Name : Blink Resizer v2.03
--  Author   : Blink
--  Date     : 2005/01/12
--  LastUpdate : 2006/07/31
--
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- Variables
-------------------------------------------------------------------------------
BlinkResizer_Config_Loaded = 0;
BlinkResizer_Config_Initialized = 0;
BlinkResizer_Player = nil;
BlinkResizer_Server = nil;
BR_execute_init_time = 0;
BR_TEXT_TITLE = "크기조절 v2.03";
BR_TEXT_RESET = "초기화";
BR_TEXT_CLOSE = "닫기";
BR_TEXT_SELECT_FRAME = "크기조절할 창선택:";
BR_TEXT_USE_RESIZER = "크기조절 사용";

BR_FORMAT_USE_RESIZER = "%s 크기조절 사용";
BR_FORMAT_NOT_INPUT_DESC = "%s의 크기를 조절할 수 있습니다.";

BR_Config = {};

BR_Frames = {};
BR_Frames[1] = { name = "캐릭터창", scale = 1.0, frames = {"CharacterFrame"}, desc = nil, isDefault=1 };
BR_Frames[2] = { name = "스킬창", scale = 1.0, frames = {"SpellBookFrame"}, desc = nil, isDefault=1 };
BR_Frames[3] = { name = "루팅창", scale = 1.0, frames = {"LootFrame"}, desc = nil, isDefault=1 };
BR_Frames[4] = { name = "그리폰창", scale = 1.0, frames = {"TaxiFrame"}, desc = nil, isDefault=1 };
BR_Frames[5] = { name = "퀘스트창", scale = 1.0, frames = {"QuestFrame"}, desc = nil, isDefault=1 };
BR_Frames[6] = { name = "퀘스트로그창", scale = 1.0, frames = {"QuestLogFrame"}, desc = nil, isDefault=1 };
BR_Frames[7] = { name = "상인창", scale = 1.0, frames = {"MerchantFrame"}, desc = nil, isDefault=1 };
BR_Frames[8] = { name = "거래창", scale = 1.0, frames = {"TradeFrame"}, desc = nil, isDefault=1 };
BR_Frames[9] = { name = "은행창", scale = 1.0, frames = {"BankFrame"}, desc = nil, isDefault=1 };
BR_Frames[10] = { name = "친구창", scale = 1.0, frames = {"FriendsFrame"}, desc = nil, isDefault=1 };
BR_Frames[11] = { name = "월드맵", scale = 1.0, frames = {"WorldMapFrame"}, desc = nil, isDefault=1 };
BR_Frames[12] = { name = "특성창", scale = 1.0, frames = {"TalentFrame"}, desc = nil, isDefault=1 };
BR_Frames[13] = { name = "경매창", scale = 1.0, frames = {"AuctionFrame"}, desc = nil, isDefault=1 };
BR_Frames[14] = { name = "메인메뉴바", scale = 1.0, frames = {"MainMenuBar"}, desc = nil, isDefault=1 };
BR_Frames[15] = { name = "본인창", scale = 1.0, frames = {"PlayerFrame"}, desc = nil, isDefault=1 };
BR_Frames[16] = { name = "대상창", scale = 1.0, frames = {"TargetFrame"}, desc = nil, isDefault=1 };
BR_Frames[17] = { name = "버프창", scale = 1.0, frames = {"BuffFrame"}, desc = nil, isDefault=1 };
BR_Frames[18] = { name = "왼쪽두줄바", scale = 1.0, frames = {"MultiBarBottomLeft"}, desc = nil, isDefault=1 }
BR_Frames[19] = { name = "오른쪽두줄바", scale = 1.0, frames = {"MultiBarBottomRight"}, desc = nil, isDefault=1 }
BR_Frames[20] = { name = "사이드바1", scale = 1.0, frames = {"MultiBarLeft"}, desc = nil, isDefault=1 }
BR_Frames[21] = { name = "사이드바2", scale = 1.0, frames = {"MultiBarRight"}, desc = nil, isDefault=1 }

--[[
BR_Frames[1] = { name = "게임메인메뉴", scale = 1.0, frames = {"GameMenuFrame"}, desc = nil, isDefault=1 };
BR_Frames[2] = { name = "캐릭터창", scale = 1.0, frames = {"CharacterFrame"}, desc = nil, isDefault=1 };
BR_Frames[3] = { name = "스킬창", scale = 1.0, frames = {"SpellBookFrame"}, desc = nil, isDefault=1 };
BR_Frames[4] = { name = "루팅창", scale = 1.0, frames = {"LootFrame"}, desc = nil, isDefault=1 };
BR_Frames[5] = { name = "그리폰창", scale = 1.0, frames = {"TaxiFrame"}, desc = nil, isDefault=1 };
BR_Frames[6] = { name = "퀘스트창", scale = 1.0, frames = {"QuestFrame"}, desc = nil, isDefault=1 };
BR_Frames[7] = { name = "퀘스트로그창", scale = 1.0, frames = {"QuestLogFrame"}, desc = nil, isDefault=1 };
BR_Frames[8] = { name = "상급직업창", scale = 1.0, frames = {"ClassTrainerFrame"}, desc = nil, isDefault=1 };
BR_Frames[9] = { name = "전문기술창", scale = 1.0, frames = {"TradeSkillFrame"}, desc = nil, isDefault=1 };
BR_Frames[10] = { name = "상인창", scale = 1.0, frames = {"MerchantFrame"}, desc = nil, isDefault=1 };
BR_Frames[11] = { name = "거래창", scale = 1.0, frames = {"TradeFrame"}, desc = nil, isDefault=1 };
BR_Frames[12] = { name = "은행창", scale = 1.0, frames = {"BankFrame"}, desc = nil, isDefault=1 };
BR_Frames[13] = { name = "친구창", scale = 1.0, frames = {"FriendsFrame"}, desc = nil, isDefault=1 };
BR_Frames[14] = { name = "제작창", scale = 1.0, frames = {"CraftFrame"}, desc = nil, isDefault=1 };
BR_Frames[15] = { name = "월드맵", scale = 1.0, frames = {"WorldMapFrame"}, desc = nil, isDefault=1 };
BR_Frames[16] = { name = "특성창", scale = 1.0, frames = {"TalentFrame"}, desc = nil, isDefault=1 };
BR_Frames[17] = { name = "PetStableFrame", scale = 1.0, frames = {"PetStableFrame"}, desc = nil, isDefault=1 };
BR_Frames[18] = { name = "경매창", scale = 1.0, frames = {"AuctionFrame"}, desc = nil, isDefault=1 };
BR_Frames[19] = { name = "메인메뉴바", scale = 1.0, frames = {"MainMenuBar"}, desc = nil, isDefault=1 };
BR_Frames[20] = { name = "본인창", scale = 1.0, frames = {"PlayerFrame"}, desc = nil, isDefault=1 };
BR_Frames[21] = { name = "대상창", scale = 1.0, frames = {"TargetFrame"}, desc = nil, isDefault=1 };
BR_Frames[22] = { name = "버프창", scale = 1.0, frames = {"BuffFrame"}, desc = nil, isDefault=1 };
BR_Frames[23] = { name = "왼쪽두줄바", scale = 1.0, frames = {"MultiBarBottomLeft"}, desc = nil, isDefault=1 }
BR_Frames[24] = { name = "오른쪽두줄바", scale = 1.0, frames = {"MultiBarBottomRight"}, desc = nil, isDefault=1 }
BR_Frames[25] = { name = "사이드바1", scale = 1.0, frames = {"MultiBarLeft"}, desc = nil, isDefault=1 }
BR_Frames[26] = { name = "사이드바2", scale = 1.0, frames = {"MultiBarRight"}, desc = nil, isDefault=1 }
]]
--------------------------------------------------------------------------------------------
-- BlinkResizer_AddFrame() 사용방법
--
-- name : /명령어로 크기를 조절할때 사용될 "창이름"을 입력해주시면 됩니다.
-- frames : 크기조절에 영향을 받을 프레임들을 넣어주시면됩니다. 배열값입니다.
-- desc : 크기조절하는 창의 설명문구를 나타냅니다.
-- 
-- 추가예)
-- BlinkResizer_AddFrame("내애드온",{"MyAddOnFrame","MyAddOnConfigFrame"},"내가 만든 애드온의 크기를 조절합니다.");
--
--------------------------------------------------------------------------------------------
function BlinkResizer_AddFrame(name,frames,desc)
	if( not name )then return; end
	for fid,data in pairs(BR_Frames) do
		if( data.name == name )then
			return;
		end
	end
	if( type(frames) == "table" )then
		for i,f in pairs(frames) do
			if( not getglobal(f) )then
				return;
			end
		end
	elseif( type(frames) == "string" ) then
		if( not getglobal(frames) )then
			return;
		else
			frames = { frames };
		end
	end
	if( not desc or strlen(desc)<1 )then
		desc = format(BR_FORMAT_NOT_INPUT_DESC,name);
	end

	local info = {
		name = name,
		scale = 1.0,
		frames = frames,
		desc = desc,
		isDefault = 0
	};
	table.insert(BR_Frames,info);
end

function BlinkResizer_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");

	-- Slash commands
	SlashCmdList["RESIZER"] = function (msg) BlinkResizer_SlashHandler(msg); end;
	SLASH_RESIZER1 = "/resizer";
	SLASH_RESIZER2 = "/크기조절";

	SlashCmdList["RESIZERCONFIG"] = function (msg) BlinkResizerOptionFrame:Show(); end;
	SLASH_RESIZERCONFIG1 = "/resizerconfig";
	SLASH_RESIZERCONFIG2 = "/크기조절설정";

	BR_execute_init_time = 0;
end

function BlinkResizer_OnEvent(event)
	if (event == "VARIABLES_LOADED") then
		BR_execute_init_time = GetTime() + 5;
		BlinkResizer_Config_Loaded = 1;
		local playerName = UnitName("player");
		if (playerName ~= nil and playerName ~= UKNOWNBEING and playerName ~= UNKNOWNOBJECT) then
			BlinkResizer_Player = playerName;
		end
		if (BlinkResizer_Player and BlinkResizer_Config_Loaded) then
			BlinkResizer_ConfigInit();
		end
	end
end

function BlinkResizer_OnUpdate()
	if( BR_execute_init_time>0 and GetTime() >= BR_execute_init_time and BlinkResizer_Config_Initialized==0 )then
		BlinkResizer_DoResizing();
		BlinkResizer:Hide();
	end
end

function BlinkResizer_ConfigInit()
	if (not BlinkResizer_Player) then return; end
	if (not BlinkResizer_Config_Loaded) then return; end

	if (BlinkResizer_Player and BlinkResizer_Config_Loaded) then
		if (not BR_Config) then
			BR_Config = {};
		end
	end
end

function BlinkResizer_DoResizing() -- callback
	if (not BlinkResizer_Player) then return; end
	if (not BlinkResizer_Config_Loaded) then return; end

	if (BlinkResizer_Player and BlinkResizer_Config_Loaded) then
		if (not BR_Config) then
			BR_Config = {};
		end
		for framename,scale in pairs(BR_Config) do
			BlinkResizer_Resizer(framename,scale);
		end
		BlinkResizer_Config_Initialized = 1;
	end
end

function BlinkResizer_Resizer(framename,scale)
	local resized = false;
	if( BlinkResizer_Config_Loaded ~= 1 )then return resized; end
	if (BlinkResizer_Player == nil or BlinkResizer_Player == UKNOWNBEING or BlinkResizer_Player == UNKNOWNOBJECT) then return resized; end
	if( not framename )then return resized; end
	if( not scale )then return resized; end
	if( type(scale) ~= "number" )then return resized; end
	if( scale < 0.5 or scale > 2.0 )then return resized; end -- 0.5보다 작거다 2.0보다 크면 이상하자낭~~
	for f,data in pairs(BR_Frames) do
		if( data.name == framename )then
			for i=1,getn(data.frames) do
				local rf = getglobal(data.frames[i]);
				if( rf )then
					--rf:SetScale( scale * UIParent:GetEffectiveScale() );
					rf:SetScale( scale );
					resized = true;
				end
			end
		end
	end
	if( not BR_Config )then
		BR_Config = {};
	end
	if( scale == 1.0 )then
		BR_Config[framename] = nil;
	else
		BR_Config[framename] = scale;
	end
	return resized;
end

function BlinkResizer_ResizerById(idx,scale)
	local resized = false;
	if( BlinkResizer_Config_Loaded ~= 1 )then return; end
	if (BlinkResizer_Player == nil or BlinkResizer_Player == UKNOWNBEING or BlinkResizer_Player == UNKNOWNOBJECT) then return; end
	if( not idx )then return; end
	if( not scale )then return; end
	if( type(scale) ~= "number" )then return; end
	if( scale < 0.5 or scale > 2.0 )then return; end -- 0.5보다 작거다 2.0보다 크면 이상하자낭~~
	if( BR_Frames[idx] )then
		for i=1,getn(BR_Frames[idx].frames) do
			local rf = getglobal(BR_Frames[idx].frames[i]);
			if( rf )then
				--rf:SetScale( scale * UIParent:GetEffectiveScale() );
				rf:SetScale( scale );
				resized = true;
			end
		end
	end
	if( not BR_Config )then
		BR_Config = {};
	end
	if( scale == 1.0 )then
		BR_Config[BR_Frames[idx].name] = nil;
	else
		BR_Config[BR_Frames[idx].name] = scale;
	end
	return resized;
end

function BlinkResizer_Default()
	for f,data in pairs(BR_Frames) do
		for i=1,getn(data.frames) do
			local rf = getglobal(data.frames[i]);
			if( rf )then
				--rf:SetScale( 1.0 * UIParent:GetEffectiveScale() );
				rf:SetScale( 1.0 );
			end
		end
	end
	BR_Config = {};
end

function BlinkResizer_SlashHandler(msg)
	local framename = gsub(msg, "%s*([^%s]+).*", "%1");
	local scale = gsub(msg, "%s*([^%s]+)%s*(.*)", "%2");

	if( strlen(framename) <= 0 ) then
		BlinkResizer_Usage();
		return;
	end

	if( framename == "설정" )then
		BlinkResizerOptionFrame:Show();
		return;
	elseif( framename == "창보기" )then
		BlinkResizer_ViewFrames();
		return;
	elseif( framename == "초기화" )then
		BlinkResizer_Default();
		BlinkResizer_Chat("<크기조절기> 초기화 완료!!");
		return;
	elseif( strlen(scale) > 0 )then
		scale = scale + 0;
		if( BlinkResizer_Resizer(framename,scale) == true )then
			BlinkResizer_Chat(format("<크기조절기> %s|1을;를; %s크기로 조절합니다.",framename,""..scale));
			return;
		end
		return;
	end
	BlinkResizer_Usage();
end

function BlinkResizer_ViewFrames()
	BlinkResizer_Chat("<크기조절기> 크기설정할수 있는 창은 다음과 같습니다 :");
	for f,data in pairs(BR_Frames) do
		local valid = true;
		for i,frm in pairs(data.frames) do
			if( not getglobal(frm) )then
				valid = false;
			end
		end
		if( valid == true)then BlinkResizer_Chat(format("<크기조절기>    |cffffffff%s|r",data.name)); end
	end
end

function BlinkResizer_Usage()
	BlinkResizer_Chat("<크기조절기> 크기조절기 사용방법:");
	BlinkResizer_Chat("<크기조절기>  /크기조절          : 도움말");
	BlinkResizer_Chat("<크기조절기>  /크기조절 설정     : 크기조절 설정창을 엽니다.");
	BlinkResizer_Chat("<크기조절기>  /크기조절 크기조절할창이름 크기(숫자) : 크기를 조절합니다.");
	BlinkResizer_Chat("<크기조절기>  /크기조절 초기화   : 모든 설정을 초기화합니다.");
	BlinkResizer_Chat("<크기조절기>  /크기조절 창보기   : 조절할수 있는 창들을 표시 합니다.");
	BlinkResizer_Chat("<크기조절기> 참고 : 크기(숫자) 범위 : 0.5 ~ 2.0");
	BlinkResizer_Chat("<크기조절기> 예제:");
	BlinkResizer_Chat("<크기조절기>  /크기조절 캐릭터창 0.9");
	BlinkResizer_Chat("<크기조절기>   캐릭터창(스텟창)을 본래크기의 90%의 크기로 조절합니다.");
end

function BlinkResizer_Chat(msg)
	if( DEFAULT_CHAT_FRAME ) then
		DEFAULT_CHAT_FRAME:AddMessage(msg);
	end
end

-- Slider Control OnFoo -------------------------------------------------------
function BlinkResizerOptionSlider_OnLoad()
	BlinkResizerOption_RefreshBody();
end

function BlinkResizerOptionSlider_OnShow()
	BlinkResizerOptionSlider_OnLoad();
end

function BlinkResizerOptionSlider_OnValueChanged()
	local idx = UIDropDownMenu_GetSelectedID(BlinkResizerOptionDropDown);
	if( this:GetValue() >= 50 and this:GetValue() <= 200 )then
		local scale = this:GetValue() * 0.01;
		BlinkResizer_ResizerById(idx,scale);
		getglobal("BlinkResizerOptionSliderText"):SetText(this:GetValue() .. "%");
	end
end

function BlinkResizerOptionSlider_OnEnter()
	GameTooltip:SetOwner(this, "ANCHOR_BOTTOMRIGHT");
	GameTooltip:SetText(this.tooltip, nil, nil, nil, nil, 1);
end


-- DropDown Control OnFoo -----------------------------------------------------
function BlinkResizerOptionDropDown_Initialize()
	local info;
	for id,frame in pairs(BR_Frames) do
		info = { };
		--info.hasColorSwatch = 1;
		if( frame.isDefault == 1 )then		
			info.textR = 1.0;
			info.textG = 1.0;
			info.textB = 1.0;
		else
			info.textR = 0.0;
			info.textG = 1.0;
			info.textB = 0.0;
		end
		info.text = frame.name;
		info.func = BlinkResizerOptionDropDownButton_OnClick;
		UIDropDownMenu_AddButton(info);
	end
end

function BlinkResizerOptionDropDown_OnLoad()
	UIDropDownMenu_Initialize(BlinkResizerOptionDropDown, BlinkResizerOptionDropDown_Initialize);
	UIDropDownMenu_SetSelectedID(BlinkResizerOptionDropDown, 1);
end

function BlinkResizerOptionDropDownButton_OnClick()
	local oldID = UIDropDownMenu_GetSelectedID(BlinkResizerOptionDropDown);
	UIDropDownMenu_SetSelectedID(BlinkResizerOptionDropDown, this:GetID());
	if(oldID ~= this:GetID()) then
		BlinkResizerOption_RefreshBody();
	end
end

function BlinkResizerOption_RefreshBody()
	local currentID = UIDropDownMenu_GetSelectedID(BlinkResizerOptionDropDown);

	local button = getglobal("BRResizableFrameCheckButton");
	local buttontext = getglobal("BRResizableFrameCheckButtonText");

	local slider = getglobal("BlinkResizerOptionSlider");
	local string = getglobal("BlinkResizerOptionSliderText");
	local minText = getglobal("BlinkResizerOptionSliderLow");
	local maxText = getglobal("BlinkResizerOptionSliderHigh");

	local value = 100;
	--BlinkPrint("currentID:"..currentID..", BlinkResizer_Server:"..BlinkResizer_Server..", BlinkResizer_Player:"..BlinkResizer_Player..", BR_Frames["..currentID.."].name:"..BR_Frames[currentID].name);
	if( BR_Frames[currentID] )then
		if( BR_Config )then
			if( BR_Config[BR_Frames[currentID].name] )then
				value = BR_Config[BR_Frames[currentID].name];
				value = value * 100;
			end
		end
	end

	slider:SetMinMaxValues(50, 200);
	slider:SetValueStep(5);
	slider:SetValue(value);
	string:SetText(value.."%");
	minText:SetText( "50%" );
	maxText:SetText( "200%" );

	if( not BR_Frames[currentID].desc or strlen(BR_Frames[currentID].desc)<1 )then
		slider.tooltip = format(BR_FORMAT_NOT_INPUT_DESC,BR_Frames[currentID].name);
	else
		slider.tooltip = BR_Frames[currentID].desc;
	end
	
	--BlinkPrint("BR_Frames["..currentID.."].name : "..BR_Frames[currentID].name);

	--buttontext:SetText(BR_Frames[currentID].name.." "..BR_TEXT_USE_RESIZER);
	if( value ~= 100 )then
		button:SetChecked(1);
		BR_EnableSliderControl(getglobal("BlinkResizerOptionSlider"));
	else
		button:SetChecked(0);
		BR_DisableSliderControl(getglobal("BlinkResizerOptionSlider"));
	end
end


-- CheckButton Control OnFoo --------------------------------------------------
function BRResizableFrameCheckButton_UpdateDependencies()
	if ( not BRResizableFrameCheckButton:GetChecked() ) then
		BlinkResizer_ResizerById(idx,1.0);
		getglobal("BlinkResizerOptionSlider"):SetValue(100);
		getglobal("BlinkResizerOptionSliderText"):SetText("100%");
		BR_DisableSliderControl(getglobal("BlinkResizerOptionSlider"));
	else
		BR_EnableSliderControl(getglobal("BlinkResizerOptionSlider"));
	end
end

-- CheckButton Control OnFoo --------------------------------------------------
function BlinkResizerOptionCloseButton_OnClick()
	BlinkResizerOptionFrame:Hide();
end

-- ResetButton Control OnFoo --------------------------------------------------
function BlinkResizerOptionResetButton_OnClick()
	BlinkResizer_Default();
	BlinkResizerOption_RefreshBody();
end

-------------------------------------------------------------------------------
function BR_EnableSliderControl(slider)
	getglobal(slider:GetName().."Thumb"):Show();
	getglobal(slider:GetName().."Text"):SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b);	
	getglobal(slider:GetName().."Low"):SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b);
	getglobal(slider:GetName().."High"):SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b);
end

function BR_DisableSliderControl(slider)
	getglobal(slider:GetName().."Thumb"):Hide();
	getglobal(slider:GetName().."Text"):SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);	
	getglobal(slider:GetName().."Low"):SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
	getglobal(slider:GetName().."High"):SetTextColor(GRAY_FONT_COLOR.r, GRAY_FONT_COLOR.g, GRAY_FONT_COLOR.b);
end
