--
-- Balotiera Romaneasca
--
--
-- Script Specializat Pentru Balotiera Romaneasca
--
-- @Autor: Mihai94
-- @Data: 02/08/2015
-- Versiunea: 1.0
-- 
--
-- A Nu Se Modifica Fara Permisiunea Mea...
--

BalotieraR = {};

function BalotieraR.prerequisitesPresent(specializations)
return true;
end;

function BalotieraR:load(xmlFile)
	local CapacBaloti = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.CapacBaloti#index"));
	if CapacBaloti ~= nil then
		self.CapacBaloti = CapacBaloti;
	end;
	
	local CapacBerbec = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.CapacBerbec#index"));
	if CapacBerbec ~= nil then
		self.CapacBerbec = CapacBerbec;
	end;

	local CapacFurca = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.CapacFurca#index"));
	if CapacFurca ~= nil then
		self.CapacFurca = CapacFurca;
	end;
	
	local ManetaMasa = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.ManetaMasa#index"));
	if ManetaMasa ~= nil then
		self.ManetaMasa = ManetaMasa;
	end;
	
	local CapacLegare = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.CapacLegare#index"));
	if CapacLegare ~= nil then
		self.CapacLegare = CapacLegare;
	end;
	
	local ManivelaPicior = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.ManivelaPicior#index"));
	if ManivelaPicior ~= nil then
		self.ManivelaPicior = ManivelaPicior;
	end;
	
	local CapacMare = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.CapacMare#index"));
	if CapacMare ~= nil then
		self.CapacMare = CapacMare;
	end;
	
	local Protap = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.Protap#index"));
	if Protap ~= nil then
		self.Protap = Protap;
	end;
	  	
	self.RoataMasa = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.RoataMasa#index"));
	
	self.ropePanelAnimation = getXMLString(xmlFile, "vehicle.ropePanel#animationName");
	self.setRopePanel = SpecializationUtil.callSpecializationsFunction("setRopePanel");

	self.File = Utils.getFilename("Texturi/Contor.dds", self.baseDirectory);
	self.hudPoxX = 0.9095;
    self.hudPoxY = 0.86; 
    self.hudWidth = 0.13325;
    self.hudHeight = 0.0513825;
    self.HUDSipma = Overlay:new("HUDSipma", self.File, self.hudPoxX, self.hudPoxY, self.hudWidth, self.hudHeight);
    self.HUD = false;
	self.TotalBalesCount = 0;
	self.lastBaleCount = 0;
	
	
	openSoundFile = Utils.getFilename("Sunete/Inchide.wav", self.baseDirectory);
    self.openSoundId = createSample("LizardHor12nSound");
    loadSample(self.openSoundId, openSoundFile, false);
    self.openPlaying = false;
	
	clouseSoundFile = Utils.getFilename("Sunete/Deschide.wav", self.baseDirectory);
    self.clouseSoundId = createSample("LizardHor12nSound");
    loadSample(self.clouseSoundId, clouseSoundFile, false);
    self.clousePlaying = false;
	self.Cardan1 = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.Cardan1#index"));
end;

function BalotieraR:delete()
	if self.HUDSipma ~= nil then
		self.HUDSipma:delete();
	end
end;

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

if self.isTurnedOn then
         		rotate(self.Cardan1, 0, 0, dt*0.01 );		
	end;	

