--
-- Massey Ferguson 7726 Dyna-VT Functions
-- 
-- author  Mythos
-- date  18/08/2015
-- www.ARM-team.gr
-- Copyright (C) ARM Team, All Rights Reserved.

MF7726DVT = {};

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

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:load(xmlFile)
	
	-- Functions
	self.MPCode = SpecializationUtil.callSpecializationsFunction("MPCode");
	self.playerCallback = SpecializationUtil.callSpecializationsFunction("playerCallback");
	
	-- Indexes
	self.coolerFan = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.coolerFan#index"));
	
	self.hydBonnetTrigger = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.hydBonnetTrigger#index"));
	self.rightWheelTrigger = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.rightWheelTrigger#index"));
	self.leftWheelTrigger = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.leftWheelTrigger#index"));
	self.leftWheelFTrigger = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.leftWheelFTrigger#index"));
	self.rightWheelFTrigger = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.rightWheelFTrigger#index"));
	
	self.LDW = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.LDW#index"));
	self.RDW = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.RDW#index"));
	self.LFEN = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.LFEN#index"));
	self.RFEN = Utils.indexToObject(self.components,getXMLString(xmlFile,"vehicle.RFEN#index"));
	
	-- Triggers
	self.MFTriggers = {};
	local i=0;
	while true do
		local key = string.format("vehicle.MFTriggers.MFTrigger(%d)", i);
		local trigger = Utils.indexToObject(self.components,getXMLString(xmlFile, key.. "#index"));
		if trigger == nil then
			break;
		end;
		self.MFTriggers[i+1] = {};
		self.MFTriggers[i+1].trigger = trigger;
		self.MFTriggers[i+1].enable = false;
		
		addTrigger(trigger, "playerCallback", self);				
		i = i + 1;
	end;
	
	self.rightWheels = {};
	local i = 0;
	while true do
		local key = string.format("vehicle.rightWheels.rightWheel(%d)", i);
		local wheel = Utils.indexToObject(self.components,getXMLString(xmlFile, key.. "#index"));
		if wheel == nil then
			break;
		end;
		table.insert(self.rightWheels, wheel);
		i = i + 1;
	end;
	
	self.leftWheels = {};
	local i = 0;
	while true do
		local key = string.format("vehicle.leftWheels.leftWheel(%d)", i);
		local wheel = Utils.indexToObject(self.components,getXMLString(xmlFile, key.. "#index"));
		if wheel == nil then
			break;
		end;
		table.insert(self.leftWheels, wheel);
		i = i + 1;
	end;
	
	self.numPlates = {};
	local i = 0;
	while true do
		local key = string.format("vehicle.numPlates.numPlate(%d)", i);
		local plate = Utils.indexToObject(self.components,getXMLString(xmlFile, key.. "#index"));
		if plate == nil then
			break;
		end;
		table.insert(self.numPlates, plate);
		i = i + 1;
	end;
	
	self.changeRWheel = 1;
	self.changeLWheel = 1;
	
	-- Booleans
	self.LKG = true;
	self.LKGF = false;
	self.ct = 4000;
	self.BNT = false;
	self.LDWM = false;
	self.RDWM = false;
	self.LFENM = true;
	self.RFENM = true;
	self.NP = 1;
	
	-- Animations
	self.bonnetAnim = getXMLString(xmlFile, "vehicle.bonnetAnim#animationName");
	self.frontLinkageAnim = getXMLString(xmlFile, "vehicle.frontLinkageAnim#animationName");
	self.frontLinkageSAFETYAnim = getXMLString(xmlFile, "vehicle.frontLinkageSAFETYAnim#animationName");

