--[[Autor: Patar
    Idee : the Death
    Helfer: sven777b (nur für die funktion des abrufens der capazität und des Füllstandes aller Anhänger)
    Modell: Freddy.com

		danke an Freddy für das super modell, danke an sven für die hilfe, immer wenn ich ihn brauche, und danke an meinen Bruder für die Idee ;) ]]--


greenstar2 = {};

function greenstar2.prerequisitesPresent(specializations)
    return true;
end;
function greenstar2:load(xmlFile)
	local lampscount = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.lamps#count"), 0);
	self.lamps = {};
	if lampscount ~= 0 then
		for i=1, lampscount do
			local lampnumber = string.format("vehicle.lamps.lamp%d", i);	
			self.lamps[i] = Utils.indexToObject(self.components, getXMLString(xmlFile, lampnumber .. "#index"));	
		end;	
	end;



---------OPTIONS--------------
	self.opt = {};
	self.flash = {};
	self.sound = {};
	self.flash.arrowleft = {}
	self.flash.arrowright = {}

	self.sound.mouseOver = false;
	self.flash.mouseOver = false;
	self.flash.arrowleft.mouseOver = false;
	self.flash.arrowright.mouseOver = false;

	self.sound.funcActive = false;
	self.flash.funcActive = false;

	self.sound.percentSet = 50;
	self.flash.percentSet = 50;

	self.ausgefuehrt = false;
	self.prozent = 0;
	self.flash.counter = 0;
	self.flash.counterend = 20;

	optfieldpath = Utils.getFilename("optfield.png", self.baseDirectory);
	soundfieldpath = Utils.getFilename("soundfield.png", self.baseDirectory);
	flashfieldpath = Utils.getFilename("flashfield.png", self.baseDirectory);
	buttonredpath = Utils.getFilename("buttonred.png", self.baseDirectory);
	buttonorangepath = Utils.getFilename("buttonorange.png", self.baseDirectory);
	buttongreenpath = Utils.getFilename("buttongreen.png", self.baseDirectory);
	arrowleftpath = Utils.getFilename("arrow_left.png", self.baseDirectory);
	arrowrightpath = Utils.getFilename("arrow_right.png", self.baseDirectory);


	self.opt.field = Overlay:new("optfield", optfieldpath, 0.67, 0.02, 0.15, 0.06);
	self.opt.buttonred = Overlay:new("optbuttonred", buttonredpath, 0.67, 0.025, 0.04, 0.05);
	self.opt.buttongreen = Overlay:new("optbuttongreen", buttongreenpath, 0.67, 0.025, 0.04, 0.05);

	self.sound.field = Overlay:new("soundfield", soundfieldpath, 0.67, 0.08, 0.15, 0.06);
	self.sound.buttonred = Overlay:new("soundbuttonred", buttonredpath, 0.67, 0.085, 0.04, 0.05);
	self.sound.buttonorange = Overlay:new("soundbuttonorange", buttonorangepath, 0.67, 0.085, 0.04, 0.05);
	self.sound.buttongreen = Overlay:new("soundbuttongreen", buttongreenpath, 0.67, 0.085, 0.04, 0.05);
	self.sound.arrowleft = Overlay:new("soundarrowleft", arrowleftpath, 0.74, 0.095, 0.02, 0.025);
	self.sound.arrowright = Overlay:new("soundarrowright", arrowrightpath, 0.8, 0.095, 0.02, 0.025);

	self.flash.field = Overlay:new("flashfield", flashfieldpath, 0.67, 0.14, 0.15, 0.06);
	self.flash.buttonred = Overlay:new("flashbuttonred", buttonredpath, 0.67, 0.145, 0.04, 0.05);
	self.flash.buttonorange = Overlay:new("flashbuttonorange", buttonorangepath, 0.67, 0.145, 0.04, 0.05);
	self.flash.buttongreen = Overlay:new("flashbuttongreen", buttongreenpath, 0.67, 0.145, 0.04, 0.05);
	self.flash.arrowleft.pic = Overlay:new("flasharrowleft", arrowleftpath, 0.74, 0.155, 0.02, 0.025);
	self.flash.arrowright.pic = Overlay:new("flasharrowright", arrowrightpath, 0.8, 0.155, 0.02, 0.025);

	local alarmSound = getXMLString(xmlFile, "vehicle.alarmSound#file");
    	if alarmSound ~= nil and alarmSound ~= "" then
     	  alarmSound = Utils.getFilename(alarmSound, self.baseDirectory); 
     	  self.alarmSound = createSample("alarmSound");
      	  self.alarmSoundEnabled = false;
      	  loadSample(self.alarmSound, alarmSound, false);
    	end;
	local buttonSound = getXMLString(xmlFile, "vehicle.buttonSound#file");
    	if buttonSound ~= nil and buttonSound ~= "" then
     	  buttonSound = Utils.getFilename(buttonSound, self.baseDirectory); 
     	  self.buttonSound = createSample("buttonSound");
      	  self.buttonSoundEnabled = false;
      	  loadSample(self.buttonSound, buttonSound, false);
    	end;



