
Trimmer = {};

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

function Trimmer:load(savegame)
    self.node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.trimmer#node"));
    self.discCOM = getXMLFloat(self.xmlFile, "vehicle.trimmer#discCOM");
    self.setActiveDisks = SpecializationUtil.callSpecializationsFunction("setActiveDisks");
    self.Trimmeres = {}
    self.numActiveDiscs = 0;
    self.defaultCOMX, self.defaultCOMY, self.defaultCOMZ = getCenterOfMass(self.components[1].node);
    
    local num = 0;
    while true do
        num = num + 1;
        local path = "vehicle.trimmer.disc"..tostring(num).."#index";
        local index = getXMLString(self.xmlFile, path);
        if index == nil then
            break;
        else
            local discId = Utils.indexToObject(self.components, index);
            setVisibility(discId, false);
            table.insert(self.Trimmeres, discId);
        end;    
    end;
    self.maxNumDiscs = num;
    self.minNumDiscs = num * (-1);
end;

function Trimmer:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
    local numDiscs = getXMLInt(xmlFile, key.."#numDiscs");
    self:setActiveDisks(numDiscs);
end;

function Trimmer:getSaveAttributesAndNodes(nodeIdent)
    local savestr = tostring(self.numActiveDiscs);
    local attributes = 'numDiscs="'..savestr..'"';
    return attributes, nil;
end;

function Trimmer:delete()
  
end;

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

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

function Trimmer:update(dt)
    if self:getIsActiveForInput() then
        if InputBinding.hasEvent(InputBinding["Trimmer_front"]) then
            self:setActiveDisks(self.numActiveDiscs + 1);
        end;
        if InputBinding.hasEvent(InputBinding["Trimmer_back"]) then
            self:setActiveDisks(self.numActiveDiscs - 1);
        end;
    end;
end;

function Trimmer:draw()
    if self.maxNumDiscs > self.numActiveDiscs then
        g_currentMission:addHelpButtonText(g_i18n:getText("Trimmer_front"), InputBinding["Trimmer_front"]); 
    end;
    if self.numActiveDiscs > self.minNumDiscs then
        g_currentMission:addHelpButtonText(g_i18n:getText("Trimmer_back"), InputBinding["Trimmer_back"]); 
    end;
    
    local comX, comY, comZ = getCenterOfMass(self.components[1].node);
    renderText(0.01, 0.01, 0.009, "COM:");
    renderText(0.03, 0.01, 0.009, string.format("%4.4f°", comX));
    renderText(0.05, 0.01, 0.009, string.format("%4.4f°", comY));
    renderText(0.07, 0.01, 0.009, string.format("%4.4f°", comZ));
end;

function Trimmer:setActiveDisks(numDiscs, noEventSend)
    if numDiscs > self.maxNumDiscs or numDiscs < self.minNumDiscs then
        return;
    end;
    
    self.numActiveDiscs = numDiscs;
    local comZ = self.defaultCOMZ;
    local discAmount = self.numActiveDiscs;

    if self.numActiveDiscs == 0 then
        setScale(self.node, 0.0, 0.0, 0.0);
        setTranslation(self.node, 0.0, 0.1, 0.0);
        setCenterOfMass(self.components[1].node, self.defaultCOMX, self.defaultCOMY, self.defaultCOMZ);
    else
        setScale(self.node, 1.0, 1.0, 1.0);
        if self.numActiveDiscs > 0 then
            setTranslation(self.node, 0.0, 0.1, 2.0);
            setRotation(self.node, 0.0, math.pi, 0.0);
        else
            setTranslation(self.node, 0.0, 0.1, -2.0);
            setRotation(self.node, 0.0, 0.0, 0.0);
            discAmount = self.numActiveDiscs * (-1.0);
        end;
        if self.numActiveDiscs > 0 then
            comZ = comZ + (discAmount * self.discCOM);
        else
            comZ = comZ - (discAmount * self.discCOM);
        end;
        setCenterOfMass(self.components[1].node, self.defaultCOMX, self.defaultCOMY, comZ);
            
        for a=1, table.getn(self.Trimmeres) do
            if a < discAmount or a == discAmount then
                local object = self.Trimmeres[a];
                local visibility = getVisibility(object);
                if visibility ~= true then
                    setVisibility(object, true);
                end;
            else
                local object = self.Trimmeres[a];
                local visibility = getVisibility(object);
                if visibility ~= false then
                    setVisibility(object, false);
                end;
            end;
        end;
        if not noEventSend then
            TrimmerEvent.sendEvent(self, self.numActiveDiscs, noEventSend);
        end;
    end;
end;

function Trimmer:readStream(streamId, connection)   
    local numDiscs = streamReadInt8(streamId);
    self:setActiveDisks(numDiscs);
end;

function Trimmer:writeStream(streamId, connection) 
    streamWriteInt8(streamId, self.numActiveDiscs);
end;