end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:delete()
	for i,j in pairs(self.MFTriggers) do
		if j.trigger ~= nil then
			removeTrigger(j.trigger);
		end;
	end;
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:mouseEvent(posX, posY, isDown, isUp, button)
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:keyEvent(unicode, sym, modifier, isDown)
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:update(dt)
	
	local enable = false;
	local id = 0;
	for i,j in pairs(self.MFTriggers) do
		if j.enable then
			id = i;
			enable = true;
			break;
		end;
	end;
	
	if enable then
		if id==1 then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_LINKAGE_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self:MPCode(1, not self.LKG); -- Open/Close front Linkage
			end;
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_BONNET_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA3);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
				self:MPCode(2, not self.BNT); -- Open/CLose Bonnet
			end;
		elseif id==2 then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_FEN_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self:MPCode(8, not self.RFENM); -- Right Mudguard
			end;
		elseif id==3 then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_FEN_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self:MPCode(7, not self.LFENM); -- Left Mudguard
			end;
		elseif id==4 then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_RW_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self.changeRWheel = self.changeRWheel+1;
				if self.changeRWheel == 4 then
					self.changeRWheel = 1;
				end;
				self:MPCode(3, nil, self.changeRWheel); -- Change Right Wheel
			end;
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_DW_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA3);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
				self:MPCode(6, not self.RDWM); -- Right Double Wheel
			end;
		elseif id==5 then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_LW_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self.changeLWheel = self.changeLWheel+1;
				if self.changeLWheel == 4 then
					self.changeLWheel = 1;
				end;
				self:MPCode(4, nil, self.changeLWheel); -- Change Left Wheel
			end;
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_DW_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA3);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
				self:MPCode(5, not self.LDWM); -- Left Double Wheel
			end;
		elseif id==6 then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("MF7726_NUMPLATE_MODE"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA2);
			if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA2) then
				self.NP = self.NP+1;
				if self.NP >=11 then
					self.NP = 1;
				end;
				self:MPCode(9, nil, self.NP); -- Number Plate
			end;
		end;
	end;
	
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:updateTick(dt)
	if self.isMotorStarted then
		rotate(self.coolerFan, 0, 0, dt*0.005);
	end;
	
	if self.LKGF then
		self.ct = self.ct - dt;
		if self.ct <0 then
			self.LKGF = false;
			self.ct = 4000;
		end;
	end;
	
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:MPCode(mode, state, a1, noEventSend)
	MF7726DVTEvent.sendEvent(self, mode, state, a1, noEventSend);

--______________________________________________________________________________
	if mode == 10 then
--_____________________________________  LINKAGE SAFETY MODE ______________________________
		self:playAnimation(self.frontLinkageSAFETYAnim, 100, nil, true);
		self.LKG = true;
--______________________________________________________________________________
	elseif mode == 13 then
--_____________________________________  LINKAGE SAFETY MODE ______________________________
		self:playAnimation(self.frontLinkageSAFETYAnim, -100, nil, true);
		self.LKG = false;
--______________________________________________________________________________
	elseif mode == 11 then
--_____________________________________  LINKAGE MODE ______________________________
		self:playAnimation(self.frontLinkageAnim, 100, nil, true);
		self.LKG = true;
--______________________________________________________________________________
	elseif mode == 12 then
--_____________________________________  LINKAGE MODE ______________________________
		self:playAnimation(self.frontLinkageAnim, -100, nil, true);
		self.LKG = false;
--______________________________________________________________________________
	elseif mode == 1 then
--_____________________________________  LINKAGE MODE ______________________________
		if state then
			self:playAnimation(self.frontLinkageAnim, 1, nil, true);
			self.LKG = true;
		else
			local sum = table.getn(self.attachedImplements);
			if sum ~= 0 then
				for i=1, sum do
					local implement = self.attachedImplements[i];
					local jointDescIndex = implement.jointDescIndex;
					if jointDescIndex ==2 then
						self.LKGF = true;
						g_currentMission:showBlinkingWarning(g_i18n:getText("MF7726_DETACH_IMPLEMENT_FIRST"), 4000);
						break;
					end;
				end;
				if not self.LKGF then
					self:playAnimation(self.frontLinkageAnim, -1, nil, true);
					self.LKG = false;
				end;
			else
				self:playAnimation(self.frontLinkageAnim, -1, nil, true);
				self.LKG = false;
			end;
		end;
--______________________________________________________________________________
	elseif mode == 2 then
--_____________________________________  BONNET MODE ______________________________
		if state then
			self:playAnimation(self.bonnetAnim, 1, nil, true);
			self.BNT = true;
		else
			self:playAnimation(self.bonnetAnim, -1, nil, true);
			self.BNT = false;
		end;
--______________________________________________________________________________
	elseif mode == 3 then
--_____________________________________  RIGHT WHEEL MODE ______________________________
		if a1 == 1 then
			setVisibility(self.rightWheels[1], true);
			setVisibility(self.rightWheels[2], false);
			setVisibility(self.rightWheels[3], false);
			self.changeRWheel = 1;
		elseif a1 == 2 then
			setVisibility(self.rightWheels[1], false);
			setVisibility(self.rightWheels[2], true);
			setVisibility(self.rightWheels[3], false);
			self.changeRWheel = 2;
		elseif a1 == 3 then
			setVisibility(self.rightWheels[1], false);
			setVisibility(self.rightWheels[2], false);
			setVisibility(self.rightWheels[3], true);
			self.changeRWheel = 3;
		end;
