-- ==================================================================================================================== --
--															--
--	File			HistoricLine.lua									--
--															--
--															--
--	Author			Zarnivoop, Luckycire(maintainer)		| Version	   :	  7.0.0 |	--
--										| Curse-Project-ID :	 	|	--
--										| License	   :	  MIT/X |	--
--															--
--											| Basis, thx	|		--
--															--
--															--
--				Baltha						| Version	   :	 11.2.0 |	--
--										| Curse-Project-ID :	1188318 |	--
--										| License	   :	  MIT/X |	--
--															--
--											| Revision	|		--
--															--
--															--
--	Path ( Revision )	"..\World of Warcraft\_retail_\Interface\AddOns\Skada\Misc\Changelog\ChangeLog.txt"	--
--															--
-- ==================================================================================================================== --


local _, Skada = ...
local L        = LibStub("AceLocale-3.0"):GetLocale("Skada", false)




local function GenerateColor(hue, saturation, value)

	local hue_int = math.floor(hue * 6)

	local f	      = hue * 6 - hue_int
	local p       = value * (1 - saturation)
	local q       = value * (1 - f * saturation)
	local t       = value * (1 - (1 - f) * saturation)

	local r, g, b = 255, 255, 255


	if hue_int and hue_int == 0 then

		r = value
		g = t
		b = p
	end


	if hue_int and hue_int == 1 then

		r = q
		g = value
		b = p
	end


	if hue_int and hue_int == 2 then

		r = p
		g = value
		b = t
	end


	if hue_int and hue_int == 3 then

		r = p
		g = q
		b = value
	end


	if hue_int and hue_int == 4 then

		r = t
		g = p
		b = value
	end


	if hue_int and hue_int == 5 then

		r = value
		g = p
		b = q
	end


	return { r, g, b }
end




local function AddButton(self, title, normaltex, highlighttex, clickfunc)

	if not self.buttons then self.buttons = {} end


	local btn = CreateFrame("Button", nil, self)


	btn.title = title

	btn:SetFrameLevel(5)
	btn:ClearAllPoints()
	btn:SetSize(13, 13)

	btn:SetNormalTexture(normaltex)
	btn:SetHighlightTexture(highlighttex, 1.0)
	btn:SetAlpha(0.5)

	btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")


	btn:SetScript("OnClick", clickfunc)


	btn:SetScript("OnEnter",

				function(this)

					GameTooltip_SetDefaultAnchor(GameTooltip, this)
					GameTooltip:SetText(title)
					GameTooltip:Show()
				end

		     )


	btn:SetScript("OnLeave",

				function()

					GameTooltip:Hide()
				end

		     )


	btn:Show()


	table.insert(self.buttons, btn)
	self:AdjustButtons()
end




local function AdjustButtons(self)

	local nr      = 0
	local lastbtn = nil


	for i, btn in ipairs(self.buttons) do

		btn:ClearAllPoints()


		if btn:IsShown() then

			if nr == 0 then

				btn:SetPoint("TOPRIGHT", self	, "TOPRIGHT", -5, 0 - (math.max(self:GetHeight() - btn:GetHeight(), 0) / 2))
			else

				btn:SetPoint("TOPRIGHT", lastbtn, "TOPLEFT", 0, 0)
			end


			lastbtn = btn
			nr	= nr + 1
		end
	end
end




local function ClearLegends(self)


	if not self.legends then self.legends = {} end


	for i, legend in ipairs(self.legends) do

		legend.id = nil
		legend:Hide()
	end


	self.legendoffset = 0
end




local function AdjustLegends(self)

	if self.legends then

		local used  = {}


		for i, legend in ipairs(self.legends) do

			if legend.id then

				table.insert(used, legend)
			else

				legend:Hide()
			end
		end


		local last_legend = nil


		for i, legend in ipairs(used) do

			if self.legendoffset and self.legendoffset > i then

				legend:Hide()
			else
		
				legend:ClearAllPoints()


				if last_legend then

					legend:SetPoint("TOPLEFT", last_legend, "BOTTOMLEFT", 0, -2)
				else

					legend:SetPoint("TOPLEFT", self, "TOPLEFT", 0, -2)
				end


				if (legend:GetTop() - legend:GetHeight()) > (self:GetTop() - self:GetHeight()) then

					legend:Show()
				else

					legend:Hide()
				end

			
				last_legend = legend
			end
		end
	end
end




local function SetupLegendScrolling(self)

	if not self.legends then self.legends = {} end


	self.legendoffset = 0
	self:EnableMouse(true)


	self:SetScript("OnMouseWheel",

				function(f, direction)

					diffLegends = #self.legends - self.legendoffset


					if direction == 1 and self.legendoffset > 0 then

						self.legendoffset = self.legendoffset - 1
					elseif direction == -1 and diffLegends > 10 then

						self.legendoffset = self.legendoffset + 1
					end


					AdjustLegends(self)
				end

		      )
end




local function AddLegend(self, legend_id, legend_label, legend_color)

	if legend_id == nil then return end


	if not self.legends then self.legends = {} end


	local has_legend  = nil
	local free_legend = nil


	for i, legend in ipairs(self.legends) do

		if legend.id == legend_id then

			legend.id = nil
			legend:Hide()
			
			i= #self.legends
		end

		if free_legend == nil and legend.id == nil then	free_legend = legend end
	end


	local l


	if free_legend then

		l = free_legend
	else

		local backdropFrameInfo =

					{

						bgFile   = "Interface\\Buttons\\WHITE8X8",
					 	tile	 = false,
					 	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
						edgeSize = 1,
					 	insets	 = { left = 1, right = 1, top = 1, bottom = 1 },

					}


		l = CreateFrame("Frame", nil, self, "BackdropTemplate")


		l:SetFrameLevel(5)
		l:SetSize(13, 13)


		l:SetBackdrop(backdropFrameInfo)


		table.insert(self.legends, l)


		local f = l:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")


		-- =====================
		-- | proportional font |
		-- =====================
