--
-- AllradV4
-- Specialization for Allrad
--
-- V2 by modelleicher & Made by Case_IH_MxU135 | FIX and V4 by DragonLord2007/modhoster
-- www.schwabenmodding.bplaced.net
-- V4 MP Fähig!Modelleicher 
-- Hud Modhosterteam und borutcebulj


AllradV4 = {};

function AllradV4.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function AllradV4:load(xmlFile)
    self.allradState = AllradV4.allradState;

    self.AllradV4Active = true;
	 
	self.hudAllradPosX = 0.844;
    self.hudAllradWidth = 0.234;
    self.hudAllradPosY = 0.25;
    self.hudAllradHeight = 0.039525;
	
	self.infoPanelAllradONPath = Utils.getFilename("textur/allrad_symb_yes.dds", self.baseDirectory);
	self.hudAllradONOverlay = Overlay:new("allrad_symb_yes", self.infoPanelAllradONPath, self.hudAllradPosX, self.hudAllradPosY, self.hudAllradWidth, self.hudAllradHeight);
	self.infoPanelAllradOFFPath = Utils.getFilename("textur/allrad_symb_no.dds", self.baseDirectory);
	self.hudAllradOFFOverlay = Overlay:new("allrad_symb_no", self.infoPanelAllradOFFPath, self.hudAllradPosX, self.hudAllradPosY, self.hudAllradWidth, self.hudAllradHeight);
	
	self.showhudAllrad = false;
end;

function AllradV4:delete()
    if self.hudAllradONOverlay ~= nil or self.hudAllradOFFOverlay ~= nil then
		self.hudAllradONOverlay:delete();
		self.hudAllradOFFOverlay:delete();		
	end;
end;

function AllradV4:readStream(streamId, connection)  
     self:allradState(streamReadBool(streamId), false);  
end;

function AllradV4:writeStream(streamId, connection)  
    streamWriteBool(streamId, self.AllradV4Active);  
end;

function AllradV4:mouseEvent(posX, posY, isDown, isUp, button)
end;
function AllradV4:keyEvent(unicode, sym, modifier, isDown)
end;

function AllradV4:allradState(state, noEventSend)  
    AllradStateEvent.sendEvent(self, state, noEventSend);	
    self.AllradV4Active = state;
end;

function AllradV4:update(dt)
    if self:getIsActiveForInput() then  
		if InputBinding.hasEvent(InputBinding.AllradSameHurliLambo) then
		  self:allradState(not self.AllradV4Active);
		end;
    end;	
	if self:getIsActive() then	
   		if self.AllradV4Active == true then
			self.showhudAllrad = true;		
			self.wheels[1].driveMode =2
		 	self.wheels[2].driveMode =2
		 	self.wheels[3].driveMode =2
		 	self.wheels[4].driveMode =2			
		else
			self.showhudAllrad = false;
		 	self.wheels[1].driveMode =0
			self.wheels[2].driveMode =0
			self.wheels[3].driveMode =2
			self.wheels[4].driveMode =2			
		end; 	
    end; 
end;

function AllradV4:draw()
   if self.showhudAllrad == true then
      	self.hudAllradONOverlay:render();
		g_currentMission:addHelpButtonText(g_i18n:getText("AllradOFF"), InputBinding.AllradSameHurliLambo);	
	else
		g_currentMission:addHelpButtonText(g_i18n:getText("AllradON"), InputBinding.AllradSameHurliLambo);		
	    self.hudAllradOFFOverlay:render();
   end;
end;

AllradStateEvent = {};
AllradStateEvent_mt = Class(AllradStateEvent, Event);

InitEventClass(AllradStateEvent, "AllradStateEvent");

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

function AllradStateEvent:new(vehicle, state) 
    self.vehicle = vehicle;
    self.state = state;
    return self;
end;

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

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

function AllradStateEvent:run(connection)  
    self.vehicle:allradState(self.state, true);
	if not connection:getIsServer() then  
		g_server:broadcastEvent(AllradStateEvent:new(self.vehicle, self.state), nil, connection, self.object);
	end;
end;

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

