Retro = {}

function Retro.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function Retro:load(xmlFile)	
	self.mirrorsEnabled = false;
	self.leftMirrorGlass = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.leftMirrorGlass#index"));
	self.rightMirrorGlass = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rightMirrorGlass#index"));	
	self.leftMirror = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.leftMirror#index"));
	self.rightMirror = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.rightMirror#index"));
	
	if self.mirrorRotationOffsetXleft == nil then
		self.mirrorRotationOffsetXleft = 10.214;
	end;
	
	if self.mirrorRotationOffsetYleft == nil then
		self.mirrorRotationOffsetYleft = -27.29;
	end;

	if self.mirrorRotationOffsetXright == nil then
		self.mirrorRotationOffsetXright = -10.214;
	end;

	if self.mirrorRotationOffsetYright == nil then
		self.mirrorRotationOffsetYright = -27.29;
	end;
	
	if self.mirrorRotationOffsetZleft == nil then
		self.mirrorRotationOffsetZleft = 178.60001;
	end;
	
	if self.mirrorRotationOffsetZright == nil then
		self.mirrorRotationOffsetZright = 1.39319;
	end;
	
	self.shiftMode = 1;
	
	-- HUD --
	self.HUDRetroActive = false;
	self.HUDpath_1 = Utils.getFilename("Scripts/HUD/Retro.png", self.baseDirectory);
	self.HUDWidth_1 = 0.08;
    self.HUDHeight_1 = 0.1;
	self.HUDPoxX_1 = 0.85;
    self.HUDPoxY_1 = 0.55;
    self.HUDRetro = Overlay:new("HUD", self.HUDpath_1, self.HUDPoxX_1, self.HUDPoxY_1, self.HUDWidth_1, self.HUDHeight_1);
	
end;
      
function Retro:delete()
end;

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

function Retro:update(dt)
	-- Input reading
	if self.isEntered then
				
		if InputBinding.hasEvent(InputBinding.RetroHUD) then
			self.HUDRetroActive = not self.HUDRetroActive;
		end;
		if InputBinding.isPressed(InputBinding.RetroLEFT) then
			if self.shiftMode == 1 then
				self.mirrorRotationOffsetYleft = self.mirrorRotationOffsetYleft + 0.5;
			elseif self.shiftMode == 2 then
				self.mirrorRotationOffsetYright = self.mirrorRotationOffsetYright + 0.5;
			end;
		end;
		
		if InputBinding.isPressed(InputBinding.RetroRIGHT) then
			if self.shiftMode == 1 then
				self.mirrorRotationOffsetYleft = self.mirrorRotationOffsetYleft - 0.5;
			elseif self.shiftMode == 2 then
				self.mirrorRotationOffsetYright = self.mirrorRotationOffsetYright - 0.5;
			end;
		end;
		
		if InputBinding.isPressed(InputBinding.RetroFORWARD) then
			if self.shiftMode == 1 then
				self.mirrorRotationOffsetXleft = self.mirrorRotationOffsetXleft + 0.5;
			elseif self.shiftMode == 2 then
				self.mirrorRotationOffsetXright = self.mirrorRotationOffsetXright - 0.5;
			end;
		end;
		
		if InputBinding.isPressed(InputBinding.RetroBACK) then
			if self.shiftMode == 1 then
				self.mirrorRotationOffsetXleft = self.mirrorRotationOffsetXleft - 0.5;
			elseif self.shiftMode == 2 then
				self.mirrorRotationOffsetXright = self.mirrorRotationOffsetXright + 0.5;
			end;
		end;
		
		if InputBinding.hasEvent(InputBinding.SHIFT) then
			self.shiftMode = self.shiftMode + 1;
			if self.shiftMode > 2 then
				self.shiftMode = 1;
			end;
		end;

		if InputBinding.hasEvent(InputBinding.ActivRetro) then
			self.mirrorsEnabled = not self.mirrorsEnabled;
		end;
			-- Mirrors
	if self.leftMirrorGlass ~= nil then
		setVisibility(self.leftMirrorGlass,self.mirrorsEnabled);
	end;
	
	if self.rightMirrorGlass ~= nil then
		setVisibility(self.rightMirrorGlass,self.mirrorsEnabled);
	end;
	
	if self.leftMirror ~= nil then
		setRotation(self.leftMirror,Utils.degToRad(self.mirrorRotationOffsetXleft),Utils.degToRad(self.mirrorRotationOffsetYleft),Utils.degToRad(self.mirrorRotationOffsetZleft));
	end;
	
	if self.rightMirror ~= nil then
		setRotation(self.rightMirror,Utils.degToRad(self.mirrorRotationOffsetXright),Utils.degToRad(self.mirrorRotationOffsetYright),Utils.degToRad(self.mirrorRotationOffsetZright));
	end;
	
	end;