end;
function greenstar2:delete()
end;
function greenstar2:mouseEvent(posX, posY, isDown, isUp, button)
	if self.isEntered then
		xsb = 0.69;
		ysb = 0.11;

		xfb = 0.69;
		yfb = 0.17;

		xfl = 0.75
		yfl = 0.165
	
		xfr = 0.81
		yfr = 0.165

		xsl = 0.75
		ysl = 0.105

		xsr = 0.81
		ysr = 0.105

		distancesb = Utils.vector2Length(posX-xsb, posY-ysb)
		distancefb = Utils.vector2Length(posX-xfb, posY-yfb)
		distancefl = Utils.vector2Length(posX-xfl, posY-yfl)
		distancefr = Utils.vector2Length(posX-xfr, posY-yfr)
		distancesl = Utils.vector2Length(posX-xsl, posY-ysl)
		distancesr = Utils.vector2Length(posX-xsr, posY-ysr)


		if distancesb <= 0.021 then
			self.sound.mouseOver = true;
		else
			self.sound.mouseOver = false;
		end;
		if distancefb <= 0.021 then
			self.flash.mouseOver = true;
		else
			self.flash.mouseOver = false;
		end;
		if distancefl <= 0.012 then
			self.flash.arrowleft.mouseOver = true;
		else
			self.flash.arrowleft.mouseOver = false;
		end;
		if distancefr <= 0.012 then
			self.flash.arrowright.mouseOver = true;
		else
			self.flash.arrowright.mouseOver = false;
		end;
		if distancesl <= 0.012 then
			self.sound.arrowleft.mouseOver = true;
		else
			self.sound.arrowleft.mouseOver = false;
		end;
		if distancesr <= 0.012 then
			self.sound.arrowright.mouseOver = true;
		else
			self.sound.arrowright.mouseOver = false;
		end;


		if button == 1 and isDown == true and self.flash.mouseOver == true then
			playSample(self.buttonSound, 1, 0.5, 2);
			setSamplePitch(self.buttonSound,1);
			self.flash.funcActive = not self.flash.funcActive;
			if self.flash.funcActive == false then
				setVisibility(getParent(self.lamps[1]), true);
			end;
		end;
		if button == 1 and isDown and self.sound.mouseOver then
			playSample(self.buttonSound, 1, 0.5, 2);
			setSamplePitch(self.buttonSound,1);
			self.sound.funcActive = not self.sound.funcActive;
		end;
		if button == 1 and isDown and self.flash.arrowleft.mouseOver then
			playSample(self.buttonSound, 1, 0.5, 2);
			setSamplePitch(self.buttonSound,1);
			if self.flash.percentSet > 5 then
				self.flash.percentSet = self.flash.percentSet-5;
			end;
		end;
		if button == 1 and isDown and self.flash.arrowright.mouseOver then
			playSample(self.buttonSound, 1, 0.5, 2);
			setSamplePitch(self.buttonSound,1);
			if self.flash.percentSet < 100 then
				self.flash.percentSet = self.flash.percentSet+5;
			end;
		end;

		if button == 1 and isDown and self.sound.arrowleft.mouseOver then
			playSample(self.buttonSound, 1, 0.5, 2);
			setSamplePitch(self.buttonSound,1);
			if self.sound.percentSet > 5 then
				self.sound.percentSet = self.sound.percentSet-5;
			end;
		end;
		if button == 1 and isDown and self.sound.arrowright.mouseOver then
			playSample(self.buttonSound, 1, 0.5, 2);
			setSamplePitch(self.buttonSound,1);
			if self.sound.percentSet < 100 then
				self.sound.percentSet = self.sound.percentSet+5;
			end;
		end;
	end;
