BT2Circled = Bartender:NewModule("circled")

local _G = getfenv(0)
local bars = {
	["Bar1"] = 12,
	["Bar2"] = 12,
	["Bar3"] = 12,
	["Bar4"] = 12,
	["Bar5"] = 12,
	
	["Bar6"] = 10,
	["Bar7"] = 10,
}
local skins = {
	["Minimap"] = {
		["icon"] 	= { w=28, h=28, x=10, y=-9, },
		["overlay"] = { w=80, h=80, tex="Interface\\Minimap\\MiniMap-TrackingBorder", },
		["equip"] 	= { w=80, h=80, x=0, y=0, tex="Interface\\Minimap\\MiniMap-TrackingBorder", },
		["flash"]	= { w=30, h=30, x=9, y=-8, },
		["cooldown"]= { w=30, h=33, x=8, y=-9, },
		["hotkey"]	= { x=5, y=0, },
		["autocast"]= { w=28, h=28, x=10, y=-9, },
		["autocastable"]= { w=50, h=50, x=-1, y=3, },
		["bagicon"]	= { w=29, h=29, x=9, y=-8, },
		
		["NormalTexture"] 		= { w=1, h=1, x=-4, y=4, },
		["HighlightTexture"] 	= { w=41, h=41, x=4, y=-4, tex="Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", },
		["PushedTexture"] 		= { w=46, h=46, x=-1, y=1, tex="Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", },
		["CheckedTexture"]		= { w=46, h=46, x=-1, y=1, tex="Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", },
	},
	["Serenity"] = {
		["icon"] 	= { w=28, h=28, x=9, y=-9, },
		["overlay"] = { w=46, h=46, tex="Interface\\AddOns\\Bartender2_Circled\\textures\\serenity0", },
		["equip"] 	= { w=46, h=46, x=0, y=0, tex="Interface\\AddOns\\Bartender2_Circled\\textures\\serenity0", },
		["flash"]	= { w=30, h=30, x=8, y=-8, },
		["cooldown"]= { w=28, h=28, x=9, y=-10, },
		["hotkey"]	= { x=6, y=-2, },
		["autocast"]= { w=24, h=27, x=11, y=-12, },
		["autocastable"]= { w=50, h=50, x=-1, y=3, },
		["bagicon"]	= { w=28, h=28, x=9, y=-9, },
		
		["NormalTexture"] 		= { w=1, h=1, x=-3, y=4, },
		["HighlightTexture"] 	= { w=41, h=41, x=2, y=-4, tex="Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", },
		["PushedTexture"] 		= { w=41, h=41, x=2, y=-4, tex="Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", },
		["CheckedTexture"]		= { w=41, h=41, x=2, y=-4, tex="Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight", },
	},
}

function BT2Circled:OnInitialize()
	self.db = Bartender:AcquireDBNamespace('Circled')
	Bartender:RegisterDefaults('Circled', 'profile', {
		hlColor = {
			r = 1,
			g = 0.86,
			b = 0.09,
		},
		hotkeys = true,
		applyskin = {
			Bar1 = true,
			Bar2 = true,
			Bar3 = true,
			Bar4 = true,
			Bar5 = true,
			Bar6 = true,
			Bar7 = true,
			bagbar = true,
		},
		skin = "Minimap",
	})
	Bartender.options.args.circled = {
		name = "Circled skin",
		desc = "Config for Circled skin",
		type = "group",
		args = {
			hlcolor = {
				name = "Highlight color", type = "color",
				desc = "Highlight color.",
				get = function()
					return self.db.profile.hlColor.r, self.db.profile.hlColor.g, self.db.profile.hlColor.b
				end,
				set = function(r, g, b)
					self.db.profile.hlColor.r, self.db.profile.hlColor.g, self.db.profile.hlColor.b = r, g, b
					self:UpdateHLColor()
				end,
				order = 1,
			},
			toggleskin = {
				name = "Toggle",
				desc = "Toggle skin per bar.",
				type = "group",
				args = {
					bar8 = {
						name = "bagbar", type = "toggle",
						desc = "Toggle skin on bagbar",
						get = function()
							return self.db.profile.applyskin.bagbar
						end,
						set = function(v)
							self.db.profile.applyskin.bagbar = v
						end,
					},
				},
			},
			skin = {
				name = "Skin", type = "text",
				desc = "Select button skin.",
				get = function() return self.db.profile.skin end,
				set = function(v)
					self.db.profile.skin = v
					self:ChangeSkin()
				end,
				validate = { "Minimap", "Serenity", },
			},
		}
	}
	
	for k in pairs(bars) do
		local barname = k
		Bartender.options.args.circled.args.toggleskin.args[barname] = {
			name = barname, type = "toggle",
			desc = "Toggle skin on "..barname,
			get = function()
				return self.db.profile.applyskin[barname]
			end,
			set = function(v)
				self.db.profile.applyskin[barname] = v
			end,
		}
	end
	
	self.applied = false
