digitalClock = {};
local digitalClock_mt = Class(digitalClock);
print("digitalClock init")

function digitalClock:onCreate(id)
    if id == nil then
        print("digitalClock:onCreate call with no id")
        return;
    end
    print("digitalClock:onCreate:" .. tostring(id))
	g_currentMission:addNonUpdateable(digitalClock.new(id));
end

function digitalClock.new(id)
-- print("digitalClock:new")
	local self = setmetatable({}, digitalClock_mt);
    
    -- settings
    local size =  0.23;
    local color = {0.3, 0.9, 0.3, 0.5};
    local hiddenColor = nil;
    local emissiveScale = 0.2;
    local scaleX = 1;
    local scaleY = 1;
    local alignment = RenderText.ALIGN_LEFT;
    local fontMaterial = g_materialManager:getFontMaterial("DIGIT");
    
	self.clocks = {};
    
	for i = 0, getNumOfChildren(id) - 1 do
		local node = getChildAt(id, i);

		if node ~= nil then
            local myClock = {};
            myClock.fontMaterial = fontMaterial;
            myClock.characterLine = fontMaterial:createCharacterLine(node, 5, size, color, hiddenColor, emissiveScale, scaleX, scaleY, alignment);

			table.insert(self.clocks, myClock)
		end
	end

	self.hasClocks = #self.clocks > 0

	if self.hasClocks then
		g_messageCenter:subscribe(MessageType.MINUTE_CHANGED, self.minuteChanged, self)
	end

	return self
end

function digitalClock:delete()
-- print("digitalClock:delete")
	g_messageCenter:unsubscribeAll(self)
end

function digitalClock:minuteChanged()
-- print("digitalClock:minuteChanged")
	if self.hasClocks then
        local timeHoursF = g_currentMission.environment.dayTime / 3600000 + 0.0001
        local timeHours = math.floor(timeHoursF)
        local timeMinutes = math.floor((timeHoursF - timeHours) * 60)

        local value = string.format("%02d:%02d", timeHours, timeMinutes)

		for _, myClock in pairs(self.clocks) do
            myClock.fontMaterial:updateCharacterLine(myClock.characterLine, value);
		end
	end
end

--- Registration
g_onCreateUtil.addOnCreateFunction("DigitalClock", digitalClock.onCreate)