end;




function greenstar2:keyEvent(unicode, sym, modifier, isDown)
end;
function greenstar2:update(dt)



	self.flash.counter = self.flash.counter + 1

	if self.menuActive then
		for i = 1, table.getn(self.cameras) do
			self.cameras[i].isActivated = false;
		end;
		setShowMouseCursor(true)
	else
		for i = 1, table.getn(self.cameras) do
			self.cameras[i].isActivated = true;
		end;
	end;

   if self.isEntered then
	if InputBinding.isPressed(InputBinding.TOGGLEOPTMHD) then
		self.menuActive = true;
		self.ausgefuehrt = false
	else
		self.menuActive = false;
		if self.ausgefuehrt ~= true then
			setShowMouseCursor(false)
		 	wrapMousePosition(0.5, 0.5);
		end;
	end;
   end;

		self.prozent = self.grainTankFillLevel/self.grainTankCapacity*100;
		if self.prozent >= 4 then
			setVisibility(self.lamps[1], true)
		else
			setVisibility(self.lamps[1], false)
		end;
		if self.prozent >= 8 then
			setVisibility(self.lamps[2], true)
		else
			setVisibility(self.lamps[2], false)
		end;
		if self.prozent >= 12 then
			setVisibility(self.lamps[3], true)
		else
			setVisibility(self.lamps[3], false)
		end;
		if self.prozent >= 16 then
			setVisibility(self.lamps[4], true)
		else
			setVisibility(self.lamps[4], false)
		end;
		if self.prozent >= 20 then
			setVisibility(self.lamps[5], true)
		else
		setVisibility(self.lamps[5], false)
		end;
		if self.prozent >= 24 then
			setVisibility(self.lamps[6], true)
		else
			setVisibility(self.lamps[6], false)
		end;
		if self.prozent >= 28 then
			setVisibility(self.lamps[7], true)
		else
			setVisibility(self.lamps[7], false)
		end;
		if self.prozent >= 32 then
			setVisibility(self.lamps[8], true)
		else
			setVisibility(self.lamps[8], false)
		end;
		if self.prozent >= 36 then
			setVisibility(self.lamps[9], true)
		else
			setVisibility(self.lamps[9], false)
		end;
		if self.prozent >= 40 then
			setVisibility(self.lamps[10], true)
		else
			setVisibility(self.lamps[10], false)
		end;
		if self.prozent >= 44 then
			setVisibility(self.lamps[11], true)
		else
			setVisibility(self.lamps[11], false)
		end;
		if self.prozent >= 48 then
			setVisibility(self.lamps[12], true)
		else
			setVisibility(self.lamps[12], false)
		end;
		if self.prozent >= 52 then
			setVisibility(self.lamps[13], true)
		else
			setVisibility(self.lamps[13], false)
		end;
		if self.prozent >= 56 then
			setVisibility(self.lamps[14], true)
		else
			setVisibility(self.lamps[14], false)
		end;
		if self.prozent >= 60 then
			setVisibility(self.lamps[15], true)
		else
			setVisibility(self.lamps[15], false)
		end;
		if self.prozent >= 64 then
			setVisibility(self.lamps[16], true)
		else
			setVisibility(self.lamps[16], false)
		end;
		if self.prozent >= 68 then
			setVisibility(self.lamps[17], true)
		else
			setVisibility(self.lamps[17], false)
		end;
		if self.prozent >= 72 then
			setVisibility(self.lamps[18], true)
		else
			setVisibility(self.lamps[18], false)
		end;
		if self.prozent >= 76 then
			setVisibility(self.lamps[19], true)
		else
			setVisibility(self.lamps[19], false)
		end;
		if self.prozent >= 80 then
			setVisibility(self.lamps[20], true)
		else
			setVisibility(self.lamps[20], false)
		end;
		if self.prozent >= 84 then
			setVisibility(self.lamps[21], true)
		else
			setVisibility(self.lamps[21], false)
		end;
		if self.prozent >= 88 then
			setVisibility(self.lamps[22], true)
		else
			setVisibility(self.lamps[22], false)
		end;
		if self.prozent >= 92 then
			setVisibility(self.lamps[23], true)
		else
			setVisibility(self.lamps[23], false)
		end;
		if self.prozent >= 96 then
			setVisibility(self.lamps[24], true)
		else
			setVisibility(self.lamps[24], false)
		end;
		if self.prozent >= 100 then
			setVisibility(self.lamps[25], true)
		else
			setVisibility(self.lamps[25], false)
		end;


	
	if self.flash.funcActive then
		if self.prozent >= self.flash.percentSet then
			if self.flash.counter > self.flash.counterend and self.flash.counter < self.flash.counterend + 2 then
				setVisibility(getParent(self.lamps[1]), true);
			end;
			if self.flash.counter > self.flash.counterend * 2 then
				setVisibility(getParent(self.lamps[1]), false);
				self.flash.counter = 0;
			end;
		else
			setVisibility(getParent(self.lamps[1]), true);
		end;
	end;

	if self.sound.funcActive and self.prozent >= self.sound.percentSet and 	self.isEntered then
		if not self.alarmSoundEnabled then
			playSample(self.alarmSound, 0, 0.5, 2);
			setSamplePitch(self.alarmSound,1);
			self.alarmSoundEnabled = true;
		end;
	else
		stopSample(self.alarmSound);
		self.alarmSoundEnabled = false;
	end;

