--[[ $Id: Core.lua 20403 2006-12-14 00:10:09Z nevcairiel $ ]]
Bartender3 = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceDB-2.0", "AceConsole-2.0", "FuBarPlugin-2.0")

Bartender3.version = "3.0." .. string.sub("$Revision: 20403 $", 12, -3)
Bartender3.date = string.sub("$Date: 2006-12-14 09:10:09 +0900 (목, 14 12 2006) $", 8, 17)
Bartender3.hasIcon = "Interface\\Icons\\INV_Drink_05"
Bartender3.hasNoColor = true
Bartender3.defaultMinimapPosition = 285
Bartender3.clickableTooltip = false
Bartender3.hideWithoutStandby = true
Bartender3.independentProfile = true
Bartender3.cannotDetachTooltip = true

local dewdrop = AceLibrary("Dewdrop-2.0")
local L = AceLibrary("AceLocale-2.2"):new("Bartender3")
local _G = getfenv(0)

function Bartender3:OnInitialize()
	self:RegisterDB("BT3DB")
	
	_, self.playerclass = UnitClass("player")
	
	self:SetupDefaults()
	self:PopulateOptions()
	
	self.unlock = false
	self.actionbars, self.actionbuttons, self.specialbars, self.bars = {}, {}, {}, {}
	for i=1,120 do
		self.actionbuttons[i] = { state = "unused", object = nil}
	end

	for i=1,10 do
		if self.db.profile.Bars[i].Enabled then
			local bar = Bartender3.Class.ActionBar:new(i, self.db.profile.Bars[i], self.db.profile.Bars[i].Buttons or 12)
			self.actionbars[i] = bar
		else
			-- feed a dummy config entry into the options table, so that we can actually activate it.
			self:CreateDisabledBarOptions(i)
		end
	end
	
	self:RemoveDefaultUIElements()
end

function Bartender3:RemoveDefaultUIElements()
	-- Hide the main grafics
	MainMenuBarArtFrame:Hide()
	MainMenuBar:Hide()
	
	self.specialbars["STANCE"] = 	Bartender3.Class.SpecialBar:new("STANCE", "StanceBar", self.db.profile.SpecialBars["STANCE"])
	self.specialbars["PET"] = 		Bartender3.Class.SpecialBar:new("PET", "PetBar", self.db.profile.SpecialBars["PET"])
	self.specialbars["MICROMENU"] =	Bartender3.Class.SpecialBar:new("MICROMENU", "MicroMenu", self.db.profile.SpecialBars["MICROMENU"])
	self.specialbars["BAGS"] =		Bartender3.Class.SpecialBar:new("BAGS", "BagBar", self.db.profile.SpecialBars["BAGS"])
end

function Bartender3:OnEnable()
	self:RegisterEvent("PLAYER_REGEN_DISABLED", "CombatLockdown")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "ScanBindings")
	self:RegisterEvent("UPDATE_BINDINGS", "ScanBindings")
	self:RegisterEvent("AceEvent_FullyInitialized")
	self:RefreshBars()
	self:Print("WARNING: This is a development version of Bartender3 and comes with absolutely NO support in any form. Please DO NOT report bugs or anything on the forums or irc. If you want an reliable version, use the official releases.")
end

function Bartender3:AceEvent_FullyInitialized()
	self:RemapBlizzardBindings()
	self:ScanBindings()
end

-- Binding remapping
-- BottomLeft - page6 - MULTIACTIONBAR1BUTTON(1-12)
-- BottomRight - page5 - MULTIACTIONBAR2BUTTON(1-12)
-- Right - page 3 - MULTIACTIONBAR3BUTTON(1-12)
-- Left - page 4 - MULTIACTIONBAR4BUTTON(1-12)

