ToolsCenterBar = {
	Version = "07.09.29_R4",
	btns = {},
        db = "ToolsCenterBarDB",
	bind = "TOOLSCENTERBUTTON",
        button = "ToolsCenterButton",
	togglebind = "TOOLSCENTERTOGGLE",
	togglebutton = "ToolsCenterToggle",
        mainframe = "ToolsCenterBarFrame",
}
local addon = ToolsCenterBar

-- 이 위에까지만 수정하시면 됩니다

local function GetStart(v) if v == 1 then return addon.db.Alpha else return 1 end end
local function GetEnd(v) if v == 0 then return addon.db.Alpha else return 1 end end
local function FadeStart(v) UIFrameFadeIn(addon.mainframe, addon.db.FadeTime, GetStart(v), GetEnd(v)) end

addon.mainframe = CreateFrame("Frame", addon.mainframe, UIParent)
addon.mainframe:RegisterEvent("VARIABLES_LOADED")
addon.mainframe:RegisterEvent("PLAYER_ENTERING_WORLD")
addon.mainframe:RegisterEvent("UPDATE_BINDINGS")
addon.mainframe:RegisterEvent("PLAYER_TARGET_CHANGED")
addon.mainframe:RegisterEvent("PLAYER_REGEN_ENABLED")
addon.mainframe:RegisterEvent("PLAYER_REGEN_DISABLED")
addon.mainframe:RegisterEvent("ACTIONBAR_SHOWGRID")
addon.mainframe:RegisterEvent("ACTIONBAR_HIDEGRID")
addon.mainframe:SetScript("OnEvent", function()
	if event == "VARIABLES_LOADED" then
		if not getglobal(addon.db) then
			addon:Default()
		end
		addon.db = getglobal(addon.db)
		addon.db.tilt = addon.db.tilt or 20
		addon:OnLoad()
		this:UnregisterEvent(event)
	elseif (event == "PLAYER_ENTERING_WORLD") then
		if not UnitAffectingCombat("player") then
			addon:SetAlpha()
		end
		this:UnregisterEvent(event)
	elseif (event == "UPDATE_BINDINGS") then
		ClearOverrideBindings(addon.toggle)
		local v1 = {GetBindingKey(addon.togglebind)}
		if #v1 ~= 0 then
			for i = 1, #v1 do
				SetOverrideBindingClick(addon.toggle, false, v1[i], addon.togglebutton)
			end
		end
		for i = 1, 12 do
			this = addon.btns[i]
			ClearOverrideBindings(this)
			local v = {GetBindingKey(addon.bind..i)}
			if #v ~= 0 then
				for i = 1, #v do
					SetOverrideBindingClick(this, false, v[i], this:GetName())
				end
			end
		end
	end
	if addon.db.UseEvent then
		if addon.db.hide then return end
		if (event == "PLAYER_TARGET_CHANGED") then
			if UnitAffectingCombat("player") then return end
			if not UnitIsVisible("target") then
				if addon.mainframe:GetAlpha() ~= 1 then return end
				FadeStart(0)
				return
			end
			if (UnitCanAttack("player", "target")) then
				if addon.mainframe:GetAlpha() == 1 then return end
				FadeStart(1)
				return
			end
		elseif (event == "PLAYER_REGEN_DISABLED") then
			if addon.mainframe:GetAlpha() ~= 1 then
				FadeStart(1)
			end
		elseif (event == "PLAYER_REGEN_ENABLED") then
			if getglobal(addon.button..1).showgrid == 1 then return end
			if UnitIsVisible("target") and UnitCanAttack("player", "target") then return end
			FadeStart(0)
		elseif (event == "ACTIONBAR_SHOWGRID") then
			if UnitAffectingCombat("player") or addon.mainframe:GetAlpha() == 1 then return end
			FadeStart(1)
		elseif (event == "ACTIONBAR_HIDEGRID") then
			if UnitAffectingCombat("player") or addon.mainframe:GetAlpha() ~= 1 or (UnitIsVisible("target") and UnitCanAttack("player", "target")) then return end
			FadeStart(0)
		end
	end
end)


function addon:Default()
	setglobal(addon.db, {
		SetID = 2,
		Overlay = 5,
		Layout = 1,
		GapButton = 8,
		GapWidth = 150,
		YPoint = 100,
		FadeTime = 0.2,
		Scale = 0.9,
		Alpha = 0.2,
		UseEvent = true,
		tilt = 20
	})
end
function addon:OnLoad()
	addon.mainframe:SetParent(UIParent)
	self:CreateButton()
	self:CreateToggleButton()
	self:SetID()
	self:SetTexture()
	self:SetScale()
	self:SetPoint()
end
function addon:SetAlpha()
	addon.mainframe:SetAlpha(self.db.UseEvent and self.db.Alpha or 1)
end
function addon:SetScale()
	addon.mainframe:SetScale(self.db.Scale)
