--[[
Copyright (C) Achimobil, 2022

Author: Achimobil
Date: 26.07.2022
Version: 0.1.0.0

Contact:
https://discord.gg/Va7JNnEkcW

History:
V 0.1.0.0 @ 26.07.2022 - First Version.

Important:
Free for use in other mods - no permission needed, only provide my name.
No changes are to be made to this script without permission from Achimobil.

Frei verwendbar - keine erlaubnis nötig, Namensnennung im Mod erforderlich.
An diesem Skript dürfen ohne Genehmigung von Achimobil keine Änderungen vorgenommen werden.
]]

DigitalClockSpezialisation = {
    Version = "0.1.0.0",
    Name = "DigitalClockSpezialisation"
}
print(g_currentModName .. " - init " .. DigitalClockSpezialisation.Name .. "(Version: " .. DigitalClockSpezialisation.Version .. ")");

function DigitalClockSpezialisation.prerequisitesPresent(specializations)
    return true;
end

function DigitalClockSpezialisation.registerEventListeners(placeableType)
	SpecializationUtil.registerEventListener(placeableType, "onLoad", DigitalClockSpezialisation);
	SpecializationUtil.registerEventListener(placeableType, "onMinuteChanged", DigitalClockSpezialisation);
end

function DigitalClockSpezialisation.registerFunctions(placeableType)
	SpecializationUtil.registerOverwrittenFunction(placeableType, "getNeedMinuteChanged", DigitalClockSpezialisation.getNeedMinuteChanged)
end

function DigitalClockSpezialisation:getNeedMinuteChanged()
	return true;
end

function DigitalClockSpezialisation.registerXMLPaths(schema, basePath)
	schema:setXMLSpecializationType("DigitalClock");
    
	schema:register(XMLValueType.NODE_INDEX, basePath .. ".digitalClocks.digitalClock(?)#node", "Node which contains all digital clock nodes");
	schema:register(XMLValueType.STRING, basePath .. ".digitalClocks.digitalClock(?)#font", "Display font name");
	schema:register(XMLValueType.STRING, basePath .. ".digitalClocks.digitalClock(?)#alignment", "Display text alignment");
	schema:register(XMLValueType.FLOAT, basePath .. ".digitalClocks.digitalClock(?)#size", "Display text size");
	schema:register(XMLValueType.FLOAT, basePath .. ".digitalClocks.digitalClock(?)#scaleX", "Display text x scale");
	schema:register(XMLValueType.FLOAT, basePath .. ".digitalClocks.digitalClock(?)#scaleY", "Display text y scale");
	schema:register(XMLValueType.STRING, basePath .. ".digitalClocks.digitalClock(?)#mask", "Display text mask");
	schema:register(XMLValueType.FLOAT, basePath .. ".digitalClocks.digitalClock(?)#emissiveScale", "Display emissive scale");
	schema:register(XMLValueType.COLOR, basePath .. ".digitalClocks.digitalClock(?)#color", "Display text color");
	schema:register(XMLValueType.COLOR, basePath .. ".digitalClocks.digitalClock(?)#hiddenColor", "Display text hidden color");
	schema:register(XMLValueType.STRING, basePath .. ".digitalClocks.digitalClock(?)#fillType", "Filltype name for the Display to show amount");
    
	schema:setXMLSpecializationType();
end

function DigitalClockSpezialisation:onLoad(savegame)
    self.spec_digitalClock = {};
	local spec = self.spec_digitalClock;
	local xmlFile = self.xmlFile;

	spec.digitalClocks = {};
	local i = 0;

	while true do
		local clockKey = string.format("placeable.digitalClocks.digitalClock(%d)", i);
		if not xmlFile:hasProperty(clockKey) then
			break;
		end
        
		local clockNode = self.xmlFile:getValue(clockKey .. "#node", nil, self.components, self.i3dMappings);
        
        local fontName = self.xmlFile:getValue(clockKey .. "#font", "DIGIT"):upper();
        local fontMaterial = g_materialManager:getFontMaterial(fontName, self.customEnvironment);
            
        local digitalClock = {};
        local alignmentStr = self.xmlFile:getValue(clockKey .. "#alignment", "CENTER");
        local alignment = RenderText["ALIGN_" .. alignmentStr:upper()] or RenderText.ALIGN_CENTER;
        local size = self.xmlFile:getValue(clockKey .. "#size", 0.11);
        local scaleX = self.xmlFile:getValue(clockKey .. "#scaleX", 1);
        local scaleY = self.xmlFile:getValue(clockKey .. "#scaleY", 1);
        local mask = self.xmlFile:getValue(clockKey .. "#mask", "000000");
        local emissiveScale = self.xmlFile:getValue(clockKey .. "#emissiveScale", 0.2);
        local color = self.xmlFile:getValue(clockKey .. "#color", {
            0.9,
            0.9,
            0.9,
            1
        }, true);
        local hiddenColor = self.xmlFile:getValue(clockKey .. "#hiddenColor", nil, true);
        digitalClock.clockNode = clockNode;
        digitalClock.formatStr, digitalClock.formatPrecision = string.maskToFormat(mask);
        digitalClock.fontMaterial = fontMaterial;
        digitalClock.characterLine = fontMaterial:createCharacterLine(digitalClock.clockNode, mask:len(), size, color, hiddenColor, emissiveScale, scaleX, scaleY, alignment);
                
        table.insert(spec.digitalClocks, digitalClock);

		i = i + 1;
	end
    

	spec.hasClocks = #spec.digitalClocks > 0
end

function DigitalClockSpezialisation:onMinuteChanged()
	local spec = self.spec_digitalClock;
	if spec.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 _, digitalClock in pairs(spec.digitalClocks) do
            digitalClock.fontMaterial:updateCharacterLine(digitalClock.characterLine, value);
		end
	end
end