end;
		
function Retro:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
    self.mirrorRotationOffsetXleft = Utils.getNoNil(getXMLFloat(xmlFile, key.."#mirrorRotationOffsetXleft"), 0);
    self.mirrorRotationOffsetYleft = Utils.getNoNil(getXMLFloat(xmlFile, key.."#mirrorRotationOffsetYleft"), 0);
	self.mirrorRotationOffsetXright = Utils.getNoNil(getXMLFloat(xmlFile, key.."#mirrorRotationOffsetXright"), 0);
    self.mirrorRotationOffsetYright = Utils.getNoNil(getXMLFloat(xmlFile, key.."#mirrorRotationOffsetYright"), 0);
	
end;

function Retro:getSaveAttributesAndNodes(nodeIdent)
    local attributes = 'mirrorRotationOffsetXleft="'..self.mirrorRotationOffsetXleft..'" mirrorRotationOffsetYleft="'..self.mirrorRotationOffsetYleft..'" mirrorRotationOffsetXright="'..self.mirrorRotationOffsetXright..'" mirrorRotationOffsetYright="'..self.mirrorRotationOffsetYright..'"';
    return attributes, nil;
end;

function Retro:onLeave()
end;

function Retro:draw()
	if self.HUDRetro ~= nil then
		if self.HUDRetroActive then
			self.HUDRetro:render();
			setTextBold(true);

			setTextColor(1, 1, 1, 1.0);
			if self.shiftMode == 1 then
				renderText(0.01, 0.09, 0.020, string.format("Adjust left mirror mode[%s]",InputBinding.getKeyNamesOfDigitalAction(InputBinding.SHIFT)));
				renderText(0.01, 0.07, 0.020, string.format("left[%s], right[%s], down[%s], up[%s], ",InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroLEFT), InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroRIGHT), InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroFORWARD), InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroBACK)));
			elseif self.shiftMode == 2 then
				renderText(0.01, 0.09, 0.020, string.format("Adjust right mirror mode[%s]",InputBinding.getKeyNamesOfDigitalAction(InputBinding.SHIFT)));
				renderText(0.01, 0.07, 0.020, string.format("left[%s], right[%s], down[%s], up[%s], ",InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroLEFT), InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroRIGHT), InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroFORWARD), InputBinding.getKeyNamesOfDigitalAction(InputBinding.RetroBACK)));
			end;
		end;
	end;
			if not self.mirrorsEnabled then
				g_currentMission:addExtraPrintText(--[[0.01, 0.02, 0.020, ]]string.format("Mirrors off[%s]",InputBinding.getKeyNamesOfDigitalAction(InputBinding.ActivRetro)));
			else
				g_currentMission:addExtraPrintText(--[[0.01, 0.02, 0.020, ]]string.format("Mirrors on[%s]",InputBinding.getKeyNamesOfDigitalAction(InputBinding.ActivRetro)));
			end;
	--g_currentMission:addExtraPrintText(string.format("Key Enter: HUD ON/OFF"));
end;
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		