--
-- henly 20 (Ls_uk Modteam)
-- 10.12.2010
--
-- > www.LS-UK.info

KMHautosteeres = {};

function KMHautosteeres.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations) and SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function KMHautosteeres:load(xmlFile)

	self.setAutoSteer = SpecializationUtil.callSpecializationsFunction("setAutoSteer");
    self.maxRotatedTimeSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wheels.maxRotatedTimeSpeed#value"), 2)*0.001;
	self.maxRotatedTimeSpeed = 0;
	self.steeringLocked = true;	
	
	self.controlpath = Utils.getFilename("textures/AutosteerLocked.png", self.baseDirectory);
    self.button1PoxX = 0.93;
    self.button1PoxY = 0.8;
    self.button1Width = 0.065;
    self.button1Height = 0.095;
    self.button1Overlay = Overlay:new("button1", self.controlpath, self.button1PoxX, self.button1PoxY, self.button1Width, self.button1Height);
    self.button1Active = true;

	self.controlpath = Utils.getFilename("textures/AutosteerReleased.png", self.baseDirectory);
    self.button2Overlay = Overlay:new("button2", self.controlpath, self.button1PoxX, self.button1PoxY, self.button1Width, self.button1Height);
    self.button2Active = true;
	
end;

function KMHautosteeres:delete()
end;

function KMHautosteeres:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	local steeringLocked = getXMLBool(xmlFile, key.."#autoSteer");
	if steeringLocked ~= nil then
		self:setAutoSteer(steeringLocked);
	end;
	return BaseMission.VEHICLE_LOAD_OK;
end;

function KMHautosteeres:getSaveAttributesAndNodes(nodeIdent)
	local attributes = 'autoSteer="'..tostring(self.steeringLocked)..'"';
	return attributes, nil;
end;
  
function KMHautosteeres:readStream(streamId, connection)
	local lockedsteer = streamReadBool(streamId);
	self:setAutoSteer(lockedsteer, true);
end;

function KMHautosteeres:writeStream(streamId, connection)
      streamWriteBool(streamId, self.steeringLocked);
end;

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

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

function KMHautosteeres:update(dt)

	if self:getIsActiveForInput() then
		if InputBinding.hasEvent(InputBinding.KMHAUTOSTEERES) then
			self:setAutoSteer(not self.steeringLocked);
		end;
	end;
end;

function KMHautosteeres:updateTick(dt)

	if self.steeringLocked then
		self.autoRotateBackSpeed = 2.5;
		self.maxRotatedTimeSpeed = 2*0.001;
	else
		self.autoRotateBackSpeed = 0;
		self.maxRotatedTimeSpeed = 0;
	end;
end;

function KMHautosteeres:draw()
	if self:getIsActive() then
		g_currentMission:addHelpButtonText(string.format(g_i18n:getText("AUTOSTEER"), self.typeDesc), InputBinding.KMHAUTOSTEERES);
		if self.steeringLocked then
			if self.button2Overlay ~= nil then
				if self.button2Active then
					self.button2Overlay:render();
				end;
			end;
		else
			if self.button1Overlay ~= nil then
				if self.button1Active then
					self.button1Overlay:render();
				end;
			end;
		end;
	end;
end;


function KMHautosteeres:onEnter()
end;

function KMHautosteeres:onLeave()
end;

function KMHautosteeres:setAutoSteer(lockedsteer, noEventSend)
	SetAutoSteerEvent.sendEvent(self, lockedsteer, noEventSend)
	self.steeringLocked = lockedsteer;
end;

SetAutoSteerEvent = {};
SetAutoSteerEvent_mt = Class(SetAutoSteerEvent, Event);
  
InitEventClass(SetAutoSteerEvent, "SetAutoSteerEvent");
  
function SetAutoSteerEvent:emptyNew()
	local self = Event:new(SetAutoSteerEvent_mt);
	self.className="SetAutoSteerEvent";
	return self;
end;
  
function SetAutoSteerEvent:new(object, lockedsteer)
	local self = SetAutoSteerEvent:emptyNew()
	self.object = object;
	self.lockedsteer = lockedsteer;
	return self;
end;
  
function SetAutoSteerEvent:readStream(streamId, connection)
	local id = streamReadInt32(streamId);
	self.lockedsteer = streamReadBool(streamId);
	self.object = networkGetObject(id);
	self:run(connection);
end;
  
function SetAutoSteerEvent:writeStream(streamId, connection)
	streamWriteInt32(streamId, networkGetObjectId(self.object));
	streamWriteBool(streamId, self.lockedsteer);
end;
  
function SetAutoSteerEvent:run(connection)
	if not connection:getIsServer() then
		g_server:broadcastEvent(self, false, connection, self.object);
	end;
	self.object:setAutoSteer(self.lockedsteer, true);
end;
  
function SetAutoSteerEvent.sendEvent(vehicle, lockedsteer, noEventSend)
	if lockedsteer ~= vehicle.steeringLocked then
		if noEventSend == nil or noEventSend == false then
			if g_server ~= nil then
				g_server:broadcastEvent(SetAutoSteerEvent:new(vehicle, lockedsteer), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(SetAutoSteerEvent:new(vehicle, lockedsteer));
			end;
		end;
	end;
end;