-- @history: loadFromAttributesAndNodes() fix by Jakob Tischler, 04 May 2014

ShiftableMass = {};

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

function ShiftableMass:load(savegame)
	self.massNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.shiftableMass#massNode"));
	self.massPerDisc = getXMLFloat(self.xmlFile, "vehicle.shiftableMass#massPerDisc");
	self.setActiveDisks = SpecializationUtil.callSpecializationsFunction("setActiveDisks");
	self.shiftableMasses = {}
	self.numActiveDiscs = 0;
	self.massBackup = getXMLFloat(self.xmlFile, "vehicle.shiftableMass#defaultMass");
	
	local num = 0;
	while true do
		num = num + 1;
		local path = "vehicle.shiftableMass.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.shiftableMasses, discId);
		end;	
	end;
	self.maxNumDiscs = num;
end;

function ShiftableMass:postLoad(savegame)
    if savegame ~= nil and not savegame.resetVehicles then
        self:setActiveDisks(getXMLInt(savegame.xmlFile, savegame.key .. '#numDiscs') or 0, true);
    end;
end

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

function ShiftableMass:delete()
  
end;

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

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

function ShiftableMass:update(dt)
	if self:getIsActiveForInput(true) then
		if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA) then
			self:setActiveDisks(self.numActiveDiscs + 1,false);
		end;
		if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
			self:setActiveDisks(self.numActiveDiscs - 1,false);
		end;
	end;
end;

function ShiftableMass:draw()
	if self:getIsActiveForInput(true) then
		if self.numActiveDiscs < 8 then
			g_currentMission:addHelpButtonText(g_i18n:getText("addDisc"), InputBinding.IMPLEMENT_EXTRA); 
		end;
		if self.numActiveDiscs > 0 then		
			g_currentMission:addHelpButtonText(g_i18n:getText("takeAwayDisc"), InputBinding.IMPLEMENT_EXTRA2);			
		end;
		
		local weight = string.format("%.1f", self.massBackup + (self.numActiveDiscs * self.massPerDisc));
		g_currentMission:addExtraPrintText(g_i18n:getText("weight") .. ": " .. weight .." t");
	end;
end;

function ShiftableMass:setActiveDisks(numDiscs, noEventSend)
	if self.shiftableMasses[numDiscs] ~= nil or numDiscs == 0 then
		self.numActiveDiscs = numDiscs;
		if self.isServer then
			local mass = (numDiscs*self.massPerDisc) + self.massBackup;
			if mass > 0 then
				setMass(self.massNode, mass);
			end;
		end;
		for a=1, table.getn(self.shiftableMasses) do
			if a < numDiscs or a == numDiscs then
				local object = self.shiftableMasses[a];
				local visibility = getVisibility(object);
				if visibility ~= true then
					setVisibility(object, true);
				end;
			else
				local object = self.shiftableMasses[a];
				local visibility = getVisibility(object);
				if visibility ~= false then
					setVisibility(object, false);
				end;
			end;
		end;
		if not noEventSend then
			ShiftableMassEvent.sendEvent(self, self.numActiveDiscs, noEventSend);
		end;
	end;
end;

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

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