--
-- BumbGewicht
-- Specialization for BumbGewicht
--
-- @author Filip "MonkeyWalk" F.
-- @date  01/02/16 v1.0
--
-- Copyright (C) MonkeyWalk, All Rights Reserved.
--


BumbGewichtSide = {};

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

function BumbGewichtSide:load(savegame)
    self.setSideState = SpecializationUtil.callSpecializationsFunction("setSideState");
    
    self.sideRight = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.BumbGewicht.sideRight#index"));
    self.sideLeft = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.BumbGewicht.sideLeft#index"));    
    
    self.SideState = 0;    
end;

function BumbGewichtSide:delete()
end;

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

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

function BumbGewichtSide:update(dt)
    if self:getIsActiveForInput() then
         if InputBinding.hasEvent(InputBinding.moveSides) then
             g_client:getServerConnection():sendEvent(BumbGewichtSideEvent:new(self, self.SideState + 1 ));
         end;
    end;  
    if self.newSideState then
        self.newSideState = nil;
        if self.SideState == 0  then
            setTranslation(self.sideRight, -0.254, 0.371, 0.358);
            setTranslation(self.sideLeft, 0.368, 0.371, -0.269);
        elseif self.SideState == 1 then
            setTranslation(self.sideRight, -0.321, 0.371, 0.304);
            setTranslation(self.sideLeft, 0.442, 0.371, -0.325);
        elseif self.SideState == 2 then
            setTranslation(self.sideRight, -0.379, 0.371, 0.26);
            setTranslation(self.sideLeft, 0.495, 0.371, -0.365);
		elseif self.SideState == 3 then
            setTranslation(self.sideRight, -0.436, 0.371, 0.219);
            setTranslation(self.sideLeft, 0.539, 0.371, -0.399);
		elseif self.SideState == 4 then
            setTranslation(self.sideRight, -0.491, 0.371, 0.177);
            setTranslation(self.sideLeft, 0.591, 0.371, -0.439);
        elseif self.SideState == 5 then
            setTranslation(self.sideRight, -0.254, 0.371, 0.358);
            setTranslation(self.sideLeft, 0.368, 0.371, -0.269);
			self.SideState = 0;           
        end;
    end;
end;
function BumbGewichtSide:setSideState(state)
    self.SideState = state;
    self.newSideState = true;
    if self.isServer then
        g_server:broadcastEvent(BumbGewichtSideEvent:new(self, self.SideState), true, nil, self);
    end;
end
function BumbGewichtSide:updateTick(dt)
end;

function BumbGewichtSide:draw()
g_currentMission:addHelpButtonText(g_i18n:getText("moveSides"), InputBinding.moveSides);    
end;

BumbGewichtSideEvent = {};
BumbGewichtSideEvent_mt = Class(BumbGewichtSideEvent, Event);
InitEventClass(BumbGewichtSideEvent, "BumbGewichtSideEvent");
function BumbGewichtSideEvent:emptyNew()
    local self = Event:new(BumbGewichtSideEvent_mt);
    return self;
end;
function BumbGewichtSideEvent:new(object, stateId)
    local self = BumbGewichtSideEvent:emptyNew()
    self.object = object;
    self.SideStateId = stateId;
    return self;
end;
function BumbGewichtSideEvent:readStream(streamId, connection)
    self.object = networkGetObject(streamReadInt32(streamId));
    self.SideStateId = streamReadInt8(streamId);
    self:run(connection);
end;
function BumbGewichtSideEvent:writeStream(streamId, connection)
    streamWriteInt32(streamId, networkGetObjectId(self.object));
    streamWriteInt8(streamId, self.SideStateId);
   
end;
function BumbGewichtSideEvent:run(connection)
    if self.object.SideState ~= self.SideStateId then
        self.object:setSideState(self.SideStateId);
    end
end;