-------------------------------------------------------------------------------
--
--  Mod Name : BlinkRogueHelper 2.2
--  Author   : Blink
--  Date     : 2004/10/11
--  LastUpdate : 2008/10/29
--
-------------------------------------------------------------------------------

BRH_ComboSkill = 40;

BRH_FinishSkill = 35;

BlinkRogueHelper_FontSizePixel = 70;


function BlinkRogueHelperFrame_OnLoad(self)
	self:RegisterEvent("UNIT_COMBO_POINTS");
	self:RegisterEvent("CHARACTER_POINTS_CHANGED");
	self:RegisterEvent("UNIT_ENERGY");
	self:RegisterEvent("VARIABLES_LOADED");
	self:RegisterEvent("LEARNED_SPELL_IN_TAB");

	self.fadeInTime = 0.2;		-- 숫자가 화면에 나타나는 시간(초)
	self.holdTime = 1.0;		-- 숫자가 화면에서 유지되는 시간(초)
	self.fadeOutTime = 0.8;		-- 숫자가 화면에서 사라지는 시간(초)
	self.flowTime = self.fadeInTime + self.holdTime + self.fadeOutTime;
	self.PI = 3.141592;

	self:Hide();

	BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel);
end

function BlinkRogueHelperFrame_OnEvent(self, event, ...)
	local arg1 = ...
	if ( event == "VARIABLES_LOADED" ) then
		if ( not BlinkRogueHelper_Enabled ) then
			BlinkRogueHelper_Enabled = 1;
		end
		if ( not BlinkRogueHelper_Type ) then
			BlinkRogueHelper_Type = 1;
		end
		BlinkRogueHelper_Register();
		BlinkRogueHelper_BM_Register();
		BlinkRogueHelper_ComboSkillEnergy()
		self:UnregisterEvent("VARIABLES_LOADED");

	elseif ( event == "UNIT_ENERGY" ) then
		if ( arg1 == "player" and self:IsShown() ) then
			SetBubbleTextColor();
		end
	
	elseif ( event == "UNIT_COMBO_POINTS" and arg1 == "player" and BlinkRogueHelper_Enabled ) then
		if ( GetComboPoints("player", "target")>0 ) then
			SetBubbleText();
			BlinkRogueHelper_Show(self);
		else
			self:Hide();
		end

	elseif event == "LEARNED_SPELL_IN_TAB" or event == "CHARACTER_POINTS_CHANGED" then
		BlinkRogueHelper_ComboSkillEnergy()
	end
end

function BlinkRogueHelper_ComboSkillEnergy()
	local talentrank = 0
	local class = select(2, UnitClass("player"))
	BRH_ComboSkill = 45

	if class == "ROGUE" then
		_, _, _, _, talentrank = GetTalentInfo(3, 16) -- 과다출혈
		if talentrank > 0 then
			BRH_ComboSkill = 35
			_, _, _, _, talentrank = GetTalentInfo(3, 27) -- 어둠의 학살자
			if talentrank > 0 then
				BRH_ComboSkill = 35 - talentrank
			end
		else
			_, _, _, _, talentrank = GetTalentInfo(2, 2) -- 사악한 일격 연마
			if talentrank == 1 then
				BRH_ComboSkill = 42
			elseif talenrank == 2 then
				BRH_ComboSkill = 40
			end
		end
	elseif class == "DRUID" then
		_, _, _, _, talentrank = GetTalentInfo(2, 26) -- 짓이기기
		if talentrank > 0 then
			_, _, _, _, talentrank = GetTalentInfo(2, 27) -- 짓이기기 연마
			if talentrank > 0 then
				BRH_ComboSkill = 45 - (talentrank * 2)
			end
		end
		_, _, _, _, talentrank = GetTalentInfo(2, 1) -- 야수의 본성
		if talentrank then
			BRH_ComboSkill = BRH_ComboSkill - talentrank
		end
	end
