--[[
-- **************************************************************************
-- * TitanClock.lua
-- *
-- * By: The Titan Panel Development Team
-- **************************************************************************
--]]

-- ******************************** Constants *******************************
TITAN_CLOCK_ID = "Clock";
local TITAN_BUTTON = "TitanPanel"..TITAN_CLOCK_ID.."Button"

local TITAN_CLOCK_FORMAT_12H = "12H";
local TITAN_CLOCK_FORMAT_24H = "24H";
local TITAN_CLOCK_FRAME_SHOW_TIME = 0.5;
local _G = getfenv(0);
-- ******************************** Variables *******************************
local L = {} -- For AceLocale
local AceTimer = {}
local ClockTimer = nil;
local updateTable = {TITAN_CLOCK_ID, TITAN_PANEL_UPDATE_ALL };
local realmName = GetRealmName();
-- ******************************** Functions *******************************

--[[
-- **************************************************************************
-- NAME : TitanPanelClockButton_OnLoad()
-- DESC : Registers the plugin upon it loading
-- **************************************************************************
--]]
function TitanPanelClockButton_OnLoad(self)
	local notes = ""
		.."Adds a clock to Titan Panel.\n"
		.."- Select server time / local time / both server and local time.\n"
	self.registry = {
		id = TITAN_CLOCK_ID,
		category = "Built-ins",
		version = TITAN_VERSION,
		menuText = L["TITAN_CLOCK_MENU_TEXT"],
		buttonTextFunction = "TitanPanelClockButton_GetButtonText",
		tooltipTitle = L["TITAN_CLOCK_TOOLTIP"],
		tooltipTextFunction = "TitanPanelClockButton_GetTooltipText",
		notes = notes,
		controlVariables = {
			ShowIcon = false,
			ShowLabelText = true,
			ShowColoredText = true,
			DisplayOnRightSide = true,
		},
		savedVariables = {
			OffsetHour = 0,
			Format = TITAN_CLOCK_FORMAT_12H,
			TimeMode = "Server",
			ShowIcon = true,
			ShowLabelText = false,
			ShowColoredText = false,
			DisplayOnRightSide = 1,
			HideGameTimeMinimap = false,
			HideMapTime = false,
		}
	};
	self:RegisterEvent("PLAYER_ENTERING_WORLD");
end

local function TitanPanelClockButton_GetColored(text)
	local label = "";
	if (TitanGetVar(TITAN_CLOCK_ID, "ShowColoredText")) then
		label = TitanUtils_GetGreenText(text)
	else
		label = TitanUtils_GetHighlightText(text)
	end
	return label;
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockButton_OnShow()
-- DESC : Create repeating timer when plugin is visible
-- **************************************************************************
--]]
function TitanPanelClockButton_OnShow()
	if not ClockTimer then
		ClockTimer = AceTimer.ScheduleRepeatingTimer("TitanPanelClock", TitanPanelPluginHandle_OnUpdate, 30, updateTable)
	end
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockButton_OnHide()
-- DESC : Destroy repeating timer when plugin is hidden
-- **************************************************************************
--]]
function TitanPanelClockButton_OnHide()
	AceTimer.CancelTimer("TitanPanelClock", ClockTimer, true)
	ClockTimer = nil;
end


function TitanPanelClockButton_OnEvent(self, event, ...)
	if (event == "PLAYER_ENTERING_WORLD") then
		-- If the user wants the minimap clock or calendar hidden then hide them
		if TitanGetVar(TITAN_CLOCK_ID, "HideGameTimeMinimap") then
			if GameTimeFrame then GameTimeFrame:Hide() end
		end
		if TimeManagerClockButton and TimeManagerClockButton:GetName() then
			if TitanGetVar(TITAN_CLOCK_ID, "HideMapTime") then
				TimeManagerClockButton:Hide()
			else
				TimeManagerClockButton:Show()
			end
		end
	end
end


