-- @ Ziuta
-- @ 27.02.2015
-- @ v1.0
--
-- http://landwirtschafts-simulator.pl/
-- Thanks for you help Koper :P
--
-- You can use and edit but keep the original author ;)
--
-- @ 50keda
-- @ 12.12.2016
-- @ v2.0
--
-- @ Edit @poc@lypse (Ls-modteam-France)
-- @ 25.12.2016 
-- @ Change capacity when you change design parts.
-- Script converted to FS17 to support new "fillUnits" and custom filling volume shapes for each capacity.
-- You can use it and edit but keep the original author ;)

changeCapacity = {}

function changeCapacity.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations) and SpecializationUtil.hasSpecialization(Fillable, specializations)
end

function changeCapacity:load(savegame)    
    self.updateCapacities = changeCapacity.updateCapacities
    self.setMode = changeCapacity.setMode
    
    self.mode = 1
        self.volumeNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.fillVolumes.volumes.volume(0)#index"))          
    self.fillUnitCapacity = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.fillUnits.fillUnit(0)#capacity"), 0.0)
    self.extraCapacities = {}
    local i = 0
    while true do
        local key = string.format("vehicle.extraCapacities.extraCapacity(%d)", i)
        if not hasXMLProperty(self.xmlFile, key) then
            break
        end
        
        local extraCapacity = {}        

        local j = 0
        while true do
            local unitKey = key .. string.format(".fillUnit(%d)", j)
            if not hasXMLProperty(self.xmlFile, unitKey) then
                break
            end

            local capacity = Utils.getNoNil(getXMLFloat(self.xmlFile, string.format("vehicle.fillUnits.fillUnit(%d)#capacity", j)), 0.0) 
                        local volumeNodeIndex = Utils.indexToObject(self.components, getXMLString(self.xmlFile, unitKey.."#volumeNodeIndex"))                    
              local newCapacity = Utils.getNoNil(getXMLFloat(self.xmlFile, unitKey .. "#capacity"), capacity)            
            table.insert(extraCapacity, {volumeNodeIndex=volumeNodeIndex, newCapacity=newCapacity})
            j = j + 1
        end

        table.insert(self.extraCapacities, extraCapacity)
        i = i + 1        
    end    
    if savegame ~= nil then
        self.mode = self.configurations["design"]
        self:updateCapacities(true)
    end
end

function changeCapacity:delete()end

function changeCapacity:readStream(streamId, connection)
    self.mode = streamReadInt8(streamId)
end

function changeCapacity:writeStream(streamId, connection)
    streamWriteInt8(streamId, self.mode)
end

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

function changeCapacity:keyEvent(unicode, sym, modifier, isDown)end

function changeCapacity:update(dt)
if self.isClient then
self:updateCapacities()
if self.configurations["design"] ~= nil then
          self.mode = self.configurations["design"]
end;
end;    
end

function changeCapacity:updateCapacities(force)
    local force = force ~= nil and force

    for k, extraCapacity in pairs(self.extraCapacities) do        
        if k == self.mode and (self:getUnitCapacity(1) ~= extraCapacity[1].newCapacity or force) then
            
            for k1, unit in pairs(self.extraCapacities[k]) do                

                -- set new unit capacity
                self:setUnitCapacity(k1, unit.newCapacity);                  
                               
            end
        end
    end  
end

function changeCapacity:updateTick(dt)end

function changeCapacity:draw()    
end

function changeCapacity:setMode(mode, noEventSend)
    self.mode = mode
    setModeEvent.sendEvent(self, mode, noEventSend)
end

--
-- MP ready
--

setModeEvent = {}
setModeEvent_mt = Class(setModeEvent, Event)

InitEventClass(setModeEvent, "setModeEvent")

function setModeEvent:emptyNew()
    local self = Event:new(setModeEvent_mt)
    return self
end

function setModeEvent:new(object, changeMode)
    local self = setModeEvent:emptyNew()
    self.object = object
    self.changeMode = changeMode
    return self
end

function setModeEvent:readStream(streamId, connection)
    -- local id = streamReadInt32(streamId)
    self.object = readNetworkNodeObject(streamId);
    self.changeMode = streamReadInt8(streamId)
    --self.object = networkGetObject(id)
    self:run(connection)
end

function setModeEvent:writeStream(streamId, connection)
    -- streamWriteInt32(streamId, networkGetObjectId(self.object))
    writeNetworkNodeObject(streamId, self.object);
    streamWriteInt8(streamId, self.changeMode)
end;

function setModeEvent:run(connection)
    self.object:setMode(self.changeMode, true)
    if not connection:getIsServer() then
        g_server:broadcastEvent(setModeEvent:new(self.object, self.changeMode), nil, connection, self.object)
    end
end

function setModeEvent.sendEvent(vehicle, changeMode, noEventSend)
    if noEventSend == nil or noEventSend == false then
        if g_server ~= nil then
            g_server:broadcastEvent(setModeEvent:new(vehicle, changeMode), nil, nil, vehicle)
        else
            g_client:getServerConnection():sendEvent(setModeEvent:new(vehicle, changeMode))
        end
    end
end