end

function BT2Circled:OnEnable()
	if not Bartender.db.profile.Extra.HideBorder then
		Bartender:ToggleBorder()
	end
	
	self:ChangeSkin()
	
	self.applied = true
end

function BT2Circled:ChangeSkin()
	local bar
	
	for bar in pairs(bars) do
		self:ApplySkin(bar)
	end
	
	self:ApplySkinBags()
	self:ApplySkinPet()
end

function BT2Circled:ApplySkin(bar)
	if not self.db.profile.applyskin[bar] then return end
	local i, alias, name
	local buttonNum = bars[bar]

	for i=1, buttonNum do
		alias = format("%sButton%d", bar, i)
		name = _G[alias]:GetName()
		
		_G[name]:SetWidth(35)
		_G[name]:SetHeight(35)
		
		self:SkinNormalTexture(name)
		self:SkinHighlightTexture(name)
		self:SkinPushedTexture(name)
		self:SkinCheckedTexture(name)
		self:SkinFlashTexture(name)
		self:SkinIcon(name)
		self:SkinHotkey(name)
		self:SkinCooldown(name)
		self:SkinOverlay(name)
		self:SkinEquipBorder(name)
	end
end

function BT2Circled:ApplySkinBags()
	if not self.db.profile.applyskin.bagbar then return end
	
	local skin = skins[self.db.profile.skin].bagicon
	local i, alias, name
	
	for i=1, 5 do
		alias = format("Bar8Button%d", i)
		name = _G[alias]:GetName()

		_G[name]:SetWidth(35)
		_G[name]:SetHeight(35)
		
		self:SkinNormalTexture(name)
		self:SkinHighlightTexture(name)
		self:SkinPushedTexture(name)
		self:SkinCheckedTexture(name)
		
		_G[name.."IconTexture"]:SetWidth(skin.w)
		_G[name.."IconTexture"]:SetHeight(skin.h)
		_G[name.."IconTexture"]:ClearAllPoints()
		_G[name.."IconTexture"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
		
		self:SkinOverlay(name)
	end
	
	self:SkinCheckedTexture("MainMenuBarBackpackButton")
end

function BT2Circled:ApplySkinPet()
	if not self.db.profile.applyskin.Bar7 then return end
	
	local skina = skins[self.db.profile.skin].autocastable
	local skinb = skins[self.db.profile.skin].autocast
	
	for i=1, 10 do
		alias = format("Bar7Button%d", i)
		name = _G[alias]:GetName()
		
		_G[name.."AutoCastable"]:ClearAllPoints()
		_G[name.."AutoCastable"]:SetWidth(skina.w)
        _G[name.."AutoCastable"]:SetHeight(skina.h)
        _G[name.."AutoCastable"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skina.x, skina.y)
		
		_G[name.."AutoCast"]:SetFrameLevel(2)
        _G[name.."AutoCast"]:SetWidth(skinb.w)
        _G[name.."AutoCast"]:SetHeight(skinb.h)
        _G[name.."AutoCast"]:ClearAllPoints()
        _G[name.."AutoCast"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skinb.x, skinb.y)
	end
end

function BT2Circled:UpdateHLColor()
	local hlcolor = self.db.profile.hlColor
	local i, alias, name
	
	for bar, buttonNum in pairs(bars) do
		for i=1, buttonNum do
			alias = format("%sButton%d", bar, i)
			name = _G[alias]:GetName()
			
			self:SkinPushedTexture(name)
			self:SkinCheckedTexture(name)
		end
	end
	
	for i=1, 5 do
		alias = format("Bar8Button%d", i)
		name = _G[alias]:GetName()
		
		self:SkinPushedTexture(name)
	end
	self:SkinCheckedTexture("MainMenuBarBackpackButton")
end

function BT2Circled:SkinNormalTexture(name)
	local skin = skins[self.db.profile.skin].NormalTexture
	local tex = _G[name]:GetNormalTexture()
	tex:SetTexture(skin.tex)
	tex:SetWidth(skin.w)
	tex:SetHeight(skin.h)
	tex:ClearAllPoints()
	tex:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
	_G[name]:SetNormalTexture(tex)
end

function BT2Circled:SkinHighlightTexture(name)
	local skin = skins[self.db.profile.skin].HighlightTexture
	local tex = _G[name]:GetHighlightTexture()
	tex:SetTexture(skin.tex)
	tex:SetWidth(skin.w)
	tex:SetHeight(skin.h)
	tex:ClearAllPoints()
	tex:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
	_G[name]:SetHighlightTexture(tex)
end

function BT2Circled:SkinPushedTexture(name)
	local skin = skins[self.db.profile.skin].PushedTexture
	local hlcolor = self.db.profile.hlColor
	local tex = _G[name]:GetPushedTexture()
	tex:SetTexture(skin.tex)
	tex:SetWidth(skin.w)
	tex:SetHeight(skin.h)
	tex:SetVertexColor(hlcolor.r, hlcolor.g, hlcolor.b, 0.8)
	tex:ClearAllPoints()
	tex:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
	tex:SetDrawLayer("HIGHLIGHT")
	tex:SetBlendMode("ADD")
	_G[name]:SetPushedTexture(tex)
end

function BT2Circled:SkinCheckedTexture(name)
	local skin = skins[self.db.profile.skin].CheckedTexture
	local hlcolor = self.db.profile.hlColor
	local tex = _G[name]:GetCheckedTexture()
	tex:SetTexture(skin.tex)
	tex:SetWidth(skin.w)
	tex:SetHeight(skin.h)
	tex:SetVertexColor(hlcolor.r, hlcolor.g, hlcolor.b, 0.8)
	tex:ClearAllPoints()
	tex:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
	tex:SetDrawLayer("OVERLAY")
	_G[name]:SetCheckedTexture(tex)
end

function BT2Circled:SkinIcon(name)
	local skin = skins[self.db.profile.skin].icon
	_G[name.."Icon"]:SetWidth(skin.w)
	_G[name.."Icon"]:SetHeight(skin.h)
	_G[name.."Icon"]:ClearAllPoints()
	_G[name.."Icon"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
end

function BT2Circled:SkinCooldown(name)
	local skin = skins[self.db.profile.skin].cooldown
	_G[name.."Cooldown"]:SetFrameLevel(2)
	_G[name.."Cooldown"]:SetWidth(skin.w)
	_G[name.."Cooldown"]:SetHeight(skin.h)
	_G[name.."Cooldown"]:ClearAllPoints()
	_G[name.."Cooldown"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
end

function BT2Circled:SkinHotkey(name)
	local skin = skins[self.db.profile.skin].hotkey
	_G[name.."HotKey"]:ClearAllPoints()
	_G[name.."HotKey"]:SetPoint("TOPRIGHT", _G[name], "TOPRIGHT", skin.x, skin.y)
	_G[name.."HotKey"]:SetDrawLayer("OVERLAY")
end

function BT2Circled:SkinOverlay(name)
	local skin = skins[self.db.profile.skin].overlay
	local overlay
	
	if not _G[name.."Overlay"] then
		overlay = _G[name]:CreateTexture(name.."Overlay", "OVERLAY")
	else
		overlay = _G[name.."Overlay"]
	end

	overlay:SetTexture(skin.tex)
	overlay:SetWidth(skin.w)
	overlay:SetHeight(skin.w)
	overlay:SetPoint("TOPLEFT", _G[name], "TOPLEFT")
end

function BT2Circled:SkinEquipBorder(name)
	local skin = skins[self.db.profile.skin].overlay
	_G[name.."Border"]:SetTexture(skin.tex)
	_G[name.."Border"]:SetWidth(skin.w)
	_G[name.."Border"]:SetHeight(skin.h)
	_G[name.."Border"]:ClearAllPoints()
	_G[name.."Border"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT")
end

function BT2Circled:SkinFlashTexture(name)
	local skin = skins[self.db.profile.skin].flash
	_G[name.."Flash"]:SetWidth(skin.w)
	_G[name.."Flash"]:SetHeight(skin.h)
	_G[name.."Flash"]:ClearAllPoints()
	_G[name.."Flash"]:SetPoint("TOPLEFT", _G[name], "TOPLEFT", skin.x, skin.y)
end