function TitanPanelClockButton_OnClick(self, button)
	if button == "LeftButton" and IsShiftKeyDown() then
		TitanUtils_CloseAllControlFrames();
		if (TitanPanelRightClickMenu_IsVisible()) then
			TitanPanelRightClickMenu_Close();
		end
		ToggleCalendar()
	elseif button == "LeftButton" then
	else
		TitanPanelButton_OnClick(self, button);
	end
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockButton_GetButtonText()
-- DESC : Display time on button based on set variables
-- **************************************************************************
--]]
function TitanPanelClockButton_GetButtonText()
	local clocktime = "";
	local labeltext = "";
	local clocktime2 = nil;
	local labeltext2 = nil;
	local _ = nil
	if TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "Server" then
		_,clocktime = TitanPanelClockButton_GetTime("Server", 0)
		labeltext = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(S) ") or ""
	elseif TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "ServerAdjusted" then
		_,clocktime = TitanPanelClockButton_GetTime ("Server", TitanGetVar(TITAN_CLOCK_ID, "OffsetHour"))
		labeltext = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(A) ") or ""
	elseif TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "Local" then
		_,clocktime = TitanPanelClockButton_GetTime ("Local", 0)
		labeltext = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(L) ") or ""
	elseif TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "ServerLocal" then
		local _, s = TitanPanelClockButton_GetTime ("Server", 0)
		local _, l = TitanPanelClockButton_GetTime ("Local", 0)
		sl = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(S) ") or ""
		ll = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(L) ") or ""
		clocktime = s
		labeltext = sl
		clocktime2 = l
		labeltext2 = ll
	elseif TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "ServerAdjustedLocal" then
		local _, s = TitanPanelClockButton_GetTime ("Server", TitanGetVar(TITAN_CLOCK_ID, "OffsetHour"))
		local _, l = TitanPanelClockButton_GetTime ("Local", 0)
		sl = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(A) ") or ""
		ll = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(L) ") or ""
		clocktime = s
		labeltext = sl
		clocktime2 = l
		labeltext2 = ll
	elseif TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "Local" then
		_,clocktime = TitanPanelClockButton_GetTime ("Local", 0)
		labeltext = TitanGetVar(TITAN_CLOCK_ID, "ShowLabelText") and TitanPanelClockButton_GetColored("(L) ") or ""
	end
	return labeltext, clocktime, labeltext2, clocktime2
end


function TitanPanelClockButton_GetTime(displaytype, offset)
	-- Calculate the hour/minutes considering the offset
	local hour, minute = GetGameTime();
	local twentyfour = "";
	local offsettime = string.format("%s", offset);
	local offsethour = 0;
	local offsetmin = 0;
	local s, e, id = string.find(offsettime, '%.5');

	if displaytype == "Server" then
		if (s ~= nil) then
			offsethour = string.sub(offsettime, 1, s);
			offsetmin = string.sub(offsettime, s+1);
			if offsetmin == "" or offsetmin == nil then offsetmin = "0"; end
			if offsethour == "" or offsethour == nil then offsethour = "0"; end
			offsethour = tonumber(offsethour);
			if (tonumber(offsettime) < 0) then offsetmin = tonumber("-" .. offsetmin); end
			minute = minute + (offsetmin*6);
			if (minute > 59) then
				minute = minute - 60;
				offsethour = offsethour + 1;
			elseif (minute < 0) then
				minute = 60 + minute;
				offsethour = offsethour - 1;
			end
		else
			offsethour = offset;
		end
	else
		-- no offset for local time
		hour, minute = tonumber(date("%H")), tonumber(date("%M"));
		offsethour = 0;
	end

	hour = hour + offsethour;

	if (hour > 23) then
		hour = hour - 24;
	elseif (hour < 0) then
		hour = 24 + hour;
	end

	-- Calculate the display text based on format 12H/24H
	if (TitanGetVar(TITAN_CLOCK_ID, "Format") == TITAN_CLOCK_FORMAT_12H) then
		local isAM;
		if (hour >= 12) then
			isAM = false;
			hour = hour - 12;
		else
			isAM = true;
		end
		if (hour == 0) then
			hour = 12;
		end
		if (isAM) then
			return nil, format(TIME_TWELVEHOURAM, hour, minute);
		else
			return nil, format(TIME_TWELVEHOURPM, hour, minute);
		end
	else
		twentyfour = format(TIME_TWENTYFOURHOURS, hour, minute);
		if (hour < 10) then
			twentyfour = "0" .. twentyfour
		end
		return nil, twentyfour;
	end

