local engineVolume = 1.0
local carSounds1 = ui.MediaPlayer()
local carSounds2 = ui.MediaPlayer()
local carSounds3 = ui.MediaPlayer()
local carSounds4 = ui.MediaPlayer()
local carSounds5 = ui.MediaPlayer()
local carSounds6 = ui.MediaPlayer()
local carSounds7 = ui.MediaPlayer()
local carSounds8 = ui.MediaPlayer()
local carSounds9 = ui.MediaPlayer()
local carSounds10 = ui.MediaPlayer()
local carSounds11 = ui.MediaPlayer()

local carSounds12 = ui.MediaPlayer()
carSounds12:setSource('./sound/992TURBOSSTARTUP.wav')
carSounds12:setLooping(false)
carSounds12:setVolume(ac.getAudioVolume('main') * 2.0)
local flag = 0

local carSounds13 = ui.MediaPlayer()
carSounds13:setSource('./sound/992INTSTARTUP.wav')
carSounds13:setLooping(false)
carSounds13:setVolume(ac.getAudioVolume('main') * 2.0)


local car = ac.getCar()
local extraF = car.extraF
local isDoorLocked = false
local carstall = true
local carstallstate = carstall
local extraB = car.extraB
local extraD = car.extraD
local isLaunchControlActive = false
local valvedState = 1.0
local incrementSpeed = 0.9
local interiorstate = 0.0

function script.update(dt)

	if car.extraC then
		valvedState = math.min(valvedState + incrementSpeed * dt, 0.0)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineExt, 'valved', valvedState)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Turbo, 'valved', valvedState)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineInt, 'valved', valvedState)
		
	end

	if not car.extraC then
		valvedState = math.max(valvedState - incrementSpeed * dt, 1.0)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineExt, 'valved', valvedState)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Turbo, 'valved', valvedState)
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.EngineInt, 'valved', valvedState)
		
	end

	if ac.isInteriorView() then
		interiorstate = 1
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Turbo, 'interior', interiorstate)
	end

	if not ac.isInteriorView() then
		interiorstate = 0
		ac.CarAudioTweak.setParameter(ac.CarAudioEventID.Turbo, 'interior', interiorstate)
	end

	--turn_signals √
	if ((ac.getCar().turningLeftLights == true or ac.getCar().turningRightLights == true) and ac.isInteriorView() == true) then
		carSounds1:setSource('./sound/turn_signals.wav'):setAutoPlay(true)
		carSounds1:setVolume(ac.getAudioVolume('main') * 2)
		carSounds1:play()
	else
		carSounds1:pause()
		carSounds1:setCurrentTime(0)
	end

	--suspension adjustment x	

	if (car.extraF ~= extraF) then
        carSounds5:pause()
        carSounds5:setCurrentTime(0)
		carSounds5:setSource('./sound/mech2.mp3'):setAutoPlay(true)
		carSounds5:setVolume(ac.getAudioVolume('main') * 1)
		carSounds5:play()
		carSounds5:setLooping(false)
		extraF = car.extraF
	end

	ac.debug('car_extraF',car.extraF)
    ac.debug('extraF',extraF)

	--door locking x	


	if ((ac.getCar().speedKmh > 20) and ac.isInteriorView() == true and isDoorLocked == false and ac.getCar().isInPit == false) then
		carSounds3:pause()
		carSounds3:setCurrentTime(0)
		carSounds3:setSource('./sound/doorlocked.mp3'):setAutoPlay(true)
		carSounds3:setVolume(ac.getAudioVolume('main') * 2)
		carSounds3:play()
    	carSounds3:setLooping(false)
		isDoorLocked = true
	end

	if ac.getCar().isInPit == true then
		isDoorLocked = false
		carSounds3:pause()
		carSounds3:setCurrentTime(0)
	end
	ac.debug('isDoorLocked',isDoorLocked)
	ac.debug('isInteriorView',ac.isInteriorView())
	ac.debug('isInPit',ac.getCar().isInPit)

	--lights level


	--engine start

  if car.extraD and flag == 0 and not ac.isInteriorView() then
      carSounds12:play()
      flag = 1
	 
  end

  if not car.extraD then
    flag = 0
  end

  if car.extraD and flag == 0 and ac.isInteriorView() then
	carSounds13:play()
	flag = 1
  end

    --door chime
    if ((ac.getCar().extraA == true) and ac.getCar().headlightsActive == true) and ac.isInteriorView() then
        -- if(carSounds2:playing() ~= true) then
        carSounds2:setSource('./sound/chime.mp3'):setAutoPlay(true)
        carSounds2:setVolume(ac.getAudioVolume('main') * 0.3)
        carSounds2:play()
        -- carSounds2:setLooping(true)
        -- end
    else
        carSounds2:pause()
        carSounds2:setCurrentTime(0)
    end

	if (car.extraA == false) then
        carSounds10:pause()
        carSounds10:setCurrentTime(0)
		carSounds10:setSource('./sound/warning.mp3'):setAutoPlay(true)
		carSounds10:setVolume(ac.getAudioVolume('main') * 0)
		carSounds10:play()
		carSounds10:setLooping(false)
		extraA = car.extraA
	end

	ac.debug('car_extraA',car.extraA)
    ac.debug('extraA',extraA)


    --GOD	

	if (car.extraC ~= extraC) then
        carSounds2:pause()
        carSounds2:setCurrentTime(0)
		carSounds2:setSource('./sound/mech.mp3'):setAutoPlay(true)
		carSounds2:setVolume(ac.getAudioVolume('main') * 0.8)
		carSounds2:play()
		carSounds2:setLooping(false)
		extraC = car.extraC
	end

	ac.debug('car_extraC',car.extraC)
    ac.debug('extraC',extraB)

	--BOOT	

	if (car.extraA == false) then
        carSounds6:pause()
        carSounds6:setCurrentTime(0)
		carSounds6:setSource('./sound/BOOT.mp3'):setAutoPlay(true)
		carSounds6:setVolume(ac.getAudioVolume('main') * 1.5)
		carSounds6:play()
		carSounds6:setLooping(false)
		extraA = car.extraA
	end
	if (car.extraA == true) then
        carSounds7:pause()
        carSounds7:setCurrentTime(0)
		carSounds7:setSource('./sound/BOOT2.mp3'):setAutoPlay(true)
		carSounds7:setVolume(ac.getAudioVolume('main') * 2)
		carSounds7:play()
		carSounds7:setLooping(false)
		extraA = car.extraA
	end

    --Launch Control	

	ac.debug('kersButtonPressed',car.kersButtonPressed)

	if ((ac.getCar().gas == 1) and (ac.getCar().kersButtonPressed == true and (ac.getCar().rpm >= 5295)) and ac.isInteriorView() == true) then
		carSounds8:pause()
		carSounds8:setCurrentTime(0)
		carSounds8:setSource('./sound/launch_control.mp3'):setAutoPlay(true)
		carSounds8:setVolume(ac.getAudioVolume('main') * 2)
		carSounds8:play()
		carSounds8:setLooping(false)
	end


	ac.debug('car_extraC',car.extraC)
    ac.debug('extraC',extraB)
	ac.debug('car_extraD',car.extraA)
    ac.debug('extraA',extraA)

end