if self.sound1 then
       if not self.openPlaying then
            playSample(self.openSoundId, 1, 7, 7);
            self.openPlaying = true;
        end;    
    else
        if self.openPlaying then
            stopSample(self.openSoundId);
            self.openPlaying = false;
        end;      
    end;	
		if self.sound2 then
       if not self.clousePlaying then
            playSample(self.clouseSoundId, 1, 7, 7);
            self.clousePlaying = true;
        end;  
    else
        if self.clousePlaying then
            stopSample(self.clouseSoundId);
            self.clousePlaying = false;
        end;     
    end;

	
	if self.isTurnedOn then
        self:setAnimationTime(1, 1);
    else
        self:setAnimationTime(1, 0);
    end;
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.CapacBaloti);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.przy_klapa = true;
		else
			self.przy_klapa = false;
		end;
		
    if self.przy_klapa then
		if InputBinding.hasEvent(InputBinding.Capac_Baloti) then
			if self.animationParts[2].clipEndTime then
				self:setAnimationTime(2, self.animationParts[2].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[2].clipStartTime  then
				self:setAnimationTime(2, self.animationParts[2].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_klapa then
		if self.animationParts[2].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Deschide_Capac_Baloti")), InputBinding.Capac_Baloti);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Inchide_Capac_Baloti")), InputBinding.Capac_Baloti);
		end;
	end;	

	local nearestDistance = 1;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.CapacBerbec);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.przy_klapa1 = true;
		else
			self.przy_klapa1 = false;
		end;
		
    if self.przy_klapa1 then
		if InputBinding.hasEvent(InputBinding.Capac_Berbec) then
			if self.animationParts[4].clipEndTime then
				self:setAnimationTime(4, self.animationParts[4].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[4].clipStartTime  then
				self:setAnimationTime(4, self.animationParts[4].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_klapa1 then
		if self.animationParts[4].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Deschide_Capac_Berbec")), InputBinding.Capac_Berbec);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Inchide_Capac_Berbec")), InputBinding.Capac_Berbec);
		end;
	end;	

	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.CapacFurca);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.przy_klapa2 = true;
		else
			self.przy_klapa2 = false;
		end;
		
    if self.przy_klapa2 then
		if InputBinding.hasEvent(InputBinding.Capac_Furca) then
			if self.animationParts[5].clipEndTime then
				self:setAnimationTime(5, self.animationParts[5].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[5].clipStartTime  then
				self:setAnimationTime(5, self.animationParts[5].animDuration);
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_klapa2 then
		if self.animationParts[5].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Deschide_Capac_Furca")), InputBinding.Capac_Furca);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Inchide_Capac_Furca")), InputBinding.Capac_Furca);
		end;
	end;
	
	if self.animationParts[2].clipStartTime and self:getIsActive(false) then
		self.isTurnedOn = false;
	end;
	
	if self.animationParts[2].clipStartTime and self:getIsActive(false) then
		if Input.isKeyPressed(Input.KEY_b) then
			g_currentMission:showBlinkingWarning(g_i18n:getText("Info_BalotieraR"), 2000);
		end;
	end;
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.ManetaMasa);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.przy_podbierak = true;
		else
			self.przy_podbierak = false;
		end;
		
    if self.przy_podbierak then
		if InputBinding.hasEvent(InputBinding.Maneta_Masa) then
			if self.animationParts[3].clipEndTime then
				self:setAnimationTime(3, self.animationParts[3].offSet);
				self.isPickupLowered = false;
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[3].clipStartTime  then
				self:setAnimationTime(3, self.animationParts[3].animDuration);	
				self.isPickupLowered = true;
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_podbierak then
		if self.animationParts[3].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Lasa_Masa_Jos")), InputBinding.Maneta_Masa);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Ridica_Masa_Sus")), InputBinding.Maneta_Masa);
		end;
	end;

	if self:getIsActive() then
		if self.animationParts[3].clipEndTime then
			rotate(self.RoataMasa, 2.5 * self.lastSpeedReal * self.movingDirection * dt ,0,0);
		end;
	end;
	
	if self.attacherVehicle ~= nil then
		self:setAnimationTime(6, self.animationParts[6].animDuration);
	else
		self:setAnimationTime(6, self.animationParts[6].startPosition);
	end;
	
	if self.przy_podbierak then
		if InputBinding.hasEvent(InputBinding.Carlig_BalotieraR) then
			self:setRopePanel(not self.ropePanel);
		end;
		if self.ropePanel then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Deschide_BalotieraR"), self.typeDesc), InputBinding.Carlig_BalotieraR);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Inchide_BalotieraR"), self.typeDesc), InputBinding.Carlig_BalotieraR);
		end;
	end;
	
	if self.przy_klapa then
		if InputBinding.hasEvent(InputBinding.Agatatoare_Spate) then
			if self.animationParts[7].clipEndTime then
				self:setAnimationTime(7, self.animationParts[7].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[7].clipStartTime  then
				self:setAnimationTime(7, self.animationParts[7].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_klapa then
		if self.animationParts[7].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Intinde_Agatatoarea_Spate")), InputBinding.Agatatoare_Spate);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Strange_Agatatoarea_Spate")), InputBinding.Agatatoare_Spate);
		end;
	end;	
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.CapacLegare);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.przy_oslona = true;
		else
			self.przy_oslona = false;
		end;
		
    if self.przy_oslona then
		if InputBinding.hasEvent(InputBinding.Capac_Legare) then
			if self.animationParts[8].clipEndTime then
				self:setAnimationTime(8, self.animationParts[8].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[8].clipStartTime  then
				self:setAnimationTime(8, self.animationParts[8].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_oslona then
		if self.animationParts[8].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Deschide_Capac_Legare")), InputBinding.Capac_Legare);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Inchide_Capac_Legare")), InputBinding.Capac_Legare);
		end;
	end;
	
	local nearestDistance = 1.0;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.ManivelaPicior);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.Des_ManivelaPicior = true;
		else
			self.Des_ManivelaPicior = false;
		end;
		
	if self.Des_ManivelaPicior then
		if InputBinding.hasEvent(InputBinding.Manivela_Picior) then
			if self.animationParts[12].clipEndTime then
				self:setAnimationTime(12, self.animationParts[12].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[12].clipStartTime  then
				self:setAnimationTime(12, self.animationParts[12].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.Des_ManivelaPicior then 
		if self.animationParts[12].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Manivela_Ridicata")), InputBinding.Manivela_Picior);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Manivela_Lasata")), InputBinding.Manivela_Picior);
		end;
	end;
	
	local nearestDistance = 2.2;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.CapacMare);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.Pic_CapacMare = true;
		else
			self.Pic_CapacMare = false;
		end;
		
	if self.Pic_CapacMare then
		if InputBinding.hasEvent(InputBinding.Capac_Mare) then
			if self.animationParts[13].clipEndTime then
				self:setAnimationTime(13, self.animationParts[13].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[13].clipStartTime  then
				self:setAnimationTime(13, self.animationParts[13].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.Pic_CapacMare then
		if self.animationParts[13].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Inchide_Capac_Mare")), InputBinding.Capac_Mare);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Deschide_Capac_Mare")), InputBinding.Capac_Mare);
		end;
	end;
	
	local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.Protap);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.Pic_Protap = true;
		else
			self.Pic_Protap = false;
		end;
		
	if self.Pic_Protap then
		if InputBinding.hasEvent(InputBinding.Protap_Fata) then
			if self.animationParts[14].clipEndTime then
				self:setAnimationTime(14, self.animationParts[14].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[14].clipStartTime  then
				self:setAnimationTime(14, self.animationParts[14].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.Pic_Protap then
		if self.animationParts[14].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Strange_Protapul")), InputBinding.Protap_Fata);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Intinde_Protapul")), InputBinding.Protap_Fata);
		end;
	end;

	if self:getIsActiveForInput() then
		if InputBinding.hasEvent(InputBinding.Afisare_Contor) then
			self.HUD = not self.HUD;
		end;
	end;

	if table.getn(self.bales) > self.lastBaleCount then
		self.TotalBalesCount = self.TotalBalesCount + 1;
		self.lastBaleCount = self.lastBaleCount + 1;
	elseif table.getn(self.bales) < self.lastBaleCount then
		self.lastBaleCount = self.lastBaleCount - 1;
	end;
	
	for k, bale in pairs(self.bales) do
		if bale.time > 0.5 then
			local sendTime = math.min(1, bale.time+dt/5000);
			self.setBaleTime(self, k, sendTime, true);
			done = false;
		end;
	end;
	
	if self.TotalBalesCount >= 250 then
		self.isTurnedOn = false;
		if self.animationParts[10].clipStartTime  then
			self:setAnimationTime(10, self.animationParts[10].animDuration);	
		end;
		if self:getIsActive() then
			g_currentMission:showBlinkingWarning(g_i18n:getText("Info_Role_Sarma"));
		end;
		if self.przy_klapa2 then
			if self.animationParts[5].clipEndTime then
				if Input.isKeyPressed(Input.KEY_o) then
					if self.animationParts[10].clipEndTime then
						self:setAnimationTime(10, self.animationParts[10].offSet);
					end;
					self.TotalBalesCount = 0;
					g_currentMission:addSharedMoney(-50);
				end;
			end;
		end;
	end;
	
	if self.przy_klapa2 then
		if self.animationParts[5].clipEndTime then
			if self.TotalBalesCount >= 250 then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Role_Sarma"), self.typeDesc), InputBinding.Role_Sarma)
			end;
		end;
	end;
	
	if self.przy_klapa then
		if not self.isTurnedOn then
			if self.animationParts[2].clipEndTime then
				if InputBinding.hasEvent(InputBinding.Role_Sarma) then
					if self.animationParts[9].clipEndTime then
						self:setAnimationTime(9, self.animationParts[9].offSet);
					elseif self.animationParts[9].clipStartTime  then
						self:setAnimationTime(9, self.animationParts[9].animDuration);	
					end;
				end;
			end;
		end;
    end;
	
	if self.przy_klapa then
		if self.animationParts[2].clipEndTime then
			if self.animationParts[9].clipStartTime then
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Goleste_Balotiera")), InputBinding.Role_Sarma);
			else
				g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Goleste_Balotiera")), InputBinding.Role_Sarma);
			end;
		end;
	end;	
