--
-- remoteControlWrapper
-- Specialization for remote control operate wrapper
--
-- @author  PeterJ
-- @date  20/10/2017
--
-- Copyright (C) PeterJ, Confidential, All Rights Reserved.
--
--


remoteControlWrapper = {};

function remoteControlWrapper.prerequisitesPresent(specializations)
	return SpecializationUtil.hasSpecialization(Attachable, specializations);
end;

function remoteControlWrapper:preLoad(savegame)
	self.setIsTurnedOn = remoteControlWrapper.setIsTurnedOn;
	self.getIsTurnedOnAllowed = remoteControlWrapper.getIsTurnedOnAllowed;
end;

function remoteControlWrapper:load(savegame)
	self.turnOnVehicle = {}
	self.turnOnVehicle.isTurnedOn = false;

	--- remote control ---
	self.RCreceiver = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.wrapper.remoteControl#receiver"));
	if self.RCreceiver ~= nil then
		self.RCrange = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.wrapper.remoteControl#range"), 15);
		self.receiverLight = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.wrapper.remoteControl#receiverLight"));
		self.RCinRange = false;
	end;
	
	
	self.RCnearestBale = nil;
	self.RCnearestBaleType = nil;

	self.allowUseRC = false;
end;
  
function remoteControlWrapper:delete()
end;
  
function remoteControlWrapper:readStream(streamId, connection)
    local turnedOn = streamReadBool(streamId)
	self:setIsTurnedOn(turnedOn, true)
end;

function remoteControlWrapper:writeStream(streamId, connection)
	streamWriteBool(streamId, self.turnOnVehicle.isTurnedOn)
end;
  
function remoteControlWrapper:mouseEvent(posX, posY, isDown, isUp, button)
end;

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

function remoteControlWrapper:update(dt)
	if not self.allowUseRC then
		if self:getIsTurnedOnAllowed() and not self:hasInputConflictWithSelection() then
			if not self.turnOnVehicle.isTurnedOn and self.baleWrapperState == baleWrapperRC.STATE_NONE then
				if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA) then
					self:setIsTurnedOn(true);
				end;
			end;
		end;
	else
		if self.RCreceiver ~= nil then
			if self.RCinRange then
				if not self.turnOnVehicle.isTurnedOn then
					if self:getIsTurnedOnAllowed() then	
						g_currentMission:addHelpButtonText(string.format(g_i18n:getText("action_turnOnOBJECT"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA, nil, GS_PRIO_HIGH);
						if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA) then
							self:setIsTurnedOn(true);
						end;
					end;
				else
					if self.baleWrapperState == baleWrapperRC.STATE_WRAPPER_FINSIHED then
						g_currentMission:addHelpButtonText(g_i18n:getText(self.currentWrapper.unloadBaleText), InputBinding.IMPLEMENT_EXTRA3, nil, GS_PRIO_HIGH);
						if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
							g_client:getServerConnection():sendEvent(BaleWrapperStateEvent:new(self, baleWrapperRC.CHANGE_BUTTON_EMPTY));
						end;
					end;
				end;
			end;
		end;
	end;
end;

function remoteControlWrapper:updateTick(dt)
	if not self.turnOnVehicle.isTurnedOn then
        self.showInvalidBaleWarning = false;
        if self:allowsGrabbingBale() then
            if self.baleGrabber.grabNode ~= nil and self.baleGrabber.currentBale == nil then
				self.RCnearestBale, self.RCnearestBaleType = baleWrapperRC.getBaleInRange(self, self.baleGrabber.grabNode, self.baleGrabber.nearestDistance);
				if self.RCnearestBale ~= nil then
                    if self.RCnearestBaleType == nil then
                        if self.lastDroppedBale ~= self.RCnearestBale then
                            self.showInvalidBaleWarning = true;
                        end;
					end;
                end;
			end;
        end;
	end;
	local rootAttacherVehicle = self:getRootAttacherVehicle()
	if rootAttacherVehicle ~= self then
		if rootAttacherVehicle.isEntered ~= nil then
			if rootAttacherVehicle.isEntered then
				self.allowUseRC = false;
			else
				self.allowUseRC = true;
			end;
		end;
	else
		self.allowUseRC = true;
	end;
	--[[print("allowUseRC",self.allowUseRC);
	print("RCinRange",self.RCinRange);
	print("isTurnedOn",self.turnOnVehicle.isTurnedOn);]]
	--- calculate character or driver distance from RC receiver ---
	if self.allowUseRC then
		if self.RCreceiver ~= nil then
			if g_currentMission.player ~= nil then
				local x,y,z = getWorldTranslation(self.RCreceiver);
				local x1,y1,z1 = getWorldTranslation(g_currentMission.player.rootNode);
				local distance = Utils.vector3Length(x-x1,y-y1,z-z1);
				self.characterInRCrange = false; 
				if distance < self.RCrange then
					self.characterInRCrange = true;
				end;
				if g_currentMission.controlledVehicle == nil then
					self.driverInRCrange = false; 
				else
					local x2,y2,z2 = getWorldTranslation(g_currentMission.controlledVehicle.rootNode);
					local distance2 = Utils.vector3Length(x-x2,y-y2,z-z2);
					self.driverInRCrange = false; 
					if distance2 < self.RCrange then
						self.driverInRCrange = true;
					end;
				end;
				if self.characterInRCrange or self.driverInRCrange then
					self.RCinRange = true;
				else
					self.RCinRange = false;
				end;
			end;
		end;
	else
		self.RCinRange = false;
	end;
	if self.receiverLight ~= nil then
		local lightReceiverOn = false;
		if self.RCinRange and (self:getIsTurnedOnAllowed() or (self.turnOnVehicle.isTurnedOn and self.baleWrapperState == baleWrapperRC.STATE_WRAPPER_FINSIHED)) then
			lightReceiverOn = true;
		end;
		setVisibility(self.receiverLight, lightReceiverOn);
	end;
end;

function remoteControlWrapper:draw()
	if self.isClient then
        if self:getIsActiveForInput(true) then
            if self.baleWrapperState == baleWrapperRC.STATE_NONE and not self.turnOnVehicle.isTurnedOn and self:getIsTurnedOnAllowed() and not self.allowUseRC then
                g_currentMission:addHelpButtonText(string.format(g_i18n:getText("action_turnOnOBJECT"), self.typeDesc), InputBinding.IMPLEMENT_EXTRA, nil, GS_PRIO_HIGH);
			end;
        end;
    end;
end;

function remoteControlWrapper:setIsTurnedOn(isTurnedOn, noEventSend)
	if isTurnedOn ~= self.turnOnVehicle.isTurnedOn then
		SetTurnedOnEvent.sendEvent(self, isTurnedOn, noEventSend)
		self.turnOnVehicle.isTurnedOn = isTurnedOn;
	end;
end;

function remoteControlWrapper:getIsTurnedOnAllowed(isTurnedOn)
	if not self.turnOnVehicle.isTurnedOn then
		if self.RCnearestBale ~= nil and self.RCnearestBaleType ~= nil and self.baleWrapperState == baleWrapperRC.STATE_NONE and self.currentWrapper.currentBale == nil then
			return true;
		end;
	end;
    return false;
end;

