mechanism = {};

function mechanism.prerequisitesPresent(specializations)
    return true;
end;

function mechanism:load(savegame)
    local a = 0;
    self.animatorsTable = {};
    while true do
        local stringAnimatorParts     = string.format("vehicle.mechanism.animators.animatorPart(%d)#", a);
        local animatorName             = getXMLString(self.xmlFile, stringAnimatorParts .. "animatorName");
        local animatorHelpText         = Utils.getNoNil(getXMLBool(self.xmlFile, stringAnimatorParts .. "animatorHelpText"), true);
        local animatorIsEngine         = Utils.getNoNil(getXMLBool(self.xmlFile, stringAnimatorParts .. "animatorIsEngine"), false);
        local isRpmDepend             = Utils.getNoNil(getXMLBool(self.xmlFile, stringAnimatorParts .. "isRpmDepend"), false);
        
        if animatorName ~= nil then
            local animatorInfo = {};
            animatorInfo.animatorName         = animatorName;
            animatorInfo.animatorHelpText     = animatorHelpText;
            animatorInfo.animatorIsEngine     = animatorIsEngine;
            animatorInfo.isRpmDepend         = isRpmDepend;
            animatorInfo.animatorState         = false;
            table.insert(self.animatorsTable, animatorInfo);
        else
            break;
        end;
        a = a + 1;
    end;
    
    local b = 0;
    self.blinkerTable = {};
    while true do
        local stringBlinkers = string.format("vehicle.mechanism.blinkers.blinker(%d)", b);
        if not hasXMLProperty(self.xmlFile, stringBlinkers) then
            break;
        end;
        local blinkerName = getXMLString(self.xmlFile, stringBlinkers.."#name");
        local blinkerHelp = Utils.getNoNil(getXMLBool(self.xmlFile, stringBlinkers.."#helpText"), true);
        if blinkerName ~= nil then
            local blinkerInfo = {};
            blinkerInfo.blinkerName     = blinkerName;
            blinkerInfo.blinkerHelp     = blinkerHelp;
            blinkerInfo.blinkerState     = false;
            blinkerInfo.blinkerPartsInfo = {};
            local p = 0;
            while true do
                local stringBlinkerParts = stringBlinkers..string.format(".blinkerPart(%d)#", p);
                if not hasXMLProperty(self.xmlFile, stringBlinkerParts) then
                    break;
                end;
                local blinkerIndex            = Utils.indexToObject(self.components, getXMLString(self.xmlFile, stringBlinkerParts.."blinkerIndex"));
                local blinkerStartVis        = Utils.getNoNil(getXMLBool(self.xmlFile, stringBlinkerParts.."blinkerStartVis"), false);
                local blinkerVisDuration    = Utils.getNoNil(getXMLInt(self.xmlFile, stringBlinkerParts.."blinkerVisDuration"), 500);
                if blinkerIndex ~= nil then
                    local blinkerPartInfo = {};
                    blinkerPartInfo.blinkerIndex            = blinkerIndex;
                    blinkerPartInfo.blinkerStartVis            = blinkerStartVis;
                    blinkerPartInfo.blinkerVisDuration         = blinkerVisDuration;
                    blinkerPartInfo.blinkerVisDurationBUp     = blinkerVisDuration;
                    table.insert(blinkerInfo.blinkerPartsInfo, blinkerPartInfo);
                end;
                p = p + 1;
            end;
            table.insert(self.blinkerTable, blinkerInfo);
        end;
        b = b + 1;
    end;
    
    local v = 0;
    self.visibleTable = {};
    while true do
        local stringVisibleParts     = string.format("vehicle.mechanism.visibles.visiblePart(%d)#", v);
        local visibleName             = getXMLString(self.xmlFile, stringVisibleParts .. "visibleName");
        local visibleStart             = Utils.getNoNil(getXMLBool(self.xmlFile, stringVisibleParts .. "visibleStart"), false);
        local visibleHelpText         = Utils.getNoNil(getXMLBool(self.xmlFile, stringVisibleParts .. "helpText"), false);
        local visibleIndex            = Utils.indexToObject(self.components, getXMLString(self.xmlFile, stringVisibleParts.."visibleIndex"));
        
        if visibleName ~= nil then
            local visibleInfo = {};
            visibleInfo.visibleName         = visibleName;
            visibleInfo.visibleStart         = visibleStart;
            visibleInfo.visibleHelpText     = visibleHelpText;
            visibleInfo.visibleIndex         = visibleIndex;
            table.insert(self.visibleTable, visibleInfo);
        else
            break;
        end;
        v = v + 1;
    end;
    
    local d = 0;
    self.dashboardTable = {};
    while true do
        local stringDashboardParts     = string.format("vehicle.mechanism.dashboards.dashboard(%d)#", d);
        local dashboardName         = getXMLString(self.xmlFile, stringDashboardParts .. "name");

        if dashboardName ~= nil then
            local dashboardInfo = {};
            dashboardInfo.dashboardName     = dashboardName;
            dashboardInfo.dashboardState     = false;
            table.insert(self.dashboardTable, dashboardInfo);
        else
            break;
        end;
        d = d + 1;
    end;
