--
-- fertSpreader
-- Class for fertilizer spreader
--
-- @authorPeterJ - euroDZN
-- @date09/09/2013
--
-- http://eurodzn.wordpress.com/
--
-- Copyright (C) euroDZN, Confidential, All Rights Reserved.

fertSpreader = {};

function fertSpreader.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Sprayer, specializations);
end;

function fertSpreader:load(xmlFile)

	self.setExtraBody = SpecializationUtil.callSpecializationsFunction("setExtraBody");
	self.extraBodyNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.fertSpreader#extraBody"));
	self.lowBodyPlane = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.fertSpreader#lowBodyPlane"));
	if self.extraBodyNode ~= nil then
		self.extraCapacity = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.fertSpreader#extraCapacity"), 0);
	end;
	self.extraBodyOn = true;

	self.setOperateCover = SpecializationUtil.callSpecializationsFunction("setOperateCover");
	self.coverAnimation = getXMLString(xmlFile, "vehicle.coverAnim#animationName");
	self.isCoverOpen = false;
	
end;

function fertSpreader:delete()
end;

function fertSpreader:readStream(streamId, connection)
	self:setOperateCover(streamReadBool(streamId), true);
	local extraBodyOn = streamReadBool(streamId);
	self:setExtraBody(extraBodyOn, true);
end;

function fertSpreader:writeStream(streamId, connection)
	streamWriteBool(streamId, self.isCoverOpen);
	streamWriteBool(streamId, self.extraBodyOn);
end;

function fertSpreader:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	if not resetVehicles then
		local coverOpen = getXMLBool(xmlFile, key.."#coverOpen");
		if coverOpen ~= nil then
			self:setOperateCover(coverOpen);
		end;
	end;
	local extraBodyOn = getXMLBool(xmlFile, key.."#extraBodyOn");
	if extraBodyOn ~= nil then
		self:setExtraBody(extraBodyOn);
	end;
	return BaseMission.VEHICLE_LOAD_OK;
end;

function fertSpreader:getSaveAttributesAndNodes(nodeIdent)
	local attributes = 'coverOpen="'..tostring(self.isCoverOpen)..'" extraBodyOn="'..tostring(self.extraBodyOn)..'"';
	return attributes, nil;
end;

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

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

function fertSpreader:update(dt)
	if self.isClient then
		if self.fillLevel <= 0 and self:getIsActiveForInput() then
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
				self:setExtraBody(not self.extraBodyOn);
			end;
		end;
		if self.extraBodyOn and self:getIsActiveForInput() then
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self:setOperateCover(not self.isCoverOpen);
			end;
		end;
	end;
end;

function fertSpreader:updateTick(dt)
	if not self:getIsActive() then
		if self.isTurnedOn then
			self:setIsTurnedOn(false);
		end;
	end;
end;

function fertSpreader:draw()
	if self.isClient then
		if self.fillLevel <= 0 then
			if self.extraBodyOn then
				g_currentMission:addHelpButtonText(g_i18n:getText("RemoveBody"), InputBinding.IMPLEMENT_EXTRA3);
			else
				g_currentMission:addHelpButtonText(g_i18n:getText("AddBody"), InputBinding.IMPLEMENT_EXTRA3);
			end;
		end;
		if self.extraBodyOn then
			if self.isCoverOpen then
				g_currentMission:addHelpButtonText(g_i18n:getText("CloseCover"), InputBinding.IMPLEMENT_EXTRA2);
			else
				g_currentMission:addHelpButtonText(g_i18n:getText("OpenCover"), InputBinding.IMPLEMENT_EXTRA2);
			end;
		end;
	end;
end;

function fertSpreader:setExtraBody(extraBodyOn,noEventSend)
	if extraBodyOn ~= self.extraBodyOn then
		SetExtraBodyEvent.sendEvent(self, extraBodyOn, noEventSend)
  
		self.extraBodyOn = extraBodyOn;
		if self.isClient then
			if self.extraBodyNode ~= nil then
				if self.extraBodyOn then
					setVisibility(self.extraBodyNode, true);
					setVisibility(self.lowBodyPlane, false);
					self.capacity = self.capacity + self.extraCapacity;
				else
					setVisibility(self.extraBodyNode, false);
					setVisibility(self.lowBodyPlane, true);
					self.capacity = self.capacity - self.extraCapacity;
				end;
			end;
		end;
	end;
end;

function fertSpreader:setOperateCover(isCoverOpen,noEventSend)
	setCoverAnimEvent.sendEvent(self, isCoverOpen, noEventSend);

	self.isCoverOpen = isCoverOpen;
	if self.isCoverOpen then
		if self.coverAnimation ~= nil and self.playAnimation ~= nil then
			self:playAnimation(self.coverAnimation, 1, nil, true);
		end;
	else
		if self.coverAnimation ~= nil and self.playAnimation ~= nil then
			self:playAnimation(self.coverAnimation, -1, nil, true);
		end;
	end;	
end;

setCoverAnimEvent = {};
setCoverAnimEvent_mt = Class(setCoverAnimEvent, Event);

InitEventClass(setCoverAnimEvent, "setCoverAnimEvent");

function setCoverAnimEvent:emptyNew()
	local self = Event:new(setCoverAnimEvent_mt);
	self.className="setCoverAnimEvent";
	return self;
end;

function setCoverAnimEvent:new(vehicle, isCoverOpen)
	local self = setCoverAnimEvent:emptyNew()
	self.vehicle = vehicle;
	self.isCoverOpen = isCoverOpen;
	return self;
end;

function setCoverAnimEvent:readStream(streamId, connection)
	local id = streamReadInt32(streamId);
	self.isCoverOpen = streamReadBool(streamId);
	self.vehicle = networkGetObject(id);
	self:run(connection);
end;

function setCoverAnimEvent:writeStream(streamId, connection)
	streamWriteInt32(streamId, networkGetObjectId(self.vehicle));
	streamWriteBool(streamId, self.isCoverOpen);
end;

function setCoverAnimEvent:run(connection) 
	self.vehicle:setOperateCover(self.isCoverOpen, true);
	if not connection:getIsServer() then
	g_server:broadcastEvent(setCoverAnimEvent:new(self.vehicle, self.isCoverOpen), nil, connection, self.vehicle);
	end;
end;

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

SetExtraBodyEvent = {};
SetExtraBodyEvent_mt = Class(SetExtraBodyEvent, Event);

InitEventClass(SetExtraBodyEvent, "SetExtraBodyEvent");

function SetExtraBodyEvent:emptyNew()
	local self = Event:new(SetExtraBodyEvent_mt);
	return self;
end;

function SetExtraBodyEvent:new(object, extraBodyOn)
	local self = SetExtraBodyEvent:emptyNew()
	self.object = object;
	self.extraBodyOn = extraBodyOn;
	return self;
end;

function SetExtraBodyEvent:readStream(streamId, connection)
	local id = streamReadInt32(streamId);
	self.extraBodyOn = streamReadBool(streamId);
	self.object = networkGetObject(id);
	self:run(connection);
end;

function SetExtraBodyEvent:writeStream(streamId, connection)
	streamWriteInt32(streamId, networkGetObjectId(self.object));
	streamWriteBool(streamId, self.extraBodyOn);
end;

function SetExtraBodyEvent:run(connection)
	if not connection:getIsServer() then
	g_server:broadcastEvent(self, false, connection, self.object);
end;
if self.object ~= nil then
	self.object:setExtraBody(self.extraBodyOn, true);
	end;
end;

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