local addonName, addon = ...

--[[-----------------------------------------------------------------------------
This is used to set the default button ids . These buttons do not conflict with
known stance/form/stealth bars.

Default Blizzard IDs:
1-12		Main Bar (Unused by Warriors)
13-24		Second Bar
25-36		Side Bar 1
37-48		Side Bar 2
49-60		Bottom Right Bar
61-72		Bottom Left Bar
73-84		Warrior Battle, Druid Cat, Rogue Stealth, Priest Shadowform
85-96		Warrior Defensive, Druid Tree
97-108	Warrior Berserker, Druid Bear
109-120	Druid Moonkin
121-132	Possess Bar (Unusable)
-------------------------------------------------------------------------------]]
local actionID, class = UnitClass('player')

if class == 'DRUID' then
	actionID = {
		48, --01
		49, --02
		50, --03
		51, --04
		52, --05
		53, --06
		54, --07
		55, --08
		56, --09
		57, --10
		58, --11
		59, --12
		60, --13
		61, --14
		62, --15
		63, --16
		64, --17
		65, --18
		66, --19
		67, --20
		68, --21
		69, --22
		70, --23
		71, --24
		72  --25
	}
elseif class == 'WARRIOR' then
	actionID = {
		1, --01
		2, --02
		3, --03
		4, --04
		5, --05
		6, --06
		7, --07
		8, --08
		9, --09
		10, --10
		11, --11
		12, --12
		72, --13, Pesky warriors.
		109, --14
		110, --15
		111, --16
		112, --17
		113, --18
		114, --19
		115, --20
		116, --21
		117, --22
		118, --23
		119, --24
		120  --25
	}
else
	actionID = {
		96, --01
		97, --02
		98, --03
		99, --04
		100, --05
		101, --06
		102, --07
		103, --08
		104, --09
		105, --10
		106, --11
		107, --12
		108, --13
		109, --14
		110, --15
		111, --16
		112, --17
		113, --18
		114, --19
		115, --20
		116, --21
		117, --22
		118, --23
		119, --24
		120  --25
	}
end

local GUI_ButtonBin = CreateFrame('Frame', nil, UIParent, 'SecureFrameTemplate')
GUI_ButtonBin:SetPoint('TOP', GUI_AnchorPoint7, 'BOTTOM', 0, 0)

local buttons = { }
for index = 1, #actionID do
	local button = CreateFrame('CheckButton', addonName .. "ContextMenuButton" .. index, GUI_ButtonBin, 'ActionBarButtonTemplate')
	buttons[index] = button
	if (index - 1) % 5 ~= 0 then
		button:SetPoint('LEFT', buttons[index - 1], 'RIGHT', 2, 0)
	elseif index ~= 1 then
		button:SetPoint('TOPLEFT', buttons[index - 5], 'BOTTOMLEFT', 0, -2)
	else
		button:SetPoint('TOPLEFT')
	end
	button:SetAttribute('type', 'action')
	button:SetAttribute('action', actionID[index])
end

GUI_ButtonBin:SetHeight(buttons[1]:GetHeight() * 5 + 8)
GUI_ButtonBin:SetWidth(buttons[1]:GetWidth() * 5 + 8)
GUI_ButtonBin:SetScale(0.8)

local texture = GUI_ButtonBin:CreateTexture(nil, 'ARTWORK')
texture:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]])
texture:SetVertexColor(1, 1, 1, 0.5)
texture:SetPoint('CENTER')
texture:SetSize(36, 36)

local title = GUI_ButtonBin:CreateFontString(nil, 'OVERLAY')
title:SetFont([[Fonts\FRIZQT__.TTF]], 12)
title:SetTextColor(1, 1, 0, 0.8)
title:SetPoint('TOP', texture, 'TOP', 0, -1)
title:SetText('GBB')

--[[-----------------------------------------------------------------------------
Context "hot spot" button
-------------------------------------------------------------------------------]]
local hotSpot = CreateFrame('Button', nil, UIParent, 'SecureHandlerClickTemplate')
hotSpot:SetSize(16, 16)
hotSpot:RegisterForClicks('LeftButtonUp', 'RightButtonUp')

local xPos, yPos
function addon:HotSpotPOS()
	yPos = 220
	xPos = -370 + addon.settings.artSetPoint
	hotSpot:SetPoint('BOTTOMRIGHT', UIParent, 'BOTTOMRIGHT', xPos, yPos)
end

local HotBG = hotSpot:CreateTexture(nil, "BACKGROUND")
HotBG:SetTexture("Interface\\AddOns\\GrimUI\\Media\\HotSpotGlow")
HotBG:SetPoint('CENTER', hotSpot, 'CENTER')
HotBG:SetSize(43, 43)
HotBG:Hide()

hotSpot:SetFrameRef("GUI_ButtonBin", GUI_ButtonBin)
hotSpot:SetAttribute('_onclick', [[
	local frame = self:GetFrameRef("GUI_ButtonBin")
	if button == 'LeftButton' then
		if IsControlKeyDown() then
			control:CallMethod("ToggleMiniMap")
		else
			if frame:IsVisible() then
				frame:Hide(true)
			else
				frame:Show(true)
			end
		end
	elseif button == 'RightButton' then
		if IsControlKeyDown() then
			control:CallMethod("ToggleInfoLog")
		else
		control:CallMethod("ToggleCombatLog")
		end
	end
]])

 hotSpot:SetScript('OnEnter', function(self)
	GameTooltip:SetOwner(self, 'ANCHOR_TOP', 0, 10)
	GameTooltip:AddLine("|cffeda55fLeftClick|r for GUI_ButtonBin", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fRightClick|r toggle CombatLog", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fCtrl+RightClick|r toggle InfoLog", 0.2, 1, 0.2)
	GameTooltip:AddLine("|cffeda55fCtrl+LeftClick|r toggle Minimap", 0.2, 1, 0.2)
	GameTooltip:Show()
	HotBG:Show()
end)

hotSpot:SetScript('OnLeave', addon.HideTooltip)
hotSpot:HookScript('OnLeave', function(self)
	HotBG:Hide()
end)

function hotSpot:ToggleCombatLog()
	addon.settings.showCombatLog = not addon.settings.showCombatLog
	addon:ConfigureCombatLog()
end

function hotSpot:ToggleInfoLog()
	addon.settings.showInfoLog = not addon.settings.showInfoLog
	addon:ConfigureInfoLog()
end

function hotSpot:ToggleMiniMap()
	ToggleMinimap()
end

--[[-----------------------------------------------------------------------------
Initialize
-------------------------------------------------------------------------------]]
addon.RegisterEvent("GUI_ButtonBin-Initialize", 'PLAYER_LOGIN', function(self, event)
	--addon.UnregisterEvent(self, event)
	if not addon.settings.showContextMenu or addon.settings.showContextMenu == false then
		GUI_ButtonBin:Hide()
	end
	addon:HotSpotPOS()
end)

--[[-----------------------------------------------------------------------------
Save setting on logout
-------------------------------------------------------------------------------]]
addon.RegisterEvent("GUI_ButtonBin-SaveState", 'PLAYER_LEAVING_WORLD', function(self, event)
	if GUI_ButtonBin:IsVisible() then addon.settings.showContextMenu = true
		else addon.settings.showContextMenu = false
	end
end)