end;

function mechanism:delete()
end;

function mechanism:mouseEvent(posX, posY, isDown, isUp, button)
end;

function mechanism:keyEvent(unicode, sym, modifier, isDown)
end;

function mechanism:update(dt)
    if g_currentMission.controlledVehicle == self then
        for _, animatorInfo in pairs(self.animatorsTable) do
            if animatorInfo.animatorName ~= nil then
                if InputBinding.hasEvent(InputBinding[animatorInfo.animatorName]) then
                    if animatorInfo.animatorState == false then
                        self:playAnimation(animatorInfo.animatorName, 1, nil, true);
                        animatorInfo.animatorState = true;
                    else
                        self:playAnimation(animatorInfo.animatorName, -1, nil, true);
                        animatorInfo.animatorState = false;
                    end;
                end;
                if animatorInfo.animatorIsEngine then
                    if self.isMotorStarted then
                        if animatorInfo.isRpmDepend then
                            self.rpmSpeedFactor = self.motor:getLastMotorRpm()*0.001;
                        else
                            self.rpmSpeedFactor = 1;
                        end;
                        self:playAnimation(animatorInfo.animatorName, self.rpmSpeedFactor, nil, true);
                    else
                        self:stopAnimation(animatorInfo.animatorName);
                    end;
                end;
            end;
        end;
        
        for _, blinkers in pairs(self.blinkerTable) do
            if blinkers.blinkerName ~= nil then
                if InputBinding.hasEvent(InputBinding[blinkers.blinkerName]) then
                    blinkers.blinkerState = not blinkers.blinkerState;
                end;
            end;
        end;
        
        for _, visibles in pairs(self.visibleTable) do
            if visibles.visibleName ~= nil then
                if InputBinding.hasEvent(InputBinding[visibles.visibleName]) then
                    visibles.visibleStart = not visibles.visibleStart;
                end;
            end;
        end;
    end;
    
    for _, blinkers in pairs(self.blinkerTable) do
        if blinkers.blinkerName ~= nil then
            if blinkers.blinkerState then
                for _, blinkersInfos in pairs(blinkers.blinkerPartsInfo) do
                    if blinkersInfos.blinkerVisDuration > 0 then
                        blinkersInfos.blinkerVisDuration = blinkersInfos.blinkerVisDuration - dt;
                    else
                        blinkersInfos.blinkerVisDuration     = blinkersInfos.blinkerVisDurationBUp;
                        blinkersInfos.blinkerStartVis         = not blinkersInfos.blinkerStartVis;
                    end;
                    setVisibility(blinkersInfos.blinkerIndex, blinkersInfos.blinkerStartVis);
                end;
            else
                for _, blinkersInfos in pairs(blinkers.blinkerPartsInfo) do
                    setVisibility(blinkersInfos.blinkerIndex, false);
                end;
            end;
        end;
    end;
    
    for _, visibles in pairs(self.visibleTable) do
        if visibles.visibleName ~= nil then
            setVisibility(visibles.visibleIndex, visibles.visibleStart);
        end;
    end;
    
    
    for _, dashboards in pairs(self.dashboardTable) do
        if dashboards.dashboardName ~= nil then
            if self.isMotorStarted then
                if dashboards.dashboardState == false then
                    self:playAnimation(dashboards.dashboardName, 1, nil, true);
                    dashboards.dashboardState = true;
                end;
            else
                if dashboards.dashboardState then
                    self:playAnimation(dashboards.dashboardName, -1, nil, true);
                    dashboards.dashboardState = false;
                end;
            end;
        end;
    end;
end;

function mechanism:draw()
    if g_currentMission.controlledVehicle == self then
        for _, animatorInfo in pairs(self.animatorsTable) do
            if animatorInfo.animatorName ~= nil and animatorInfo.animatorIsEngine == false then
                if animatorInfo.animatorHelpText then
                    g_currentMission:addHelpButtonText(g_i18n:getText(animatorInfo.animatorName), InputBinding[animatorInfo.animatorName]);
                end;
            end;
        end;

        for _, blinkers in pairs(self.blinkerTable) do
            if blinkers.blinkerName ~= nil and blinkers.blinkerHelp then
                g_currentMission:addHelpButtonText(g_i18n:getText(blinkers.blinkerName), InputBinding[blinkers.blinkerName]);
            end;
        end;
        
        for _, visibles in pairs(self.visibleTable) do
            if visibles.visibleName ~= nil and visibles.visibleHelpText then
                g_currentMission:addHelpButtonText(g_i18n:getText(visibles.visibleName), InputBinding[visibles.visibleName]);
            end;
        end;
    end;
end;