--		f:SetFont([[Interface\Addons\Skada\Media\Fonts\Accidental Presidency.ttf]], 13, "")


		-- ====================================
		-- | non-proportional font (monospace)|
		-- ====================================
		f:SetFont([[Interface\Addons\Skada\Media\Fonts\Cousine-Bold.ttf]], 10, "")


		f:SetWidth(self:GetWidth() - 12)
		f:SetHeight(16)
		f:SetJustifyH("LEFT")
		f:SetPoint("TOPLEFT", l, "TOPRIGHT", 3, 0)

		l.text = f
	end


	l.id = legend_id


	l:SetBackdropColor(legend_color[1], legend_color[2], legend_color[3])
	l:ClearAllPoints()



	local output = Skada:ProcessingDiagram(legend_id, legend_label)


	l.text:SetText(output)
	l.label = legend_label


	l:EnableMouse()


	if self.EnterLegend then l:SetScript("OnEnter",

							function(f)

								l_label = string.sub(legend_label, 1, string.len(legend_id))


								if legend_id == l_label then

									legend_label = l_label
								end


								self.EnterLegend(legend_id, legend_label)
							end

					    )

	end


	if self.LeaveLegend then l:SetScript("OnLeave",

							function(f)

								l_label = string.sub(legend_label, 1, string.len(legend_id))


								if legend_id == l_label then

									legend_label = l_label
								end


								self.LeaveLegend(legend_id, legend_label)
							end

					    )
	end




	if self.ClickLegend then l:SetScript("OnMousedown",

							function(f, button)

								l_label = string.sub(legend_label, 1, string.len(legend_id))


								if legend_id == l_label then

									legend_label = l_label
								end


								self.ClickLegend(legend_id, legend_label, button)
							end
					    )
		end
end





local function showmode(win, id, label, mode)

	if win.selectedmode then table.insert(win.history, win.selectedmode) end
	if mode.Enter	    then mode:Enter(win, id, label)		     end


	win:DisplayMode(mode)
end




local function ClickRow(self, win, id, label, button)

	local click1 = win.metadata.click1
	local click2 = win.metadata.click2
	local click3 = win.metadata.click3


	if button == "RightButton" and IsShiftKeyDown() then

		Skada:OpenMenu(win)
	elseif win.metadata.click then

		win.metadata.click(win, id, label, button)
	elseif button == "RightButton" then

		win:RightClick()
	elseif click2 and IsShiftKeyDown() then

		showmode(win, id, label, click2)
	elseif click3 and IsControlKeyDown() then

		showmode(win, id, label, click3)
	elseif click1 then

		showmode(win, id, label, click1)
	end
end




local function EnterRow(self, win, id, label)

	win.ttactive = false


	Skada:ShowTooltip(win, id, label)
end




local function LeaveRow(self, win, id, label)

	if win.ttactive then

		ttactive = false


		GameTooltip:Hide()
	end
end




local function MakeResizable(self, growup)

	local f


	if self.resizebutton then

		f = self.resizebutton
	else

		f = CreateFrame("Button", "BarGroupResizeButton", self)


		self.resizebutton = f

		f:Show()

		f:SetFrameLevel(11)
		f:SetSize(13, 13)


		self:SetResizable(true)
	end


	f:SetScript("OnMouseDown",

				function(frame, button)

					if (button == "LeftButton") then

						self.isResizing = true


						if growup then

							self:StartSizing("TOPRIGHT")
						else

							self:StartSizing("BOTTOMRIGHT")
						end


						if self.OnResizingInProgress then

							self:SetScript("OnUpdate",

										function()

											if self.isResizing then

												self:OnResizingInProgress()

											else

												self:SetScript("OnUpdate", nil)
											end
										end

								      )
						end
					end
				end

		   )


	f:SetScript("OnMouseUp",

				function(frame,button)

					if self.isResizing == true then

						self:StopMovingOrSizing()
						self.isResizing = false


						if self.OnResizingDone then self:OnResizingDone() end
					end
				end

		   )

		
	f:ClearAllPoints()
	f:SetNormalTexture("Interface\\CHATFRAME\\UI-ChatIM-SizeGrabber-Down")
	f:SetHighlightTexture("Interface\\CHATFRAME\\UI-ChatIM-SizeGrabber-Down")


	if growup then

		f:SetPoint("TOPRIGHT", self, "TOPRIGHT", 2, 2)
	else

		f:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", 2, -2)
	end
end




Skada.AddButton		   = AddButton
Skada.AddLegend		   = AddLegend

Skada.AdjustButtons	   = AdjustButtons
Skada.AdjustLegends	   = AdjustLegends

Skada.ClearLegends	   = ClearLegends
Skada.ClickRow		   = ClickRow

Skada.EnterRow		   = EnterRow
Skada.GenerateColor	   = GenerateColor

Skada.LeaveRow		   = LeaveRow
Skada.MakeResizable	   = MakeResizable

Skada.SetupLegendScrolling = SetupLegendScrolling