--
-- Automatyczne centrowanie kierownicy
-- By Fumski
-- 15.04.2012
-- Www.LS-Modteam.pl
--

KierownicaCenter = {};

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

function KierownicaCenter:load(xmlFile)

	self.setAutoSteer = SpecializationUtil.callSpecializationsFunction("setAutoSteer");
	
    self.maxRotatedTimeSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wheels.maxRotatedTimeSpeed#value"), 2)*0.001;
	self.autoRotateBackSpeed = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wheels.autoRotateBackSpeed#value"), 2)*0.001;
	self.maxRotatedTimeSpeed = 0;
	self.autoRotateBackSpeed = 0;
	self.steeringLocked = true;	
	
	self.controlpath = Utils.getFilename("textures/Nie.png", self.baseDirectory);
    self.button1PoxX = 0.93;
    self.button1PoxY = 0.7;
    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/Tak.png", self.baseDirectory);
    self.button2PoxX = 0.93;
    self.button2PoxY = 0.7;
    self.button2Width = 0.065;
    self.button2Height = 0.095;
    self.button2Overlay = Overlay:new("button2", self.controlpath, self.button2PoxX, self.button2PoxY, self.button2Width, self.button2Height);
    self.button2Active = true;
	
end;

function KierownicaCenter:readStream(streamId, connection)
    local lockedsteer = streamReadBool(streamId);
    self:setAutoSteer(lockedsteer, true);
end;

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

function KierownicaCenter:keyEvent(unicode, sym, modifier, isDown)

end;

function KierownicaCenter:update(dt)
    if self:getIsActiveForInput() then
        if InputBinding.hasEvent(InputBinding.KierownicaCenter) then
            self:setAutoSteer(not self.steeringLocked);
        end;
    end;
end;

function KierownicaCenter:updateTick(dt)
	if self.steeringLocked then
		self.autoRotateBackSpeed = 2.5
		self.maxRotatedTimeSpeed = 2*0.001;
	else
		self.autoRotateBackSpeed = 0.0
		self.maxRotatedTimeSpeed = 0;
	end;
end;

function KierownicaCenter:draw()
	if self:getIsActive() then
		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;
	if self.steeringLocked then	
		g_currentMission:addExtraPrintText("Klawisz Enter: Automatyczne centrowanie Kierownicy");
	else
		g_currentMission:addExtraPrintText("Klawisz Enter: Reczne centrowanie Kierownicy");
	end;
end;


function KierownicaCenter:onEnter()
end;

function KierownicaCenter:onLeave()
end;

function KierownicaCenter:delete()
end;

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

function KierownicaCenter: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;
