local addonName, addon = ...



--[[-----------------------------------------------------------------------------
This is used to set the default button ids . These buttons do not conflict with
known stance/form/stealth bars.  These settings are not ideal, but are the best
compromise I could come up with short of writing virtual action buttons.

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 contextMenu = CreateFrame('Frame', nil, UIParent, 'SecureFrameTemplate')
contextMenu:SetPoint('TOP', GUIAnchorPoint7, 'BOTTOM', 0, 0)

local buttons = { }
for index = 1, #actionID do
	local button = CreateFrame('CheckButton', addonName .. "ContextMenuButton" .. index, contextMenu, '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

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

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

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

--[[-----------------------------------------------------------------------------
Context "hot spot" button
-------------------------------------------------------------------------------]]
local hotSpot = CreateFrame('Button', nil, UIParent, 'SecureHandlerClickTemplate')

hotSpot:SetHeight(16)
hotSpot:SetWidth(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:SetHeight(43)
HotBG:SetWidth(43)
HotBG:Hide()

hotSpot:SetFrameRef("contextMenu", contextMenu)
hotSpot:SetAttribute('_onclick', [[
	local frame = self:GetFrameRef("contextMenu")
	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 ContextMenu", 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()
		
		if(Minimap:IsShown()) then
			PlaySound("igMiniMapClose");
			Minimap:Hide();
			_G["GrimUIcoreArtMM"]:Hide()
			addon.settings.ShowMiniMap = false 
		else
			PlaySound("igMiniMapOpen");
			addon:ShowFrame(Minimap);
			if not addon.settings.NotSoMinimap or addon.settings.NotSoMinimap == false then
				_G["GrimUIcoreArtMM"]:Show()
			end
			addon.settings.ShowMiniMap = true 
		end

end



--[[-----------------------------------------------------------------------------
Initialize
-------------------------------------------------------------------------------]]
addon.RegisterEvent("ContextMenu-Initialize", 'PLAYER_ENTERING_WORLD', function(self, event)
	addon.UnregisterEvent(self, event)

	if not addon.settings.showContextMenu then
		contextMenu:Hide()
	end
	addon:HotSpotPOS()
end)

--[[-----------------------------------------------------------------------------
Save setting on logout
-------------------------------------------------------------------------------]]
addon.RegisterEvent("ContextMenu-SaveState", 'PLAYER_LOGOUT', function()
	addon.settings.showContextMenu = contextMenu:IsShown() or false
end)
