--JU

local DGV = DugisGuideViewer
if not DGV then return end

local SF, Obj, SmallFrame = DGV:RegisterModule("StickyFrame", "Objectives", "SmallFrame")
local DebugPrint = DGV.DebugPrint

local _
--DGV.StickyFrame = SF
function SF:Initialize()
	SF.StickyQuests = {} --StickyQuests[stickyrow] = mainFrameRow
	local container = DugisStickyFrameContainer
	local tracker = container.TrackerFrame
	local header = tracker.BlocksFrame.Header
	
	local MAX_QUESTS = 10
	
	
	--
	-- Local Functions
	--
	local function GetStickyIndex(guideIndex)
		for i, index in ipairs(SF.StickyQuests) do
			if index == guideIndex then
				return i
			end
		end
	end
	
	local function OnMouseUp(button, mouseButton)
		local index = GetStickyIndex(button.guideIndex or button:GetParent().guideIndex)
		if index and mouseButton == "LeftButton" and IsShiftKeyDown() then
			table.remove(SF.StickyQuests, index)
			DGV:UpdateStickyFrame( )
		end
	end
	
	local function OnDragStop(button, event)
		local point, relativeTo, relativePoint, xOfs, yOfs = SF.Frame:GetPoint()
		local db = DGV.chardb.StickyFrame
		
		db.pos_x = xOfs
		db.pos_y = yOfs
		db.relativePoint = relativePoint
	end
	
	local function ModelButtonOnClick(button, event)
		DGV:ShowModel(button:GetParent().guideIndex)
	end

	function SF:AddRow(questIndex)
		if not tContains(SF.StickyQuests, questIndex) and #SF.StickyQuests < MAX_QUESTS then
			table.insert(SF.StickyQuests, questIndex)    
			DGV:UpdateStickyFrame( )
		end
	end

	function SF:Load()
		local module = ObjectiveTracker_GetModuleInfoTable(
			"DGV_STICKY_TRACKER_MODULE", 
			DGV_TRACKER_MODULE, 
			"DugisObjectiveBlockTemplate")
		module.BlocksFrame = tracker.BlocksFrame
		module:SetHeader(
			module.BlocksFrame.Header,
			"")

		local function OnEnter(block, event)
			DGV_TRACKER_MODULE.OnBlockHeaderEnter(module, block)
			SmallFrameTooltip:Hide()
			if DGV:UserSetting(DGV_STICKYFRAMESHOWDESCRIPTIONS) then return end
			if not SF.Tooltip then
				SF.Tooltip = CreateFrame( "GameTooltip", "StickyFrameTooltip", nil, "GameTooltipTemplate" )
			end
			local guideIndex = block.guideIndex
			if guideIndex then
				SF.Tooltip:SetFrameStrata("TOOLTIP")
				SF.Tooltip:SetOwner(SF.Frame, "ANCHOR_TOPLEFT")
				SF.Tooltip:SetParent(SF.Frame)
				SF.Tooltip:AddLine("|cffffd200"..(block.HeaderText:GetText() or "").."|r", 1, 1, 1, true)
				SF.Tooltip:AddLine(" ", 1, 1, 1, true)
				SF.Tooltip:AddLine(Obj.GetDescriptionText(guideIndex), 1, 1, 1, true)
				SF.Tooltip:Show()
			end
		end
		
		local function OnLeave(block, event)
			DGV_TRACKER_MODULE.OnBlockHeaderLeave(module, block)
			SmallFrameTooltip:Hide()
			if SF.Tooltip then
				SF.Tooltip:Hide()
			end
		end

		local function OnDragStart()
			container:StartMoving()
		end

		local function OnDragStop()
			container:StopMovingOrSizing()
			local point, relativeTo, relativePoint, xOfs, yOfs = container:GetPoint()
			local db = DGV.chardb.StickyFrame
		
			db.pos_x = xOfs
			db.pos_y = yOfs
			db.relativePoint = relativePoint
		end

		if not SF.Frame then
			SF.Frame = container
			DGV:SetFrameBackdrop(container, "Interface\\DialogFrame\\UI-DialogBox-Gold-Background", DGV:GetBorderPath(), 10, 4, 12, 5)
			container:RegisterForDrag("LeftButton")
			container:SetScript("OnDragStart", OnDragStart)
			container:SetScript("OnDragStop", OnDragStop)
			container:ClearAllPoints()

			local db = DGV.chardb.StickyFrame
			container:SetPoint(db.relativePoint, db.pos_x, db.pos_y)
			container:Hide()
		end

		local function ChkOnClick(button, event)
			local guideIndex = button:GetParent().guideIndex
			
			DGV:UpdateRowVisualizations(guideIndex)
			local mainFrameChkButton = DGV.visualRows[guideIndex].frame.Chk
			
			DugisGuideViewer_CheckButton_OnEvent(mainFrameChkButton, event)
		end

		function module:OnBlockHeaderEnter(block)
			OnEnter(block)
		end

		function module:OnBlockHeaderLeave(block)
			OnLeave(block)
		end

		function module:OnBlockHeaderClick(block, mouseButton)
			OnMouseUp(block, mouseButton)
		end

		function module:IsDescriptionShown()
			return DGV:UserSetting(DGV_STICKYFRAMESHOWDESCRIPTIONS)
		end

		local function DragPassthrough(frame)
			frame:RegisterForDrag("LeftButton")
			frame:SetMovable(true)
			frame:SetScript("OnDragStart", OnDragStart)
			frame:SetScript("OnDragStop", OnDragStop)
		end

		function module:UpdateSingle(stepIndex)
			local block = DGV_TRACKER_MODULE.UpdateSingle(module, stepIndex)
			local stepState = DGV:GetQuestState(stepIndex)
			if stepState == "C" then
				block.Chk:SetCheckedTexture("Interface\\Buttons\\UI-CheckBox-Check")
				block.Chk:SetChecked(true)
			elseif stepState == "U" then
				block.Chk:SetCheckedTexture("") 
				block.Chk:SetChecked(false)
			elseif stepState == "X" then
				block.Chk:SetCheckedTexture("Interface\\RAIDFRAME\\ReadyCheck-NotReady") 
				block.Chk:SetChecked(true)
			end
			block.ModelButton:SetShown(DGV:HasModel(stepIndex))
			block.ModelButton:SetScript("OnClick", ModelButtonOnClick)
			block.Chk:SetScript("OnClick", ChkOnClick)
			block.Desc:SetScript("OnMouseUp", OnMouseUp)

			DragPassthrough(block)
			DragPassthrough(block.HeaderButton)
			DragPassthrough(block.Desc)
		end

		function module:Update()
			self:BeginLayout()
			container:Hide()
			if DGV:UserSetting(DGV_STICKYFRAME) then
				for stickyIndex, guideIndex in ipairs(SF.StickyQuests) do
					container:Show()
					self:UpdateSingle(guideIndex)
				end
			end
			self.fromHeaderOffsetY = 12
			self.Header:Hide()

			self:EndLayout();
		end

		function DGV:UpdateStickyFrame( )
			if not SF.Frame then return end

			if tracker.isUpdating then
				return;
			end
			tracker.isUpdating = true;
			tracker.BlocksFrame.maxHeight = math.huge
		
			tracker.BlocksFrame.currentBlock = nil;
			tracker.BlocksFrame.contentsHeight = 0;
		
			-- mark headers unused
			header.added = nil
		
			-- run module updates
			module:Update()
		
			tracker.BlocksFrame.currentBlock = nil
			tracker.isUpdating = false
			container:SetHeight(tracker.BlocksFrame.contentsHeight + 30)
		end

		function DGV:ClearStickyFrame()
			wipe(SF.StickyQuests)
			self:UpdateStickyFrame()
		end
	end

	function SF:Unload()
		DGV:ClearStickyFrame()
	end
end
