-- ----------------------------------------------------------------------------
-- GridIndicatorSideIcons by kunda
-- ----------------------------------------------------------------------------
local L = AceLibrary("AceLocale-2.2"):new("GridIndicatorSideIcons")
local AceOO = AceLibrary("AceOO-2.0")

GridIndicatorSideIcons = GridStatus:NewModule("GridIndicatorSideIcons")
GridIndicatorSideIcons.defaultDB = {
	iconSizeTop = 8,
	iconSizeBottom = 8,
	iconSizeLeft = 8,
	iconSizeRight = 8,
	xoffsetTB = 0,
	yoffsetTB = 0,
	xoffsetLR = 0,
	yoffsetLR = 0,
	}

local indicators = GridFrame.frameClass.prototype.indicators
table.insert(indicators, { type = "icontop",		order = 7.11, name = L["Top Icon"] })
table.insert(indicators, { type = "iconbottom",	order = 7.12, name = L["Bottom Icon"] })
table.insert(indicators, { type = "iconleft",		order = 7.13, name = L["Left Icon"] })
table.insert(indicators, { type = "iconright",	order = 7.14, name = L["Right Icon"] })


local statusmap = GridFrame.db.profile.statusmap
if not statusmap["icontop"] then
	statusmap["icontop"] = {}
	statusmap["iconbottom"] = {}
	statusmap["iconleft"] = {}
	statusmap["iconright"] = {}
end

local GridIndicatorSideIconsFrameClass = AceOO.Class(GridFrame.frameClass)

local _frameClass = nil
if not _frameClass then
	_frameClass = GridFrame.frameClass
	GridFrame.frameClass = GridIndicatorSideIconsFrameClass
end