function Bartender3:RemapBlizzardBindings()
	_G["BINDING_HEADER_MULTIACTIONBAR"] =  "Bartender3 Default Bars"
	for bar=2,10 do
		for i=1,12 do
			if (bar >= 3 and bar <= 6) then
				local k
				if bar == 6 then k = 1
				elseif bar == 5 then k = 2
				else k = bar end
				_G["BINDING_NAME_MULTIACTIONBAR"..k.."BUTTON"..i] = "Bartender3 Bar" .. bar .. " Button " .. i
			else
				_G["BINDING_NAME_BT3BAR"..((bar==10) and 0 or bar).."BUTTON"..i] = "Bartender3 Bar" .. bar .. " Button " .. i
			end
		end
	end
end

function Bartender3:ScanBindings()
	if InCombatLockdown() then return end
	-- would i need to reset all overwrites before? hmm
	ClearOverrideBindings(self.frame)
	local key1, key2
	for i=1,12 do
		key1, key2 = GetBindingKey("ACTIONBUTTON"..i)
		if key1 then self:SetOverrideBinding(1, i, key1) end
		if key2 then self:SetOverrideBinding(1, i, key2) end
		for bar=2,10 do
			local binding
			if (bar >= 3 and bar <= 6) then
				local k
				if bar == 6 then k = 1
				elseif bar == 5 then k = 2
				else k = bar end
				binding = "MULTIACTIONBAR"..k.."BUTTON"..i
			else
				binding = "BT3BAR"..(bar==10 and 0 or bar).."BUTTON"..i
			end
			key1, key2 = GetBindingKey(binding)
			if key1 then self:SetOverrideBinding(bar, i, key1) end
			if key2 then self:SetOverrideBinding(bar, i, key2) end
		end
	end
end

function Bartender3:SetOverrideBinding(bar, button, key)
	if not bar or not button or not key then return end
	if not self.actionbars[bar] then return end
	
	local frame = self.actionbars[bar].buttons[button]
	
	if frame then
		SetOverrideBindingClick(self.frame, false, key, frame:GetName()) 
	end
end

function Bartender3:OnProfileEnable()
	self:LockBars()
	for i=1,10 do
		-- bar disabling logic, lots of cases -.-
		local config = self.db.profile.Bars[i]
		-- bar should be enabled, and was enabled
		if config.Enabled and self.actionbars[i] and self.actionbars[i].config.Enabled then 
			self.actionbars[i]:ChangeProfile(config)
		-- bar should be enabled, but was not, and object not existent
		elseif config.Enabled and not self.actionbars[i] then
			self.actionbars[i] = Bartender3.Class.ActionBar:new(i, config, config.Buttons or 12)
		-- bar should be enabled, but was not, object exists, but was set to disabled
		elseif config.Enabled and not self.actionbars[i].config.Enabled then
			self.actionbars[i]:ChangeProfile(config)
			self.actionbars[i]:InitOptions()
		-- bar should be disabled, and was enabled
		elseif not config.Enabled and self.actionbars[i] and self.actionbars[i].config.Enabled then
			self.actionbars[i]:ChangeProfile(config)
			self:CreateDisabledBarOptions(i)
		-- bar should be disabled, was disabled, but object does exist
		elseif not config.Enabled and self.actionbars[i] then
			self.actionbars[i]:ChangeProfile(config)
		end
	end
	for i,v in pairs(self.specialbars) do
		v:ChangeProfile(self.db.profile.SpecialBars[i])
	end
	self:ScanBindings()
end

function Bartender3:CombatLockdown()
	if self.unlock then
		self:LockBars()
		self:Print("ActionBars have been locked because you entered combat.")
		-- close any dewdrop menu owned by us
		local parent = dewdrop:GetOpenedParent()
		for i,v in pairs(self.bars) do
			if parent == v.frame then
				dewdrop:Close()
			end
		end
	end
end