end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockButton_GetTooltipText()
-- DESC : Display tooltip text
-- **************************************************************************
--]]
function TitanPanelClockButton_GetTooltipText()
		local _, clockTimeLocal = TitanPanelClockButton_GetTime ("Local", 0)
		local _, clockTimeServer = TitanPanelClockButton_GetTime ("Server", 0)
		local _, clockTimeServerAdjusted = TitanPanelClockButton_GetTime ("Server", TitanGetVar(TITAN_CLOCK_ID, "OffsetHour"))
		local clockTimeLocalLabel = L["TITAN_CLOCK_TOOLTIP_LOCAL_TIME"].."\t"..TitanUtils_GetHighlightText(clockTimeLocal)
		local clockTimeServerLabel = L["TITAN_CLOCK_TOOLTIP_SERVER_TIME"].."\t"..TitanUtils_GetHighlightText(clockTimeServer)
		local clockTimeServerAdjustedLabel = "";
		if TitanGetVar(TITAN_CLOCK_ID, "OffsetHour") ~= 0 then
			clockTimeServerAdjustedLabel = L["TITAN_CLOCK_TOOLTIP_SERVER_ADJUSTED_TIME"].."\t"..TitanUtils_GetHighlightText(clockTimeServerAdjusted).."\n"
		end
		local clockText = TitanPanelClock_GetOffsetText(TitanGetVar(TITAN_CLOCK_ID, "OffsetHour"));
		return ""..
		clockTimeLocalLabel.."\n"..
		clockTimeServerLabel.."\n"..
		clockTimeServerAdjustedLabel..
		L["TITAN_CLOCK_TOOLTIP_VALUE"].."\t"..TitanUtils_GetHighlightText(clockText).."\n"..
		TitanUtils_GetGreenText(L["TITAN_CLOCK_TOOLTIP_HINT1"]).."\n"..
		TitanUtils_GetGreenText(L["TITAN_CLOCK_TOOLTIP_HINT2"]).."\n"..
		TitanUtils_GetGreenText(L["TITAN_CLOCK_TOOLTIP_HINT3"]);
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockControlSlider_OnEnter()
-- DESC : Display slider tooltip
-- **************************************************************************
--]]
function TitanPanelClockControlSlider_OnEnter(self)
	self.tooltipText = TitanOptionSlider_TooltipText(L["TITAN_CLOCK_CONTROL_TOOLTIP"], TitanPanelClock_GetOffsetText(TitanGetVar(TITAN_CLOCK_ID, "OffsetHour")));
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT");
	GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	TitanUtils_StopFrameCounting(self:GetParent());
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockControlSlider_OnLeave()
-- DESC : Hide slider tooltip
-- **************************************************************************
--]]
function TitanPanelClockControlSlider_OnLeave(self)
	self.tooltipText = nil;
	GameTooltip:Hide();
	TitanUtils_StartFrameCounting(self:GetParent(), TITAN_CLOCK_FRAME_SHOW_TIME);
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockControlSlider_OnShow()
-- DESC : Display slider tooltip options
-- **************************************************************************
--]]
function TitanPanelClockControlSlider_OnShow(self)
	_G[self:GetName().."Text"]:SetText(TitanPanelClock_GetOffsetText(TitanGetVar(TITAN_CLOCK_ID, "OffsetHour")));
	_G[self:GetName().."High"]:SetText(L["TITAN_CLOCK_CONTROL_LOW"]);
	_G[self:GetName().."Low"]:SetText(L["TITAN_CLOCK_CONTROL_HIGH"]);