end
function BlinkRogueHelper_OnUpdate(self, elapse)
	if ( BlinkRogueHelper_Enabled == 1 ) then
		if( BlinkRogueHelper_Type == 2 ) then
			BlinkRogueHelper_Type2(self);
		elseif( BlinkRogueHelper_Type == 3 ) then
			BlinkRogueHelper_Type3(self);
		elseif( BlinkRogueHelper_Type == 4 ) then
			BlinkRogueHelper_Type4(self);
		else
			BlinkRogueHelper_Type1(self);
		end
	end
end

function BlinkRogueHelper_Type1(self)
	local elapsed = GetTime() - self.startTime;

	local fadeInTime = self.fadeInTime;
	if ( elapsed < fadeInTime ) then
		local alpha = (elapsed / fadeInTime);
		self:SetAlpha(alpha);
		BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel * alpha + 1);
		self:SetPoint("CENTER", "UIParent", "CENTER", 0, BlinkRogueHelper_FontSizePixel*alpha );
		return;
	end
	local holdTime = self.holdTime;
	if ( elapsed < (fadeInTime + holdTime) ) then
		self:SetAlpha(1.0);
		BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel + 1);
		self:SetPoint("CENTER", "UIParent", "CENTER", 0, BlinkRogueHelper_FontSizePixel );
		return;
	end
	local fadeOutTime = self.fadeOutTime;
	if ( elapsed < (fadeInTime + holdTime + fadeOutTime) ) then
		local alpha = 1.0 - ((elapsed - holdTime - fadeInTime) / fadeOutTime);
		self:SetAlpha(alpha);
		BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel * alpha + 1);
		self:SetPoint("CENTER", "UIParent", "CENTER", 0, BlinkRogueHelper_FontSizePixel*alpha );
		return;
	end
	self:Hide();
end

function BlinkRogueHelper_Type2(self)
	local elapsed = GetTime() - self.startTime;
	local flowTime = elapsed / self.flowTime;
	self:SetPoint("CENTER", "UIParent", "CENTER", 0, (BlinkRogueHelper_FontSizePixel*10)*flowTime-50 );

	local fadeInTime = self.fadeInTime;
	if ( elapsed < fadeInTime ) then
		local alpha = (elapsed / fadeInTime);
		self:SetAlpha(alpha);
		return;
	end
	local holdTime = self.holdTime;
	if ( elapsed < (fadeInTime + holdTime) ) then
		self:SetAlpha(1.0);
		return;
	end
	local fadeOutTime = self.fadeOutTime;
	if ( elapsed < (fadeInTime + holdTime + fadeOutTime) ) then
		local alpha = 1.0 - ((elapsed - holdTime - fadeInTime) / fadeOutTime);
		self:SetAlpha(alpha);
		return;
	end
	self:Hide();
end

function BlinkRogueHelper_Type3(self)
	local elapsed = GetTime() - self.startTime;
	local flowTime = elapsed / self.flowTime;
	local xpos = math.sin(self.PI*2*flowTime) * 40;

	self:SetPoint("CENTER", "UIParent", "CENTER", xpos, (BlinkRogueHelper_FontSizePixel*4)*flowTime-60 );

	local fadeInTime = self.fadeInTime;
	if ( elapsed < fadeInTime ) then
		local alpha = (elapsed / fadeInTime);
		self:SetAlpha(alpha);
		return;
	end
	local holdTime = self.holdTime;
	if ( elapsed < (fadeInTime + holdTime) ) then
		self:SetAlpha(1.0);
		return;
	end
	local fadeOutTime = self.fadeOutTime;
	if ( elapsed < (fadeInTime + holdTime + fadeOutTime) ) then
		local alpha = 1.0 - ((elapsed - holdTime - fadeInTime) / fadeOutTime);
		self:SetAlpha(alpha);
		return;
	end
	self:Hide();
end