function Bartender3:GetFreeButton(bar)
	if bar then
		for i=1,12 do
			local button = ((bar.id-1)*12)+i
			if ( self.actionbuttons[button].state == "unused" ) then 
				local newbutton = Bartender3.Class.Button:new(bar, button)
				self.actionbuttons[button].state = "used"
				self.actionbuttons[button].object = newbutton
				return newbutton
			elseif ( self.actionbuttons[button].state == "free" ) then 
				self.actionbuttons[button].state = "used"
				self.actionbuttons[button].object:AssignButton(bar, button)
				return self.actionbuttons[button].object
			end
		end
	end
end

function Bartender3:UnlockBars()
	for i,v in pairs(self.bars) do
		if v.config.Enabled then v:UnlockFrames() end
	end
	self.unlock = true
	self:UpdateDisplay()
end

function Bartender3:LockBars()
	for i,v in pairs(self.bars) do
		if v.config.Enabled then v:LockFrames() end
	end
	self.unlock = nil
	self:UpdateDisplay()
end

function Bartender3:RefreshBars(bar)
	if bar then bar:RefreshLayout() else
		for i,v in pairs(self.bars) do
			if v.config.Enabled then v:RefreshLayout() end
		end
	end
end

function Bartender3:GetConfig(index, special)
	if special then
		return self.db.profile.SpecialBars[index]
	else
		return self.db.profile.Bars[index]
	end
end

function Bartender3:GetBarObject(index, special)
	if special then
		return self.specialbars[index]
	else
		return self.actionbars[index]
	end
end

function Bartender3:SetZoom(zoom)
	self.db.profile.Zoomed = zoom
	for i,v in pairs(self.bars) do
		if v.config.Enabled then v:ButtonZoomCheck() end
	end
end

function Bartender3:ToggleBarEnabled(barid)
	if InCombatLockdown() then return end
	
	local bar, barconfig = self.actionbars[barid], self.db.profile.Bars[barid]
	if bar and barconfig.Enabled then -- disable the bar
		barconfig.Enabled = false
		bar:LockFrames()
		bar.frame:Hide()
		self:CreateDisabledBarOptions(barid)
	elseif bar then -- bar was disabled before
		barconfig.Enabled = true
		bar:InitOptions()
		bar:ChangeProfile(barconfig) -- refresh config
		if self.unlock then bar:UnlockFrames() end
	else -- bar doesnt exist yet
		barconfig.Enabled = true
		bar = Bartender3.Class.ActionBar:new(barid, barconfig, barconfig.Buttons or 12)
		self.actionbars[barid] = bar
		if self.unlock then bar:UnlockFrames() end
	end
	self:ScanBindings()
end

local Tablet = AceLibrary("Tablet-2.0")
-- FuBar Stuff
function Bartender3:OnTooltipUpdate()
	local cat = Tablet:AddCategory("columns", 2)
	cat:AddLine("text", "Button lock", "text2", self:GetOnOffText(self.db.profile.ButtonLock))
	cat:AddLine("text", "Bar lock", "text2", self:GetOnOffText(not self.unlock))
	Tablet:SetHint("|cffeda55fClick|r to toggle button lock. |cffeda55fShift-Click|r to toggle bar lock.")
end

function Bartender3:GetOnOffText(bool)
	return bool and "|cff00ff00On|r" or "|cffff0000Off|r"
end

function Bartender3:OnClick(button)
	if IsShiftKeyDown() then
		if Bartender3.unlock then
			self:LockBars()
		else
			self:UnlockBars()
		end
	else
		self:LockButtons()
	end
	self:UpdateDisplay()
end

function Bartender3:LockButtons(silence)
	if self.db.profile.ButtonLock then
		self.db.profile.ButtonLock = false
		if not silence then self:Print("ActionBar lock |cffffffcf[|r|cffff0000Off|cffffffcf]|r") end
	else
		self.db.profile.ButtonLock = true
		if not silence then self:Print("ActionBar lock |cffffffcf[|r|cff00ff00On|cffffffcf]|r") end
	end
end

