ToolsRCVersion = "07.10.03_R5"

local f = CreateFrame("Frame")
f:RegisterEvent("VARIABLES_LOADED")

f:SetScript("OnEvent", function()
	if event == "VARIABLES_LOADED" then
		ToolsRCDB = ToolsRCDB or
			{	hotkey = {1,1,1},
				name = {1,1,1},
				count = {1,1,1},
				arrow = 1,
				rc = true,
			}
		if not ToolsRCDB.size then
			ToolsRCDB.size = {
				hotkey = 14,
				name = 10,
				count = 12,
			}
			ToolsRCDB.outline = {
				hotkey = true,
				name = true,
				count = true,
			}
		end
		if not (IsAddOnLoaded("OmniCC") or IsAddOnLoaded("!OmniCC")) then
		-- OmniCC에서 발췌
			local ActionButton_OnUpdate_Old = ActionButton_OnUpdate;
			function ActionButton_OnUpdate(elapsed)
				local changeRange = false;
				if (this.rangeTimer) then
					if ( this.rangeTimer <= elapsed ) then
						local newRange = nil;
						if ( IsActionInRange( ActionButton_GetPagedID(this) ) == 0) then
							newRange = true;
						end
							if (this.redRangeFlag ~= newRange) then
						this.redRangeFlag = newRange;
							changeRange = true;
						end
					end
				end
				ActionButton_OnUpdate_Old(elapsed);
				if (changeRange) then
					ActionButton_UpdateUsable();
				end
			end
		-- 여기까지 토시하나 안틀리고 배낌
		end
		bak_ActionButton_UpdateUsable = ActionButton_UpdateUsable

		hooksecurefunc("ActionButton_UpdateHotkeys", ActionButton_UpdateHotkeys_TRC)
		ActionButton_UpdateUsable = TB_ActionButton_UpdateUsable
	end
end)

function ToolsRCChangeFont(btn)
	local t = btn or this
	getglobal(t:GetName().."HotKey"):SetFont("Fonts\\2002B.ttf", ToolsRCDB.size.hotkey, ToolsRCDB.outline.hotkey and "OUTLINE")
	getglobal(t:GetName().."Name"):SetFont("Fonts\\2002B.ttf", ToolsRCDB.size.name, ToolsRCDB.outline.name and "OUTLINE")
	getglobal(t:GetName().."Count"):SetFont("Fonts\\2002B.ttf", ToolsRCDB.size.count, ToolsRCDB.outline.count and "OUTLINE")
	getglobal(t:GetName().."HotKey"):SetVertexColor(unpack(ToolsRCDB.hotkey))
end

function TB_ActionButton_UpdateUsable()
	local icon = getglobal(this:GetName().."Icon")
	local hotkey = getglobal(this:GetName().."HotKey")
	local normalTexture = getglobal(this:GetName().."NormalTexture")
	local isUsable, notEnoughMana = IsUsableAction(ActionButton_GetPagedID(this))

	if (IsActionInRange(ActionButton_GetPagedID(this)) == 0) then
		hotkey:SetTextColor(1,.1,.1)
		if not ToolsRCDB.rc then
			local t = hotkey:GetText()
			if not t or t == "" then
				hotkey:SetText(RANGE_INDICATOR)
			end
			return
		end
		if ( isUsable ) then -- 거리가 멀고, 사용 가능한 경우 : 어두운 적색
			icon:SetVertexColor(1, 0.2, 0.2)
		elseif ( notEnoughMana ) then -- 거리가 멀고, 마나가 부족할때 : 어둡게
			icon:SetVertexColor(0.3, 0.3, 1)
		else -- 거리가 멀고, 마나는 있지만 사용조건이 안맞을경우 색을 어둡게(ex, 제압/복수/반격)
			icon:SetVertexColor(0.5, 0.3, 0.3)
		end
	else
		if hotkey:GetText() == RANGE_INDICATOR then
			hotkey:SetText("")
		end
		hotkey:SetTextColor(unpack(ToolsRCDB.hotkey))
		if ( isUsable ) then
			icon:SetVertexColor(1.0, 1.0, 1.0)
		elseif ( notEnoughMana ) then
			icon:SetVertexColor(0.5, 0.5, 1.0)
		else
			icon:SetVertexColor(0.4, 0.4, 0.4)
		end
	end
end

function ActionButton_UpdateHotkeys_TRC(actionButtonType)
	if not this.changeFont then
		getglobal(this:GetName().."HotKey").SetVertexColor = function()	end
		ToolsRCChangeFont()
		ToolsRCChangeColor_Load()
		this.changeFont = true
	end
	if ( not actionButtonType ) then
		actionButtonType = "ACTIONBUTTON";
	end

	getglobal(this:GetName().."HotKey"):Show()

	local text = GetBindingText(	GetBindingKey(actionButtonType..(this.id or this:GetID())) or
						GetBindingKey("CLICK "..this:GetName()..":LeftButton"),
						"KEY_", 1)
	text = string.gsub(text, "마우스 가운데 버튼", "M3")
	text = string.gsub(text, "4번 마우스 버튼", "M4")
	text = string.gsub(text, "5번 마우스 버튼", "M5")
	text = string.gsub(text, "마우스 휠 아래로", (ToolsRCDB.arrow == 1 and "▼") or "Wd")
	text = string.gsub(text, "마우스 휠 위로",  (ToolsRCDB.arrow == 1 and "▲") or "Wu")
	text = string.gsub(text, "숫자패드% ", "n-")
	text = string.gsub(text, "스페이스 바", "Sp")
	text = string.gsub(text, "Backspace", "Bs")
	text = string.gsub(text, "Page Up", "pU")
	text = string.gsub(text, "Page Down", "pD")
	text = string.gsub(text, "Home", "Hm")
	text = string.gsub(text, "Insert", "In")
	text = string.gsub(text, "Delete", "Dl")
	text = string.gsub(text, "End", "Ed")
	getglobal(this:GetName().."HotKey"):SetText(text)
end

function ToolsRC_ChangeColor(part, btn)
	getglobal((btn or this:GetName())..part):SetTextColor(unpack(ToolsRCDB[strlower(part)]))
end
function ToolsRCChangeColor_Load()
	ToolsRC_ChangeColor("Name")
	ToolsRC_ChangeColor("Count")
	ToolsRC_ChangeColor("HotKey")
end