--______________________________________________________________________________
	elseif mode == 4 then
--_____________________________________  LEFT WHEEL MODE ______________________________
		if a1 == 1 then
			setVisibility(self.leftWheels[1], true);
			setVisibility(self.leftWheels[2], false);
			setVisibility(self.leftWheels[3], false);
			self.changeLWheel = 1;
		elseif a1 == 2 then
			setVisibility(self.leftWheels[1], false);
			setVisibility(self.leftWheels[2], true);
			setVisibility(self.leftWheels[3], false);
			self.changeLWheel = 2;
		elseif a1 == 3 then
			setVisibility(self.leftWheels[1], false);
			setVisibility(self.leftWheels[2], false);
			setVisibility(self.leftWheels[3], true);
			self.changeLWheel = 3;
		end;
--______________________________________________________________________________
	elseif mode == 5 then
--_____________________________________  LEFT DOUBLE WHEEL MODE ______________________________
		if state then
			setVisibility(self.LDW, true);
			self.LDWM = true;
		else
			setVisibility(self.LDW, false);
			self.LDWM = false;
		end;
--______________________________________________________________________________
	elseif mode == 6 then
--_____________________________________  RIGHT DOUBLE WHEEL MODE ______________________________
		if state then
			setVisibility(self.RDW, true);
			self.RDWM = true;
		else
			setVisibility(self.RDW, false);
			self.RDWM = false;
		end;
--______________________________________________________________________________
	elseif mode == 7 then
--_____________________________________  LEFT FENDER MODE ______________________________
		if state then
			setVisibility(self.LFEN, true);
			self.LFENM = true;
		else
			setVisibility(self.LFEN, false);
			self.LFENM = false;
		end;
--______________________________________________________________________________
	elseif mode == 8 then
--_____________________________________  RIGHT FENDER MODE ______________________________
		if state then
			setVisibility(self.RFEN, true);
			self.RFENM = true;
		else
			setVisibility(self.RFEN, false);
			self.RFENM = false;
		end;
--______________________________________________________________________________
	elseif mode == 9 then
--_______________________________  NUM PLATES MODE ______________________________
		local num = table.getn(self.numPlates);
		for i=1,num do
			setVisibility(self.numPlates[i], false);
		end;
		setVisibility(self.numPlates[a1], true);
		self.NP = a1;
	end;