end;

function BalotieraR:setRopePanel(isRopePanel)
	if isRopePanel then
		if self.ropePanelAnimation ~= nil and self.playAnimation ~= nil then
			self:playAnimation(self.ropePanelAnimation, -1, nil, true);
			self.ropePanel = true;
		end;
	else
		if self.ropePanelAnimation ~= nil and self.playAnimation ~= nil then
			self:playAnimation(self.ropePanelAnimation, 1, nil, true);
			self.ropePanel = false;
		end;
	end;
end;

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

function BalotieraR:validateAttacherJoint(implement, jointDesc, dt)
return true;
end;

function BalotieraR:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	if not resetVehicles then		
		local hud = getXMLBool(xmlFile, key.."#hud");
		if hud ~= nil then
			self.HUD = hud;
		end;
	end;
    return BaseMission.VEHICLE_LOAD_OK;
end;

function BalotieraR:getSaveAttributesAndNodes(nodeIdent)
	if self.HUD then
		hud = "true";
	else
		hud = "false";
	end;
	local attributes = string.format('hud="%s"', hud);
    return attributes,nodes;
end

function BalotieraR:draw()
	if self.HUD then
		self.HUDSipma:render();
		g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Dezactiveaza_Contor"), self.typeDesc), InputBinding.Afisare_Contor);
		if self.TotalBalesCount >= 230 then
			setTextBold(true);
			setTextColor(1,0,0,1);
			totalBalesCountString = string.format("%d", self.TotalBalesCount);
			renderText(0.9592, 0.8789, 0.025, string.format(totalBalesCountString));
		else
			setTextBold(true);
			totalBalesCountString = string.format("%d", self.TotalBalesCount);
			renderText(0.9592, 0.8789, 0.025, string.format(totalBalesCountString));
		end;
	else
		g_currentMission:addHelpButtonText(string.format(g_i18n:getText("Activeaza_Contor"), self.typeDesc), InputBinding.Afisare_Contor);	
	end;
end;