end
function addon:SetID()
	local id
	if self.db.SetID == 1 then
		id = 1
	elseif self.db.SetID == 2 then
		id = 9
	elseif self.db.SetID == 3 then
		id = 6
	elseif self.db.SetID == 4 then
		id = 7
	elseif self.db.SetID == 5 then
		id = 8
	end
	for i = 1, 12 do
		getglobal(self.button..i):SetScript("OnAttributeChanged", ActionButton_UpdateAction)
		getglobal(self.button..i):SetAttribute("action", i+(12*id))
		getglobal(self.button..i):SetScript("OnAttributeChanged", nil)
	end
end
function addon:ButtonSetPoint(btn, p1, p, p2, x, y)
	btn:ClearAllPoints()
	btn:SetPoint(p1, p, p2, x, y)
end
function addon:SetPoint()
	local id = self.db.Layout
	if id <= 4 then
		self:ButtonSetPoint(self.btns[1], "RIGHT", UIParent, "CENTER", -self.db.GapWidth, self.db.YPoint)
		self:ButtonSetPoint(self.btns[7], "LEFT", UIParent, "CENTER", self.db.GapWidth, self.db.YPoint)
	else
		self:ButtonSetPoint(self.btns[1], "CENTER", UIParent, "CENTER", self.db.GapWidth, self.db.YPoint)
		self:ButtonSetPoint(self.btns[7], "LEFT", self.btns[6], "RIGHT", self.db.GapButton, 0)
	end
	if id == 1 then
		self:ButtonSetPoint(self.btns[2], "TOP", self.btns[1], "BOTTOM", -(self.db.tilt), -self.db.GapButton)
		self:ButtonSetPoint(self.btns[3], "TOP", self.btns[2], "BOTTOM", -(self.db.tilt/2), -self.db.GapButton)
		self:ButtonSetPoint(self.btns[4], "TOP", self.btns[3], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[5], "TOP", self.btns[4], "BOTTOM", self.db.tilt/2, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[6], "TOP", self.btns[5], "BOTTOM", self.db.tilt, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[8], "TOP", self.btns[7], "BOTTOM", self.db.tilt, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[9], "TOP", self.btns[8], "BOTTOM", self.db.tilt/2, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[10], "TOP", self.btns[9], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[11], "TOP", self.btns[10], "BOTTOM", -(self.db.tilt/2), -self.db.GapButton)
		self:ButtonSetPoint(self.btns[12], "TOP", self.btns[11], "BOTTOM", -(self.db.tilt), -self.db.GapButton)
		return
	elseif id == 2 then
		self:ButtonSetPoint(self.btns[2], "TOP", self.btns[1], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[3], "TOP", self.btns[2], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[4], "TOPRIGHT", self.btns[1], "TOPLEFT", -self.db.GapButton, -self.db.tilt)
		self:ButtonSetPoint(self.btns[5], "TOP", self.btns[4], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[6], "TOP", self.btns[5], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[8], "TOP", self.btns[7], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[9], "TOP", self.btns[8], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[10], "TOPLEFT", self.btns[7], "TOPRIGHT", self.db.GapButton, -self.db.tilt)
		self:ButtonSetPoint(self.btns[11], "TOP", self.btns[10], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[12], "TOP", self.btns[11], "BOTTOM", 0, -self.db.GapButton)
		return
	elseif id >= 3 and id < 5 then
		self:ButtonSetPoint(self.btns[2], "LEFT", self.btns[1], "RIGHT", self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[3], "LEFT", self.btns[2], "RIGHT", self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[5], "LEFT", self.btns[4], "RIGHT", self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[6], "LEFT", self.btns[5], "RIGHT", self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[8], "RIGHT", self.btns[7], "LEFT", -self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[9], "RIGHT", self.btns[8], "LEFT", -self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[11], "RIGHT", self.btns[10], "LEFT", -self.db.GapButton, 0)
		self:ButtonSetPoint(self.btns[12], "RIGHT", self.btns[11], "LEFT", -self.db.GapButton, 0)
		if id == 3 then
			self:ButtonSetPoint(self.btns[4], "TOPRIGHT", self.btns[1], "BOTTOMRIGHT", -self.db.tilt, -self.db.GapButton)
			self:ButtonSetPoint(self.btns[10], "TOPLEFT", self.btns[7], "BOTTOMLEFT", self.db.tilt, -self.db.GapButton)
		else
			self:ButtonSetPoint(self.btns[4], "LEFT", self.btns[3], "RIGHT", self.db.GapButton, 0)
			self:ButtonSetPoint(self.btns[10], "RIGHT", self.btns[9], "LEFT", -self.db.GapButton, 0)
		end
		return
	end
	for i = 2, 12 do
		self:ButtonSetPoint(self.btns[i], "LEFT", self.btns[i-1], "RIGHT", self.db.GapButton, 0)
	end
	if id == 5 then
		return
	elseif id == 6 then
		self:ButtonSetPoint(self.btns[7], "TOP", self.btns[1], "BOTTOM", 0, -self.db.GapButton)
	elseif id == 7 then
		self:ButtonSetPoint(self.btns[5], "TOP", self.btns[1], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[9], "TOP", self.btns[5], "BOTTOM", 0, -self.db.GapButton)
	elseif id == 8 then
		self:ButtonSetPoint(self.btns[4], "TOP", self.btns[1], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[7], "TOP", self.btns[4], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[10], "TOP", self.btns[7], "BOTTOM", 0, -self.db.GapButton)
	elseif id == 9 then
		self:ButtonSetPoint(self.btns[3], "TOP", self.btns[1], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[5], "TOP", self.btns[3], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[7], "TOP", self.btns[5], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[9], "TOP", self.btns[7], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[11], "TOP", self.btns[9], "BOTTOM", 0, -self.db.GapButton)
	elseif id == 10 then
		self:ButtonSetPoint(self.btns[2], "TOP", self.btns[1], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[3], "TOP", self.btns[2], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[4], "TOP", self.btns[3], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[5], "TOP", self.btns[4], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[6], "TOP", self.btns[5], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[7], "TOP", self.btns[6], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[8], "TOP", self.btns[7], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[9], "TOP", self.btns[8], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[10], "TOP", self.btns[9], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[11], "TOP", self.btns[10], "BOTTOM", 0, -self.db.GapButton)
		self:ButtonSetPoint(self.btns[12], "TOP", self.btns[11], "BOTTOM", 0, -self.db.GapButton)
	end