function BlinkRogueHelper_Type4(self)
	local elapsed = GetTime() - self.startTime;
	local flowTime = elapsed / self.flowTime;
	local xpos = math.sin(self.PI*2*flowTime) * 40;

	self:SetPoint("CENTER", "UIParent", "CENTER", (BlinkRogueHelper_FontSizePixel*4)*flowTime-(BlinkRogueHelper_FontSizePixel*2), 60 );

	local fadeInTime = self.fadeInTime;
	if ( elapsed < fadeInTime ) then
		local alpha = (elapsed / fadeInTime);
		self:SetAlpha(alpha);
		return;
	end
	local holdTime = self.holdTime;
	if ( elapsed < (fadeInTime + holdTime) ) then
		self:SetAlpha(1.0);
		return;
	end
	local fadeOutTime = self.fadeOutTime;
	if ( elapsed < (fadeInTime + holdTime + fadeOutTime) ) then
		local alpha = 1.0 - ((elapsed - holdTime - fadeInTime) / fadeOutTime);
		self:SetAlpha(alpha);
		return;
	end
	self:Hide();
end

function SetBubbleTextColor()
	local mana = UnitMana("player");
	local r,g,b;

	if( mana >= BRH_ComboSkill )then
		r = 0.1;
		g = 1.0;
		b = 0.1; -- 녹색
	elseif( mana >= BRH_FinishSkill )then
		r = 0.0;
		g = 0.39;
		b = 0.88; -- 파란색
	else
		r = 1.0;
		g = 0.1;
		b = 0.1; -- 빨간색
	end

	BubbleTextString:SetTextColor(r, g, b);
end

function SetBubbleText()
	local combo = GetComboPoints(PlayerFrame.unit, "target");
	BubbleTextString:SetText(combo);
	SetBubbleTextColor();
end

function BlinkRogueHelper_Show(self)
	self.startTime = GetTime();
	self:Show();
end

function BlinkRogueHelper_Register()
	SlashCmdList["BLINKROGUEHELPERSLASHENABLE"] = BlinkRogueHelper_Toggle;
	SLASH_BLINKROGUEHELPERSLASHENABLE1 = "/도적도우미사용";
	SLASH_BLINKROGUEHELPERSLASHENABLE2 = "/blinkroguehelperenable";
	SLASH_BLINKROGUEHELPERSLASHENABLE3 = "/brhuse";

	SlashCmdList["BLINKROGUEHELPERSELECT1"] = BlinkRogueHelper_SelectType1;
	SLASH_BLINKROGUEHELPERSELECT11 = "/도적도우미1";
	SLASH_BLINKROGUEHELPERSELECT12 = "/blinkroguehelper1";
	SLASH_BLINKROGUEHELPERSELECT13 = "/brh1";

	SlashCmdList["BLINKROGUEHELPERSELECT2"] = BlinkRogueHelper_SelectType2;
	SLASH_BLINKROGUEHELPERSELECT21 = "/도적도우미2";
	SLASH_BLINKROGUEHELPERSELECT22 = "/blinkroguehelper2";
	SLASH_BLINKROGUEHELPERSELECT23 = "/brh2";

	SlashCmdList["BLINKROGUEHELPERSELECT3"] = BlinkRogueHelper_SelectType3;
	SLASH_BLINKROGUEHELPERSELECT31 = "/도적도우미3";
	SLASH_BLINKROGUEHELPERSELECT32 = "/blinkroguehelper3";
	SLASH_BLINKROGUEHELPERSELECT33 = "/brh3";

	SlashCmdList["BLINKROGUEHELPERSELECT4"] = BlinkRogueHelper_SelectType4;
	SLASH_BLINKROGUEHELPERSELECT41 = "/도적도우미4";
	SLASH_BLINKROGUEHELPERSELECT42 = "/blinkroguehelper4";
	SLASH_BLINKROGUEHELPERSELECT43 = "/brh4";

end