--	self:SetThumbTexture("Interface\Buttons\UI-SliderBar-Button-Vertical")
	self:SetMinMaxValues(-12, 12);
	self:SetValueStep(0.5);
	self:SetObeyStepOnDrag(true) -- since 5.4.2 (Mists of Pandaria)
	self:SetValue(0 - TitanGetVar(TITAN_CLOCK_ID, "OffsetHour"));
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockControlSlider_OnValueChanged(arg1)
-- DESC : Display slider tooltip text
-- VARS : arg1 = positive or negative change to apply
-- **************************************************************************
--]]
function TitanPanelClockControlSlider_OnValueChangedWheel(self, a1)
	_G[self:GetName().."Text"]:SetText(TitanPanelClock_GetOffsetText(0 - self:GetValue()));
	local tempval = self:GetValue();

	if a1 == -1 then
		self:SetValue(tempval + 0.5);
	end

	if a1 == 1 then
		self:SetValue(tempval - 0.5);
	end

	TitanSetVar(TITAN_CLOCK_ID, "OffsetHour", 0 - self:GetValue());
	if ( ServerTimeOffsets[realmName] ) then
		ServerTimeOffsets[realmName] = TitanGetVar(TITAN_CLOCK_ID, "OffsetHour");
	end
	TitanPanelButton_UpdateButton(TITAN_CLOCK_ID);

	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(L["TITAN_CLOCK_CONTROL_TOOLTIP"], TitanPanelClock_GetOffsetText(TitanGetVar(TITAN_CLOCK_ID, "OffsetHour")));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end


function TitanPanelClockControlSlider_OnValueChanged(self, a1)
	local step = self:GetValue()
	_G[self:GetName().."Text"]:SetText(TitanPanelClock_GetOffsetText(0 - step));
	TitanSetVar(TITAN_CLOCK_ID, "OffsetHour", 0 - step);
	if ( ServerTimeOffsets[realmName] ) then
		ServerTimeOffsets[realmName] = TitanGetVar(TITAN_CLOCK_ID, "OffsetHour");
	end
	TitanPanelButton_UpdateButton(TITAN_CLOCK_ID);

	-- Update GameTooltip
	if (self.tooltipText) then
		self.tooltipText = TitanOptionSlider_TooltipText(L["TITAN_CLOCK_CONTROL_TOOLTIP"], TitanPanelClock_GetOffsetText(TitanGetVar(TITAN_CLOCK_ID, "OffsetHour")));
		GameTooltip:SetText(self.tooltipText, nil, nil, nil, nil, 1);
	end
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClock_GetOffsetText(offset)
-- DESC : Get hour offset value and return
-- VARS : offset = hour offset from server time
-- **************************************************************************
--]]
function TitanPanelClock_GetOffsetText(offset)
	if (offset > 0) then
		return TitanUtils_GetGreenText("+" .. tostring(offset));
	elseif (offset < 0) then
		return TitanUtils_GetRedText(tostring(offset));
	else
		return TitanUtils_GetHighlightText(tostring(offset));
	end
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockControlFrame_OnLoad()
-- DESC : Create clock option frame
-- **************************************************************************
--]]
function TitanPanelClockControlFrame_OnLoad(self)
	_G[self:GetName().."Title"]:SetText(L["TITAN_CLOCK_CONTROL_TITLE"]);
--[[
Blizzard decided to remove direct Backdrop API in 9.0 (Shadowlands)
so inherit the template (XML)
and set the values in the code (Lua)

9.5 The tooltip template was removed from the GameTooltip.
--]]
	TitanPanelRightClickMenu_SetCustomBackdrop(self)
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockControlFrame_OnUpdate(elapsed)
-- DESC : If dropdown is visible, see if its timer has expired.  If so, hide frame
-- VARS : elapsed = <research>
-- **************************************************************************
--]]
function TitanPanelClockControlFrame_OnUpdate(self, elapsed)
	TitanUtils_CheckFrameCounting(self, elapsed);
end