function GridIndicatorSideIcons:OnEnable()
	self:CleanOptionsMenu() -- hack for better indicator menu

	GridFrame.options.args.advanced.args["sideicons"] = {
		type = "group",
		name = L["Icon (Sides)"],
		desc = L["Options for Icon (Sides) indicators."],
		order = 106,
		args = {
			["iconsizetop"] = {
				type = "range",
				name = L["Icon Size Top"],
				desc = L["Adjust the size of the top icon."],
				order = 106.1,
				min = 5,
				max = 50,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.iconSizeTop
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.iconSizeTop = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["iconsizebottom"] = {
				type = "range",
				name = L["Icon Size Bottom"],
				desc = L["Adjust the size of the bottom icon."],
				order = 106.2,
				min = 5,
				max = 50,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.iconSizeBottom
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.iconSizeBottom = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["iconsizeleft"] = {
				type = "range",
				name = L["Icon Size Left"],
				desc = L["Adjust the size of the left icon."],
				order = 106.3,
				min = 5,
				max = 50,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.iconSizeLeft
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.iconSizeLeft = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["iconsizeright"] = {
				type = "range",
				name = L["Icon Size Right"],
				desc = L["Adjust the size of the right icon."],
				order = 106.4,
				min = 5,
				max = 50,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.iconSizeRight
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.iconSizeRight = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["xoffsetTB"] = {
				type = "range",
				name = L["Offset X-axis (top/bottom)"],
				desc = L["Adjust the offset of the X-axis for the top and bottom icon."],
				order = 106.5,
				min = -10,
				max = 10,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.xoffsetTB
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.xoffsetTB = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["yoffsetTB"] = {
				type = "range",
				name = L["Offset Y-axis (top/bottom)"],
				desc = L["Adjust the offset of the Y-axis for the top and bottom icon."],
				order = 106.6,
				min = -10,
				max = 10,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.yoffsetTB
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.yoffsetTB = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["xoffsetLR"] = {
				type = "range",
				name = L["Offset X-axis (left/right)"],
				desc = L["Adjust the offset of the X-axis for the left and right icon."],
				order = 106.7,
				min = -10,
				max = 10,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.xoffsetLR
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.xoffsetLR = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			},
			["yoffsetLR"] = {
				type = "range",
				name = L["Offset Y-axis (left/right)"],
				desc = L["Adjust the offset of the Y-axis for the left and right icon."],
				order = 106.8,
				min = -10,
				max = 10,
				step = 1,
				get = function ()
					return GridIndicatorSideIcons.db.profile.yoffsetLR
				end,
				set = function (v)
					GridIndicatorSideIcons.db.profile.yoffsetLR = v
					GridFrame:WithAllFrames(function (f) f:SetIconSize(v) end)
				end,
			}
		}
	}
	hooksecurefunc(GridFrame, "UpdateOptionsMenu", GridIndicatorSideIcons.CleanOptionsMenu) -- hack for better indicator menu
end

function GridIndicatorSideIcons:CleanOptionsMenu()
	GridFrame.options.args["sideicons"] = {
		type = "group",
		icon = "Interface\\QuestFrame\\UI-Quest-BulletPoint",
		name = L["Icon (Sides)"],
		desc = L["Options for Icon (Sides) indicators."],
		order = 57.1,
		args = {
			["icontop"]    = GridFrame.options.args.icontop,
			["iconbottom"] = GridFrame.options.args.iconbottom,
			["iconleft"]   = GridFrame.options.args.iconleft,
			["iconright"]  = GridFrame.options.args.iconright
		}
	}
	GridFrame.options.args.icontop = nil
	GridFrame.options.args.iconbottom = nil
	GridFrame.options.args.iconleft = nil
	GridFrame.options.args.iconright = nil
end

function GridIndicatorSideIconsFrameClass.prototype:CreateIndicator(indicator)
	GridIndicatorSideIconsFrameClass.super.prototype.CreateIndicator(self, indicator)
	local f = self.frame
	local xoffsetTB = GridIndicatorSideIcons.db.profile.xoffsetTB
	local yoffsetTB = GridIndicatorSideIcons.db.profile.yoffsetTB
	local xoffsetLR = GridIndicatorSideIcons.db.profile.xoffsetLR
	local yoffsetLR = GridIndicatorSideIcons.db.profile.yoffsetLR
	if indicator == "icontop" then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeTop
		f.icontop = f.Bar:CreateTexture("IconTop", "OVERLAY")
		f.icontop:SetWidth(wh)
		f.icontop:SetHeight(wh)
		f.icontop:SetPoint("TOP", f, "TOP", xoffsetTB, yoffsetTB)
		f.icontop:SetTexCoord(0.05, 0.95, 0.05, 0.95)
		f.icontop:SetTexture(1,1,1,0)
	elseif indicator == "iconbottom" then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeBottom
		f.iconbottom = f.Bar:CreateTexture("IconBottom", "OVERLAY")
		f.iconbottom:SetWidth(wh)
		f.iconbottom:SetHeight(wh)
		f.iconbottom:SetPoint("BOTTOM", f, "BOTTOM", xoffsetTB, (yoffsetTB*-1))
		f.iconbottom:SetTexCoord(0.05, 0.95, 0.05, 0.95)
		f.iconbottom:SetTexture(1,1,1,0)
	elseif indicator == "iconleft" then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeLeft
		f.iconleft = f.Bar:CreateTexture("IconLeft", "OVERLAY")
		f.iconleft:SetWidth(wh)
		f.iconleft:SetHeight(wh)
		f.iconleft:SetPoint("LEFT", f, "LEFT", (xoffsetLR*-1), yoffsetLR)
		f.iconleft:SetTexCoord(0.05, 0.95, 0.05, 0.95)
		f.iconleft:SetTexture(1,1,1,0)
	elseif indicator == "iconright" then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeRight
		f.iconright = f.Bar:CreateTexture("IconRight", "OVERLAY")
		f.iconright:SetWidth(wh)
		f.iconright:SetHeight(wh)
		f.iconright:SetPoint("RIGHT", f, "RIGHT", xoffsetLR, yoffsetLR)
		f.iconright:SetTexCoord(0.05, 0.95, 0.05, 0.95)
		f.iconright:SetTexture(1,1,1,0)
	end
end

function GridIndicatorSideIconsFrameClass.prototype:SetIndicator(indicator, color, text, value, maxValue, texture, start, duration, stack)
	GridIndicatorSideIconsFrameClass.super.prototype.SetIndicator(self, indicator, color, text, value, maxValue, texture, start, duration, stack)
	if indicator == "icontop"
	or indicator == "iconbottom"
	or indicator == "iconleft"
	or indicator == "iconright"
	then
		if not self.frame[indicator] then
			self:CreateIndicator(indicator)
		end
		if texture then
			self.frame[indicator]:SetTexture(texture)
			self.frame[indicator]:SetAlpha(1)
			self.frame[indicator]:Show()
			if type(color) == "table" then
				self.frame[indicator]:SetAlpha(color.a)
			end
		else
			self.frame[indicator]:Hide()
		end
	end
end

function GridIndicatorSideIconsFrameClass.prototype:ClearIndicator(indicator)
	GridIndicatorSideIconsFrameClass.super.prototype.ClearIndicator(self, indicator)
	if indicator == "icontop"
	or indicator == "iconbottom"
	or indicator == "iconleft"
	or indicator == "iconright"
	then
		if self.frame[indicator] then
			self.frame[indicator]:SetTexture(1,1,1,0)
			self.frame[indicator]:Hide()
		end
	end
end

function GridIndicatorSideIconsFrameClass.prototype:SetIconSize(size)
	GridIndicatorSideIconsFrameClass.super.prototype.SetIconSize(self, GridFrame.db.profile.iconSize)
	local f = self.frame
	local xoffsetTB = GridIndicatorSideIcons.db.profile.xoffsetTB
	local yoffsetTB = GridIndicatorSideIcons.db.profile.yoffsetTB
	local xoffsetLR = GridIndicatorSideIcons.db.profile.xoffsetLR
	local yoffsetLR = GridIndicatorSideIcons.db.profile.yoffsetLR
	if f.icontop then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeTop
		f.icontop:SetPoint("TOP", f, "TOP", xoffsetTB, yoffsetTB)
		f.icontop:SetWidth(wh)
		f.icontop:SetHeight(wh)
	end
	if f.iconbottom then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeBottom
		f.iconbottom:SetPoint("BOTTOM", f, "BOTTOM", xoffsetTB, (yoffsetTB*-1))
		f.iconbottom:SetWidth(wh)
		f.iconbottom:SetHeight(wh)
	end
	if f.iconleft then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeLeft
		f.iconleft:SetPoint("LEFT", f, "LEFT", (xoffsetLR*-1), yoffsetLR)
		f.iconleft:SetWidth(wh)
		f.iconleft:SetHeight(wh)
	end
	if f.iconright then
		local wh = GridIndicatorSideIcons.db.profile.iconSizeRight
		f.iconright:SetPoint("RIGHT", f, "RIGHT", xoffsetLR, yoffsetLR)
		f.iconright:SetWidth(wh)
		f.iconright:SetHeight(wh)
	end
end