end;
function greenstar2:draw()
	if not self.menuActive then
		g_currentMission:addHelpButtonText(g_i18n:getText("show_menu"), InputBinding.TOGGLEOPTMHD);
	end;
	self.opt.field:render();
	if self.menuActive then
		renderText(0.765, 0.16, 0.02, tostring(self.flash.percentSet.."%"));
		renderText(0.765, 0.1, 0.02, tostring(self.sound.percentSet.."%"));
		self.sound.field:render();
		self.flash.field:render();
		self.sound.arrowright:render();
		self.sound.arrowleft:render();
		self.flash.arrowright.pic:render();
		self.flash.arrowleft.pic:render();
		self.opt.buttongreen:render();
		if self.sound.mouseOver then
			self.sound.buttonorange:render();
		end;
		if self.flash.mouseOver then
			self.flash.buttonorange:render();
		end;
		if self.flash.funcActive and not self.flash.mouseOver then
			self.flash.buttongreen:render();
		elseif not self.flash.mouseOver then
			self.flash.buttonred:render();
		end;
		if self.sound.funcActive and not self.sound.mouseOver then
			self.sound.buttongreen:render();
		elseif not self.sound.mouseOver then
			self.sound.buttonred:render();
		end;
	else
		self.opt.buttonred:render();
	end;


end;



function greenstar2:onLeave()
	setShowMouseCursor(false);
	self.menuActive = false;
end;