end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:draw()
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)

	if not resetVehicles then
		self.LKG = Utils.getNoNil(getXMLBool(xmlFile, key.."#LKG"),false);
		self.BNT = Utils.getNoNil(getXMLBool(xmlFile, key.."#BNT"),false);
		self.changeRWheel = Utils.getNoNil(getXMLInt(xmlFile, key.."#RRW"),1);
		self.changeLWheel = Utils.getNoNil(getXMLInt(xmlFile, key.."#RLW"),1);
		self.RDWM = Utils.getNoNil(getXMLBool(xmlFile, key.."#RDWM"),false);
		self.LDWM = Utils.getNoNil(getXMLBool(xmlFile, key.."#LDWM"),false);
		self.RFENM = Utils.getNoNil(getXMLBool(xmlFile, key.."#RFENM"),true);
		self.LFENM = Utils.getNoNil(getXMLBool(xmlFile, key.."#LFENM"),true);
		self.NP = Utils.getNoNil(getXMLInt(xmlFile, key.."#NP"),1);
		
		if self.LKG then
			local vehId = getXMLInt(xmlFile, key.."#id")
			local i = 0;
			local safety = false;
			while true do
				local akey = string.format("careerVehicles.attachment(%d)", i);
				local id0 = getXMLInt(xmlFile, akey.."#id0");
				if id0 == nil then
					break;
				end;
				local id1 = getXMLInt(xmlFile, akey.."#id1");
				if id0==vehId or id1==vehId then
					local jIndex = getXMLInt(xmlFile, akey.."#jointIndex");
					if jIndex == 2 then
						self:MPCode(10);
						safety = true;
						break;
					end;
				end;
				i = i + 1;
			end;
			if not safety then
				self:MPCode(11);
			end;
		else
			self:MPCode(12);
		end;
		
		self:MPCode(2, self.BNT);
		self:MPCode(3, nil, self.changeRWheel);
		self:MPCode(4, nil, self.changeLWheel);
		self:MPCode(5, self.LDWM);
		self:MPCode(6, self.RDWM);
		self:MPCode(7, self.LFENM);
		self:MPCode(8, self.RFENM);
		self:MPCode(9, nil, self.NP);
		
	end;
	
	return BaseMission.VEHICLE_LOAD_OK;
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:getSaveAttributesAndNodes(nodeIdent)
	local attributes = 'LKG="'..tostring(self.LKG)..
	'" BNT="'..tostring(self.BNT)..
	'" RRW="'..tostring(self.changeRWheel)..
	'" RLW="'..tostring(self.changeLWheel)..
	'" RDWM="'..tostring(self.RDWM)..
	'" LDWM="'..tostring(self.LDWM)..
	'" RFENM="'..tostring(self.RFENM)..
	'" LFENM="'..tostring(self.LFENM)..
	'" NP="'..tostring(self.NP)..'"';

	local nodes = nil;
	
	return attributes,nodes;
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:readStream(streamId, connection)
	self.LKG = streamReadBool(streamId);
	self.BNT = streamReadBool(streamId);
	self.changeRWheel = streamReadInt32(streamId);
	self.changeLWheel = streamReadInt32(streamId);
	self.LDWM = streamReadBool(streamId);
	self.RDWM = streamReadBool(streamId);
	self.RFENM = streamReadBool(streamId);
	self.LFENM = streamReadBool(streamId);
	self.NP = streamReadInt32(streamId);
	
	if self.LKG then
		self:MPCode(10, nil, nil, true);
	else
		self:MPCode(13, nil, nil, true);
	end;
	
	self:MPCode(2, self.BNT, nil, true);
	self:MPCode(3, nil, self.changeRWheel, true);
	self:MPCode(4, nil, self.changeLWheel, true);
	self:MPCode(5, self.LDWM, nil, true);
	self:MPCode(6, self.RDWM, nil, true);
	self:MPCode(7, self.LFENM, nil, true);
	self:MPCode(8, self.RFENM, nil, true);
	self:MPCode(9, nil, self.NP, true);
	
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:writeStream(streamId, connection)
	streamWriteBool(streamId, self.LKG);
	streamWriteBool(streamId, self.BNT);
	streamWriteInt32(streamId, self.changeRWheel);
	streamWriteInt32(streamId, self.changeLWheel);
	streamWriteBool(streamId, self.LDWM);
	streamWriteBool(streamId, self.RDWM);
	streamWriteBool(streamId, self.RFENM);
	streamWriteBool(streamId, self.LFENM);
	streamWriteInt32(streamId, self.NP);
end;

--________________________________________________________________________________________________________________________________________________________
function MF7726DVT:playerCallback(triggerId, otherId, onEnter, onLeave, onStay)
	
	local id = 0;
	for i,j in pairs(self.MFTriggers) do
		if j.trigger == triggerId then
			id = i;
			break;
		end;
	end;
	
	if id ~= 0 then
		if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
			self.MFTriggers[id].enable = true;
		elseif onLeave then
			self.MFTriggers[id].enable = false;
		end;
	end
	
end;

--**********************************************************************************************************************************************************************************
MF7726DVTEvent = {};
MF7726DVTEvent_mt = Class(MF7726DVTEvent, Event);

InitEventClass(MF7726DVTEvent, "MF7726DVTEvent");

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

function MF7726DVTEvent:new(vehicle, mode, state, a1)
    local self = MF7726DVTEvent:emptyNew()
    self.vehicle = vehicle;
	self.mode = mode;
	self.state = state;
	self.a1 = a1;
    return self;
end;

function MF7726DVTEvent:readStream(streamId, connection)
    local id = streamReadInt32(streamId);
	self.mode = streamReadInt32(streamId);
	self.state = streamReadBool(streamId);
	self.a1 = streamReadInt32(streamId);
    self.vehicle = networkGetObject(id);
    self:run(connection);
end;

function MF7726DVTEvent:writeStream(streamId, connection)
    streamWriteInt32(streamId, networkGetObjectId(self.vehicle));
	streamWriteInt32(streamId, self.mode);
	streamWriteBool(streamId, self.state);
	streamWriteInt32(streamId, self.a1);
end;

function MF7726DVTEvent:run(connection)
	self.vehicle:MPCode(self.mode, self.state, self.a1, true);
    if not connection:getIsServer() then
        g_server:broadcastEvent(MF7726DVTEvent:new(self.vehicle, self.mode, self.state, self.a1), nil, connection, self.vehicle);
    end;
end;

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