local addonName = "Altoholic"
local addon = _G[addonName]
local colors = addon.Colors

local L = LibStub("AceLocale-3.0"):GetLocale(addonName)
local module = "DataStore_Quests"

addon:Controller("AltoholicUI.TabOptions.SettingsDataStoreQuests", { "AltoholicUI.Options", function(Options)

	local dailyResetDDM

	local function DailyResetDropDown_OnClick(self)
		-- set the new reset hour
		local newHour = self.value
		
		DataStore:SetOption(module, "DailyResetHour", newHour)
		dailyResetDDM:SetSelectedValue(newHour)
	end

	local function DailyResetDropDown_Initialize(self)
		local info = self:CreateInfo()
		local selectedHour = DataStore:GetOption(module, "DailyResetHour")
		
		for hour = 0, 23 do
			info.value = hour
			info.text = format(TIMEMANAGER_TICKER_24HOUR, hour, 0)
			info.func = DailyResetDropDown_OnClick
			info.checked = (hour == selectedHour)

			self:AddButtonInfo(info)
		end
	end

	return {
		OnBind = function(frame)
			-- Attach to the parent
			local parent = AltoholicFrame.TabOptions
			
			frame:SetParent(parent)
			frame:SetPoint("TOPLEFT", parent.Background, "TOPLEFT", 0, 0)
			frame:SetPoint("BOTTOMRIGHT", parent.Background, "BOTTOMRIGHT", 26, 0)
			parent:RegisterPanel(15, frame)

			-- Setup localization & clear localized value after use (no longer needed)
			local colors = addon.Colors
			
			frame.Title:SetText(format("%s%s", colors.white, L["DataStore_Quests' Options"]))
			L["DataStore_Quests' Options"] = nil
			
			frame.DailyResetDropDownLabel:SetText(format("%s%s:", colors.white, L["DAILY_QUESTS_RESET_LABEL"]))
			
			-- Setup the drop down for the reset hour
			local hour = DataStore:GetOption(module, "DailyResetHour")
			
			dailyResetDDM = frame.DailyResetDropDown
			dailyResetDDM:SetMenuWidth(100) 
			dailyResetDDM:SetButtonWidth(20) 
			dailyResetDDM:SetSelectedValue(hour)
			dailyResetDDM:SetText(format(TIMEMANAGER_TICKER_24HOUR, hour, 0))
			dailyResetDDM:Initialize(DailyResetDropDown_Initialize)
		end,
		Update = function(frame, isResizing)
			frame.TrackTurnIns:SetChecked(DataStore:GetOption(module, "TrackTurnIns"))
			frame.AutoUpdateHistory:SetChecked(DataStore:GetOption(module, "AutoUpdateHistory"))
			
			frame:Show()
		end,
	}
end})