--[[
-- **************************************************************************
-- NAME : TitanPanelRightClickMenu_PrepareClockMenu()
-- DESC : Generate clock right click menu options
-- **************************************************************************
--]]
function TitanPanelRightClickMenu_PrepareClockMenu()
	TitanPanelRightClickMenu_AddTitle(TitanPlugins[TITAN_CLOCK_ID].menuText);

	local info = {};
	info.text = L["TITAN_CLOCK_MENU_LOCAL_TIME"];
	info.func = function() TitanSetVar(TITAN_CLOCK_ID, "TimeMode", "Local") TitanPanelButton_UpdateButton(TITAN_CLOCK_ID) end
	info.checked = function() return TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "Local" end
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	info = {};
	info.text = L["TITAN_CLOCK_MENU_SERVER_TIME"];
	info.func = function() TitanSetVar(TITAN_CLOCK_ID, "TimeMode", "Server") TitanPanelButton_UpdateButton(TITAN_CLOCK_ID) end
	info.checked = function() return TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "Server" end
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	info = {};
	info.text = L["TITAN_CLOCK_MENU_SERVER_ADJUSTED_TIME"];
	info.func = function() TitanSetVar(TITAN_CLOCK_ID, "TimeMode", "ServerAdjusted") TitanPanelButton_UpdateButton(TITAN_CLOCK_ID) end
	info.checked = function() return TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "ServerAdjusted" end
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	info = {};
	info.text = L["TITAN_CLOCK_MENU_SERVER_TIME"].." & "..L["TITAN_CLOCK_MENU_LOCAL_TIME"]
	info.func = function() TitanSetVar(TITAN_CLOCK_ID, "TimeMode", "ServerLocal") TitanPanelButton_UpdateButton(TITAN_CLOCK_ID) end
	info.checked = function() return TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "ServerLocal" end
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	info = {};
	info.text = L["TITAN_CLOCK_MENU_SERVER_ADJUSTED_TIME"].." & "..L["TITAN_CLOCK_MENU_LOCAL_TIME"]
	info.func = function() TitanSetVar(TITAN_CLOCK_ID, "TimeMode", "ServerAdjustedLocal") TitanPanelButton_UpdateButton(TITAN_CLOCK_ID) end
	info.checked = function() return TitanGetVar(TITAN_CLOCK_ID, "TimeMode") == "ServerAdjustedLocal" end
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	info = {};  -- 12 or 24 hour format
	info.text = L["TITAN_CLOCK_CHECKBUTTON"]
	info.func = function() 
			if (TitanGetVar(TITAN_CLOCK_ID, "Format") == TITAN_CLOCK_FORMAT_12H) then
				TitanSetVar(TITAN_CLOCK_ID, "Format", TITAN_CLOCK_FORMAT_24H);
			else
				TitanSetVar(TITAN_CLOCK_ID, "Format", TITAN_CLOCK_FORMAT_12H);
			end
			if ( ServerHourFormat[realmName] ) then
				ServerHourFormat[realmName] = TitanGetVar(TITAN_CLOCK_ID, "Format");
			end

			TitanPanelButton_UpdateButton(TITAN_CLOCK_ID) 
		end
	info.checked = function() 
			if (TitanGetVar(TITAN_CLOCK_ID, "Format") == TITAN_CLOCK_FORMAT_24H) then
				return true
			else
				return false
			end
		end
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	TitanPanelRightClickMenu_AddSpacer();

	info = {};
	info.text = L["TITAN_CLOCK_MENU_HIDE_MAPTIME"];
	info.func = TitanPanelClockButton_ToggleMapTime;
	info.checked = TitanGetVar(TITAN_CLOCK_ID, "HideMapTime");
	info.keepShownOnClick = 1;
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	info = {};
	info.text = L["TITAN_CLOCK_MENU_HIDE_CALENDAR"];
	info.func = TitanPanelClockButton_ToggleGameTimeFrameShown;
	info.checked = TitanGetVar(TITAN_CLOCK_ID, "HideGameTimeMinimap");
	info.keepShownOnClick = 1;
	TitanPanelRightClickMenu_AddButton(info, TitanPanelRightClickMenu_GetDropdownLevel());

	TitanPanelRightClickMenu_AddControlVars(TITAN_CLOCK_ID)
end

--[[
-- **************************************************************************
-- NAME : TitanPanelClockButton_ToggleRightSideDisplay()
-- DESC : Add clock button to bar
-- **************************************************************************
--]]
function TitanPanelClockButton_ToggleRightSideDisplay()
	TitanToggleVar(TITAN_CLOCK_ID, "DisplayOnRightSide");
	TitanPanel_RemoveButton(TITAN_CLOCK_ID);
	--TitanPanel_AddButton(TITAN_CLOCK_ID);
end

