local addonName = "Altoholic"
local addon = _G[addonName]

addon:Controller("AltoholicUI.TabCharacters.QuestLog", { "AltoholicUI.Options", function(Options)
	
	local colors = addon.Colors

	local function GetStatus(character, category, numQuests)
		local allCategories = (category == 0)
		
		local text = ""
		
		if allCategories then
			text = format("%s%s|r / %s%s", colors.white, QUEST_LOG, colors.white, ALL)
		else
			local headers = DataStore:GetQuestHeaders(character)
			text = format("%s%s|r / %s%s", colors.white, QUEST_LOG, colors.white, headers[category])
		end

		return format("%s|r / %s (%s%d|r)|r", 
			DataStore:GetColoredCharacterName(character), 
			text, 
			colors.green, numQuests)
	end

	return {
		OnBind = function(frame)
			local parent = AltoholicFrame.TabCharacters
			
			frame:SetParent(parent)
			frame:SetPoint("TOPLEFT", parent.Background, "TOPLEFT", 0, 0)
			frame:SetPoint("BOTTOMRIGHT", parent.Background, "BOTTOMRIGHT", 26, 0)
			parent:RegisterPanel("QuestLog", frame)
			
			-- Handle resize
			frame:SetScript("OnSizeChanged", function(self, width, height)
				if not frame:IsVisible() then return end
			
				frame:Update(true)
			end)
		end,
		
		Update = function(frame, isResizing)
			local parent = frame:GetParent()
			local character = parent:GetCharacter()
			if not character then return end
		
			local currentCategoryID = Options.Get("UI.Tabs.Characters.ViewQuestLogCategory")
			
			-- Get the quest list
			local questList = {}
			
			DataStore:IterateQuests(character, currentCategoryID, function(questIndex) 
				table.insert(questList, questIndex)
			end)
		
			-- Set the tab status
			parent:SetStatus(GetStatus(character, currentCategoryID, #questList))
			
			-- Update the scrollframe
			local scrollFrame = frame.ScrollFrame
			local numRows = scrollFrame.numRows
			local offset = scrollFrame:GetOffset()
			
			local maxDisplayedRows = math.floor(scrollFrame:GetHeight() / scrollFrame.rowHeight)
			
			for rowIndex = 1, numRows do
				local rowFrame = scrollFrame:GetRow(rowIndex)
				local line = rowIndex + offset
				
				if line <= #questList and (rowIndex <= maxDisplayedRows)then	-- if the line is visible
					if not (isResizing and rowFrame:IsVisible()) then
						rowFrame:SetID(questList[line])
						
						local questName, questID, link, groupName, level, groupSize, tagID, 
								isComplete, isDaily, isTask, isBounty, isStory, isHidden, isSolo = DataStore:GetQuestLogInfo(character, questList[line])
						local money = DataStore:GetQuestLogMoney(character, questList[line])
						
						rowFrame:SetName(questName, level)
						rowFrame:SetType(tagID)
						rowFrame:SetRewards(character)
						rowFrame:SetInfo(isComplete, isDaily, groupSize, money)
					end
					rowFrame:Show()
				else
					rowFrame:Hide()
				end
			end

			scrollFrame:Update(#questList, maxDisplayedRows)
			frame:Show()
		end,
	}
end})