end
function addon:CreateToggleButton()
--	self.toggle = CreateFrame("Button", self.togglebutton, UIParent, "SecureAnchorUpDownTemplate")
	self.toggle = CreateFrame("Button", self.togglebutton, UIParent, "SecureHandlerClickTemplate")
--	self.toggle.hdr = CreateFrame("Frame", nil, UIParent, "SecureStateHeaderTemplate")
	self.toggle.hdr = CreateFrame("Frame", nil, UIParent, "SecureHandlerStateTemplate")
	self.mainframe:SetAttribute("showstates", "1")

	self.toggle.hdr:SetAttribute("addchild", self.mainframe)
	self.toggle.hdr:SetAttribute("statemap-anchor-mousedown", "1:0;0:1")

	self.toggle:SetAttribute("anchorchild", self.toggle.hdr)
	self.toggle:SetAttribute("onmousedownbutton1", "mdn")
	self.toggle:SetAttribute("childstate-mdn", "^mousedown")
	self.toggle:SetAttribute("childverify-mdn", true)
	self.toggle:SetScript("PreClick", function()
		self.db.hide = not self.db.hide
	end)
	self.toggle.hdr:SetAttribute("state", self.db.hide and 0 or 1)
end
function addon:CreateButton()
	for i = 1, 12 do
		self.btns[i] = CreateFrame("CheckButton", self.button..i, self.mainframe, "SecureActionButtonTemplate, ActionBarButtonTemplate")
		local f = CreateFrame("Frame", nil, self.btns[i])
		self.mainframe:SetAttribute("addchild", self.btns[i])
		self.btns[i]:SetFrameStrata("BACKGROUND")
		self.btns[i].id = i
		f.p = self.btns[i]
		self.btns[i].buttonType = self.bind
		f:SetScript("OnUpdate", function()
			local enable
			if (self.db.UseEvent) then
				if self.mainframe:GetAlpha() ~= 0 and not this.enable then
					this.enable = true
					this.p:RegisterForClicks("AnyUp")
					this.p:RegisterForDrag("LeftButton", "RightButton")
				elseif self.mainframe:GetAlpha() == 0 and this.enable then
					this.enable = nil
					this.p:RegisterForClicks("")
					this.p:RegisterForDrag("")
				end
			end
		end)
	end
end
function addon:SetTexture()
	if not (ToolsCommonButton) then return end
	for i = 1, 12 do
		ToolsCommonButton:SetButtonTexture(self.btns[i], self.db.Overlay, self.db.BT3)
	end
end

function ToolsCenterBarBindDummy()
	-- 단축키 표시 더미 함수명 변경하지 않아도 됩니다
end

-- addon.bind 에 맟춰놓은 문자열로 설정하세요
-- binding.xml 수정하시는거 잊지 마세요

BINDING_HEADER_TOOLSCENTERBARBIND = "중앙버튼 단축키"
BINDING_NAME_TOOLSCENTERTOGGLE = "센터바 토글"
BINDING_NAME_TOOLSCENTERBUTTON1 = "버튼 1"
BINDING_NAME_TOOLSCENTERBUTTON2 = "버튼 2"
BINDING_NAME_TOOLSCENTERBUTTON3 = "버튼 3"
BINDING_NAME_TOOLSCENTERBUTTON4 = "버튼 4"
BINDING_NAME_TOOLSCENTERBUTTON5 = "버튼 5"
BINDING_NAME_TOOLSCENTERBUTTON6 = "버튼 6"
BINDING_NAME_TOOLSCENTERBUTTON7 = "버튼 7"
BINDING_NAME_TOOLSCENTERBUTTON8 = "버튼 8"
BINDING_NAME_TOOLSCENTERBUTTON9 = "버튼 9"
BINDING_NAME_TOOLSCENTERBUTTON10 = "버튼 10"
BINDING_NAME_TOOLSCENTERBUTTON11 = "버튼 11"
BINDING_NAME_TOOLSCENTERBUTTON12 = "버튼 12"