function TitanPanelClockButton_ToggleGameTimeFrameShown()
	TitanToggleVar(TITAN_CLOCK_ID, "HideGameTimeMinimap");
		if GameTimeFrame and GameTimeFrame:GetName() then
			if TitanGetVar(TITAN_CLOCK_ID, "HideGameTimeMinimap") then
				GameTimeFrame:Hide()
			else
				GameTimeFrame:Show()
			end
		end
end

function TitanPanelClockButton_ToggleMapTime()
	TitanToggleVar(TITAN_CLOCK_ID, "HideMapTime");
	if TimeManagerClockButton and TimeManagerClockButton:GetName() then
		if TitanGetVar(TITAN_CLOCK_ID, "HideMapTime") then
			TimeManagerClockButton:Hide()
		else
			TimeManagerClockButton:Show()
		end
	end
end

-- ====== Create needed frames
local function Create_Frames()
	if _G[TITAN_BUTTON] then
		return -- if already created
	end
	
	-- general container frame
	local f = CreateFrame("Frame", nil, UIParent)
--	f:Hide()

	-- Titan plugin button
	local window = CreateFrame("Button", TITAN_BUTTON, f, "TitanPanelTextTemplate")
	window:SetFrameStrata("FULLSCREEN")
	-- Using SetScript("OnLoad",   does not work
	TitanPanelClockButton_OnLoad(window);
--	TitanPanelButton_OnLoad(window); -- Titan XML template calls this...
	
	window:SetScript("OnShow", function(self)
		TitanPanelClockButton_OnShow()
		TitanPanelButton_OnShow(self)
	end)
	window:SetScript("OnHide", function(self)
		TitanPanelClockButton_OnHide()
	end)
	window:SetScript("OnEvent", function(self, event, ...)
		TitanPanelClockButton_OnEvent(self, event, ...) 
	end)
	window:SetScript("OnClick", function(self, button)
		TitanPanelClockButton_OnClick(self, button)
		TitanPanelButton_OnClick(self, button)
	end)


---[===[
	-- Config screen
	local cname = "TitanPanelClockControlFrame"
	local config = CreateFrame("Frame", cname, f, BackdropTemplateMixin and "BackdropTemplate")
	config:SetFrameStrata("FULLSCREEN") -- FULLSCREEN
	config:Hide()  -- 
	config:SetWidth(90)
	config:SetHeight(200)

	config:SetScript("OnShow", function(self)
	end)
	config:SetScript("OnEnter", function(self)
		TitanUtils_StopFrameCounting(self)
	end)
	config:SetScript("OnLeave", function(self)
		TitanUtils_StartFrameCounting(self, 0.5)
	end)
	config:SetScript("OnUpdate", function(self, elapsed)
		TitanPanelClockControlFrame_OnUpdate(self, elapsed)
	end)
	
	-- Config Title
	local str = nil
	local style = "GameFontNormalSmall"
	str = config:CreateFontString(cname.."Title", "ARTWORK", style)
	str:SetPoint("TOP", config, 0, -10)

	-- Config slider sections
	local slider = nil

	-- Hours offset
	local inherit = "TitanOptionsSliderTemplate"
	local offset = CreateFrame("Slider", "TitanPanelClockControlSlider", config, inherit)
	offset:SetPoint("TOP", config, 0, -40)
	offset:SetScript("OnShow", function(self)
		TitanPanelClockControlSlider_OnShow(self)
	end)
	offset:SetScript("OnValueChanged", function(self)
		TitanPanelClockControlSlider_OnValueChanged(self, value)
	end)
	offset:SetScript("OnMouseWheel", function(self)
		TitanPanelClockControlSlider_OnValueChangedWheel(self, delta)
	end)
	offset:SetScript("OnEnter", function(self)
		TitanPanelClockControlSlider_OnEnter(self)
	end)
	offset:SetScript("OnLeave", function(self)
		TitanPanelClockControlSlider_OnLeave(self)
	end)

	-- Now that the parts exist, initialize
	TitanPanelClockControlFrame_OnLoad(config)

--]===]
end


if TITAN_ID then -- it exists
	AceTimer = LibStub("AceTimer-3.0")
	L = LibStub("AceLocale-3.0"):GetLocale(TITAN_ID, true)
	Create_Frames() -- do the work
end