function BlinkRogueHelper_BM_Register()

	if _G.BlinkConfig then
		_G.BlinkConfig:RegisterOptions{
			name = "도적 도우미",
			type = "root",
			command = {"도적도우미","blinkroguehelper","brh"},
			options = {
				{
					type = "description",
					text = "블링크의 도적 콤보 도우미",
					fontobject = "QuestTitleFont",
					r = 1,
					g = 0.82,
					b = 0,
					justifyH = "LEFT",
				},
				{
					type = "description",
					text = "도적 또는 표범 드루이드의 연계포인트를 화려하게 표시해줍니다.",
					fontobject = "ChatFontNormal",
					justifyH = "LEFT",
					justifyV = "TOP",
				},
				{
					type = "toggle",
					text = "사용",
					tooltip = "도적 도우미 애드온을 사용합니다.",
					get = function ()
						return BlinkRogueHelper_Enabled
					end,
					set = function (value)
						BlinkRogueHelper_Enabled = value
					end,
					needRefresh = true,
				},
				{
					type = "group",
					disable = function ()
						return not BlinkRogueHelper_Enabled
					end,
					options = {
						{
							type = "radio",
							text = "움직임 타입 선택",
							values = {
								{
									text = "움직임 타입1",
									value = 1,
								},
								{
									text = "움직임 타입2",
									value = 2,
								},
								{
									text = "움직임 타입3",
									value = 3,
								},
								{
									text = "움직임 타입4",
									value = 4,
								},
							},
							get = function ()
								return BlinkRogueHelper_Type
							end,
							set = function (value)
								BlinkRogueHelper_Type = value
								if value == 1 then
									BlinkRogueHelper_SelectType1()
								elseif value == 2 then
									BlinkRogueHelper_SelectType2()
								elseif value == 3 then
									BlinkRogueHelper_SelectType3()
								elseif value == 4 then
									BlinkRogueHelper_SelectType4()
								end
							end,
							col = 2,
						},
					},
				},
			},
		}
	end
end

function BlinkRogueHelper_Toggle()
	if( not BlinkRogueHelper_Enabled or BlinkRogueHelper_Enabled ~= 1 ) then
		BlinkRogueHelper_Enabled = 1;
		ChatFrame1:AddMessage("도적 연계포인트 도우미를 활성화 합니다.");
	else
		BlinkRogueHelper_Enabled = 0;
		ChatFrame1:AddMessage("도적 연계포인트 도우미를 비활성화 합니다.");
	end
end

function BlinkRogueHelper_SelectType1()
	BlinkRogueHelper_Type = 1;
	BlinkRogueHelper_FontSizePixel=70;
	BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel);
end
function BlinkRogueHelper_SelectType2()
	BlinkRogueHelper_Type = 2;
	BlinkRogueHelper_FontSizePixel=70;
	BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel);
end
function BlinkRogueHelper_SelectType3()
	BlinkRogueHelper_Type = 3;
	BlinkRogueHelper_FontSizePixel=70;
	BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel);
end
function BlinkRogueHelper_SelectType4()
	BlinkRogueHelper_Type = 4;
	BlinkRogueHelper_FontSizePixel=70;
	BubbleTextString:SetTextHeight(BlinkRogueHelper_FontSizePixel);
end

-------------------------------------------------------------------------------
function BRH_BM_EnableClick()
	BlinkRogueHelper_Toggle();
end
function BRH_BM_EnableCheck()
	if( not BlinkRogueHelper_Enabled or BlinkRogueHelper_Enabled ~= 1 ) then
		BlinkRogueHelper_Enabled = 0;
		return false;
	else
		return true;
	end
end

function BRH_BM_TypeClick(t)
	if ( t == 2 ) then
		BlinkRogueHelper_SelectType2();
	elseif ( t == 3 ) then
		BlinkRogueHelper_SelectType3();
	elseif ( t == 4 ) then
		BlinkRogueHelper_SelectType4();
	else
		BlinkRogueHelper_SelectType1();
	end
end
function BRH_BM_TypeCheck(t)
	if ( not BlinkRogueHelper_Type ) then
		BlinkRogueHelper_Type = 1;
	end
	if ( tonumber(BlinkRogueHelper_Type) == tonumber(t) ) then
		return true;
	end
end

function BRH_BM_Type1Click() BRH_BM_TypeClick(1); end
function BRH_BM_Type2Click() BRH_BM_TypeClick(2); end
function BRH_BM_Type3Click() BRH_BM_TypeClick(3); end
function BRH_BM_Type4Click() BRH_BM_TypeClick(4); end

function BRH_BM_Type1Check(d) BRH_BM_TypeCheck(1); end
function BRH_BM_Type2Check(d) BRH_BM_TypeCheck(2); end
function BRH_BM_Type3Check(d) BRH_BM_TypeCheck(3); end
function BRH_BM_Type4Check(d) BRH_BM_TypeCheck(4); end
