--[[
Interface: 1.5.1.0 b6732

Copyright (C) GtX (Andy), 2019

Author: GtX | Andy
Date: 09.11.2019

Contact:
https://forum.giants-software.com
https://github.com/GtX-Andy

History:
V 1.0.0.0 @ 09.11.2019 - Release Version.

Important:
Free for use in other mods - no permission needed.

No changes are to be made to this script without permission from GtX | Andy
An diesem Skript dürfen ohne Genehmigung von GtX | Andy keine Änderungen vorgenommen werden
]]


AnalogueClockSpec = {}

function AnalogueClockSpec.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Enterable, specializations)
end

function AnalogueClockSpec.registerFunctions(vehicleType)
    SpecializationUtil.registerFunction(vehicleType, "updateTime", AnalogueClockSpec.updateTime)
end

function AnalogueClockSpec.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onLoad", AnalogueClockSpec)
    SpecializationUtil.registerEventListener(vehicleType, "onUpdate", AnalogueClockSpec)
end

function AnalogueClockSpec:onLoad(savegame)
    self.spec_analogueClocks = {}
    local spec = self.spec_analogueClocks

    spec.clocks = {}
    spec.numClocks = 0

    spec.lastTimeMinutes = 0
    spec.lastTimeHours = 0

    if self.isClient then
        local i = 0
        while true do
            local key = string.format("vehicle.enterable.analogueClocks.analogueClock(%d)", i)
            if not hasXMLProperty(self.xmlFile, key) then
                break
            end

            local minuteHand = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#minuteHandNode"), self.i3dMappings)
            local hourHand = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#hourHandNode"), self.i3dMappings)

            if minuteHand ~= nil and hourHand ~= nil then
                local axis = Utils.getNoNil(getXMLInt(self.xmlFile, key .. "#rotAxis"), 3)
                if axis == 1 then
                    rotAxis = {1, 0, 0}
                elseif axis == 2 then
                    rotAxis = {0, 1, 0}
                else
                    rotAxis = {0, 0, 1}
                end

                local direction = 1
                if Utils.getNoNil(getXMLBool(self.xmlFile, key .. "#reverseRot"), false) then
                    direction = -1
                end

                spec.numClocks = spec.numClocks + 1
                spec.clocks[spec.numClocks] = {minuteHand = minuteHand, hourHand = hourHand, rotAxis = rotAxis, direction = direction}
            end

            i = i + 1
        end

        if spec.numClocks > 0 then
            self:updateTime(true)
        end
    end
end

function AnalogueClockSpec:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    if self.isClient and self:getIsActive() and self.spec_analogueClocks.numClocks > 0 then
        self:updateTime(false)
    end
end

function AnalogueClockSpec:updateTime(force)
    local currentTime = g_currentMission.environment.dayTime / (1000 * 60 * 60)

    local timeHours = math.floor(currentTime)
    local timeMinutes = math.floor((currentTime - timeHours) * 60)

    if timeMinutes ~= self.spec_analogueClocks.lastTimeMinutes or timeHours ~= self.spec_analogueClocks.lastTimeHours or force then
        self.spec_analogueClocks.lastTimeHours = timeHours
        self.spec_analogueClocks.lastTimeMinutes = timeMinutes

        local hourRot = ((currentTime % 12) / 12) * math.pi * 2
        local minuteRot = (timeMinutes / 60) * math.pi * 2

        for i = 1, self.spec_analogueClocks.numClocks do
            local analogueClock = self.spec_analogueClocks.clocks[i]

            local rx, ry, rz = analogueClock.rotAxis[1], analogueClock.rotAxis[2], analogueClock.rotAxis[3]
            local dir = analogueClock.direction

            setRotation(analogueClock.hourHand, (rx * hourRot) * dir, (ry * hourRot) * dir, (rz * hourRot) * dir)
            setRotation(analogueClock.minuteHand, (rx * minuteRot) * dir, (ry * minuteRot) * dir, (rz * minuteRot) * dir)
        end
    end
end
