if PLANE_ICAO == "A320" then
  
  --Включение плагина происходит при открытии двери 1L и перевода переключателя Застегните ремни в положение Auto или On
  
  local company_icao = "SDM" --Rossiya Airlines

  local auto = 1 -- Способ обработки некоторых триггеров: 1 - Отрабатываются автоматически, 0 - перед взлетом и при посадке воспроизводятся после только чаймов 
  
  local volume = 4 --Начальная громкость (от 1 до 5)
  
  --Множители уровня громкости
  local view_int_gain = 1 --Вид внутри кабины
  local view_ext_gain = 0.3 --Внешний вид
  
  local engine_on_gain = 0.6 --Двигатели включены
  local engine_off_gain = 0.5 --Двигатели выключены
  
  local on_air_gain = 0.8 --В воздухе
  local on_ground_gain = 0.5 --На земле
  
  local in_cockpit_gain = 0.9 --В кабине
  local in_cabin_gain = 1.8 --В салоне
  
  add_macro("Passenger Announcements A320FF: Disable for this flight", "disable_pa()")
  add_macro("Passenger Announcements A320FF: Volume Down", "voldn()") 
  add_macro("Passenger Announcements A320FF: Volume Up", "volup()") 

  create_command("FlyWithLua/PA_A320FF/volup", "Passenger Announcements A320FF: Volume Up", "", "", "volup()") 
  create_command("FlyWithLua/PA_A320FF/voldn", "Passenger Announcements A320FF: Volume Down","", "","voldn()")

  local pa_welcome = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_welcome.wav") --Приветствие пассажиров
  local pa_doors = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_doors.wav") --Двери, приветствие СБ
  local pa_safety = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_safety.wav") --Безопасность
  local pa_takeoff = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_takeoff.wav") --Взлет
  local pa_climb = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_climb.wav") --Набор
  local pa_cruise = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_cruise.wav") --Эшелон
  local pa_turb = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_turb.wav") --Турбулентность
  local pa_descent = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_descent.wav") --Снижение
  local pa_approach = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_approach.wav") --Завершение обслуживания
  local pa_landing = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_landing.wav") --Посадка
  local pa_destination = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/pa_destination.wav") --Прибытие
  local ps_applause = load_WAV_file(SCRIPT_DIRECTORY .. "PA_A320FF/" .. company_icao .. "/ps_applause.wav") --Аплодисменты

  local disable = 1
 
  local pa_welcome_played = 0
  local pa_welcome_count = 0
  local pa_doors_played = 0
  local pa_safety_played = 0
  local pa_takeoff_played = 0
  local pa_climb_played = 0
  local pa_cruise_played = 0
  local pa_turb_played = 0
  local pa_descent_played = 0
  local pa_approach_played = 0
  local pa_landing_played = 0
  local pa_destination_played = 0
  local ps_applause_played = 0
  local gear_up = 0

  local door_open = 0
  local airborn = 0

  local chime = 0
  local off_time = 0
  local on_time = 0
  local second_click = 0

  local result_volume = 0

  local in_air_fpm = 9999

  function volup() 
    if volume == 5 then
    elseif volume ~= 5 then
      volume = volume + 1
    end
  end 

  function voldn()
    if volume == 1 then
    elseif volume ~= 1 then
      volume = volume - 1
    end
  end 
    
  function disable_pa()
    disable = 1
  end 

  dataref("seatbelt_sign_pos", "sim/cockpit/switches/fasten_seat_belts") --положение тумблера пристегните ремни
  dataref("door_pos", "sim/cockpit2/switches/door_open","readonly",1) --положение левой правой двери
  dataref("engine1_running", "sim/flightmodel/engine/ENGN_running",0) --первый двигатель
  dataref("engine2_running", "sim/flightmodel/engine/ENGN_running",1) --второй двигатель

  dataref("g_force", "sim/flightmodel/forces/g_nrml") --сила тяжести
  dataref("flaps", "sim/flightmodel2/controls/flap_handle_deploy_ratio") --0,0.25,0.5,0.75,1
  dataref("gnd_stat", "sim/flightmodel/forces/faxil_gear") --статус - на земле
  dataref("alt", "sim/cockpit2/gauges/indicators/altitude_ft_pilot") --высота в футах
  dataref("viewext", "sim/graphics/view/view_is_external") --внешний вид
  dataref("paused", "sim/time/paused") --пауза
  dataref("prb", "sim/cockpit2/controls/parking_brake_ratio") --стояночный тормоз 
  dataref("turb", "sim/weather/turbulence[0]") --турбулентность
  dataref("nosespd", "sim/flightmodel/misc/nosewheel_speed") --скорость носового колеса (не используется)
  dataref("spd", "sim/flightmodel/position/indicated_airspeed") --воздушная скорость
  dataref("fpm", "sim/flightmodel/position/vh_ind_fpm") --вертикальная скорость
  
  dataref("camera_z_position", "sim/aircraft/view/acf_peZ") --позиция камеры
  
  dataref("strob_lights","sim/cockpit/electrical/strobe_lights_on") --Strob Lights
  dataref("beacon_lights","sim/cockpit/electrical/beacon_lights_on") --Beacon Lights (не используется)
  dataref("landing_lights","sim/cockpit/electrical/landing_lights_on") --Landing Lights (не используется)
  dataref("taxi_lights","sim/cockpit2/switches/taxi_light_on") --Taxi Lights (не используется)
  dataref("nav_lights","sim/cockpit/electrical/nav_lights_on") --Nav Lights (не используется) 
  
  dataref("gear","sim/cockpit2/controls/gear_handle_down") --шасси
  
  local stblt_chg_pos = 0
  local stblt_last_pos = seatbelt_sign_pos

  --Настройка громкости
  function setvol()
    if disable == 0 then
    
      result_volume = volume / 5
      
      --Множитель для внешнего вида
      if viewext == 0 then
        result_volume = result_volume * view_int_gain
      else
        result_volume = result_volume * view_ext_gain
      end
      
      --Множитель при включении двигателей
      if engine1_running == 1 or engine2_running == 1 then 
        result_volume = result_volume * engine_on_gain
      else
        result_volume = result_volume * engine_off_gain
      end
      
      --Множитель при полете
      if gnd_stat == 0 then 
        result_volume = result_volume * on_air_gain
      else
        result_volume = result_volume * on_ground_gain
      end
      
      --Множитель кабина/салон
      if camera_z_position < -12.3 then
        result_volume = result_volume * in_cockpit_gain
      else
        result_volume = result_volume * in_cabin_gain
      end

      if result_volume > 1 then result_volume = 1 end
      if result_volume < 0.1 then result_volume = 0.1 end
        
      set_sound_gain(pa_welcome, result_volume)
      set_sound_gain(pa_doors, result_volume) 
      set_sound_gain(pa_safety, result_volume) 
      set_sound_gain(pa_takeoff, result_volume) 
      set_sound_gain(pa_climb, result_volume) 
      set_sound_gain(pa_cruise, result_volume) 
      set_sound_gain(pa_turb, result_volume) 
      set_sound_gain(pa_descent, result_volume) 
      set_sound_gain(pa_approach, result_volume * 0.9) 
      set_sound_gain(pa_landing, result_volume * 0.9) 
      set_sound_gain(pa_destination, result_volume) 
      set_sound_gain(ps_applause, 1) 
    end
  end  
      
  local welcome_time = os.clock()
  
     
  function onesecondlogic() --раз в секунду
      
    --Включение плагина
    if seatbelt_sign_pos > 0 and door_pos == 0 then
      disable = 0
    end
    
    if disable == 0 then
      
      --airborn = 1 --Для тестов!
      --door_open = 1  --Для тестов!
      
      setvol()
      
      --Проверка открытия двери 1L
      if door_pos == 0 then
        door_open = 1
      end
      
      --Проверка уборки шасси
      if gear == 0 and gear_up == 0 then
        gear_up = 1
      end
      
      --Сброс проигрывания Посадка при уборке шасси и закрылок (уход на второй круг)
      if gear == 0 and flaps == 0 and gnd_stat == 0 and airborn == 1 then
         pa_landing_played = 0
      end 
      
      --Повтор проигрывания pa_welcome через 2 минуты (не более 2 раз)
      if os.clock() > welcome_time + 180 and pa_welcome_played == 1 then 
        pa_welcome_played = 0
      end
    
      --Проверка взлета
      if gnd_stat == 0 and fpm > 350 and alt > 300 and airborn == 0 then
        airborn = 1
        stblt_chg_pos = 0
        chime = 0
      end
      
      --Сброс после посадки и открытии двери
      if airborn == 1 and door_pos == 0 then
        airborn = 0
        gear_up = 0
        pa_welcome_played = 0
        pa_welcome_count = 0        
        pa_doors_played = 0
        pa_safety_played = 0
        pa_takeoff_played = 0
        pa_climb_played = 0
        pa_cruise_played = 0
        pa_turb_played = 0
        pa_descent_played = 0
        pa_approach_played = 0
        pa_landing_played = 0
        pa_destination_played = 0
        ps_applause_played = 0
        stblt_chg_pos = 0
        chime = 0
      end
      
      --Приветствие
      if seatbelt_sign_pos == 2 and door_pos == 0 and gnd_stat ~= 0 and paused == 0 and pa_welcome_played == 0 and pa_welcome_count < 2 then
        play_sound(pa_welcome)
        pa_welcome_played = 1
        pa_welcome_count = pa_welcome_count + 1
        welcome_time = os.clock()
        stblt_chg_pos = 0
      end
       
      --Двери
      if door_open == 1 and door_pos == 1 and gnd_stat ~= 0 and paused == 0 and pa_doors_played == 0 then
        stop_sound (pa_welcome)
        play_sound (pa_doors)
        pa_welcome_played = 1
        pa_doors_played = 1
        pa_welcome_count = 2
      end
       
      --Взлет
      if (chime == 1 or strob_lights == 1 and auto == 1) and flaps > 0 and spd < 40 and gnd_stat ~= 0 and paused == 0 and pa_takeoff_played == 0 then
        stop_sound (pa_welcome)
        stop_sound (pa_doors)
        stop_sound (pa_safety)
        play_sound(pa_takeoff)
        pa_welcome_played = 1
        pa_doors_played = 1
        pa_safety_played = 1
        pa_takeoff_played = 1
        chime = 0
      end
      
      --Посадка
      if (chime == 1 or auto == 1 and flaps > 0.5 and fpm < -350 and alt < 6000) and gear == 1 and gear_up == 1 and spd > 100 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_landing_played == 0 then
        stop_sound(pa_descent)
        stop_sound(pa_approach)
        play_sound(pa_landing)
        pa_descent_played = 1
        pa_approach_played = 1
        pa_landing_played = 1
        chime = 0
      end
    
      --Сброс проигрывания турбулентности
      if seatbelt_sign_pos == 0 then
        pa_turb_played = 0 
      end
      
    end
  end

  function tensecondlogic() --раз в десять секунд
    if disable == 0 then
      
      --Демонстрация
      if door_open == 1 and door_pos == 1 and (engine1_running == 1 or engine2_running == 1 or prb == 0) and gnd_stat ~= 0 and paused == 0 and pa_safety_played == 0 then
        stop_sound (pa_welcome)
        stop_sound (pa_doors)
        play_sound (pa_safety)
        pa_welcome_played = 1
        pa_doors_played = 1
        pa_safety_played = 1
        pa_welcome_count = 2
      end
      
      --Набор
      if (chime == 1 or alt > 12000 and auto == 1 and fpm > 350) and spd > 220 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_climb_played == 0 then
        play_sound(pa_climb)
        pa_climb_played = 1
        chime = 0
        stblt_chg_pos = 0
      end

      --Эшелон 
      if stblt_chg_pos == 1 and seatbelt_sign_pos == 0 and chime == 0 then
        if alt > 10000 and spd > 220 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_cruise_played == 0 then 
          stop_sound(pa_turb)
          stop_sound(pa_climb)
          play_sound(pa_cruise) 
          pa_climb_played = 1 
          pa_cruise_played = 1 
          stblt_chg_pos = 0
          chime = 0
        end
      end
      
      --Турбулентность
      if stblt_chg_pos == 1 and seatbelt_sign_pos > 0 then
        if turb > 2 and fpm < 350 and fpm > -350 and alt > 10000 and spd > 220 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_turb_played == 0 then
          stop_sound(pa_climb)
          stop_sound(pa_cruise)
          play_sound(pa_turb)
          pa_turb_played = 1
          stblt_chg_pos = 0
          chime = 0
        end
      end

      --Снижение
      if stblt_chg_pos == 1 and seatbelt_sign_pos > 0 and chime == 0 then
        if fpm < -350 and alt > 10000 and spd > 220 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_descent_played == 0 then
          stop_sound(pa_climb)
          stop_sound(pa_cruise)
          stop_sound(pa_turb)
          play_sound(pa_descent)
          pa_descent_played = 1
          stblt_chg_pos = 0
          chime = 0
        end
      end
    
      --Завершение обслуживания
      if seatbelt_sign_pos > 0 then
        if fpm < -350 and alt < 10000 and alt > 6000 and spd > 180 and airborn == 1 and gnd_stat == 0 and paused == 0 and pa_approach_played == 0 then
          stop_sound(pa_climb)
          stop_sound(pa_cruise)
          stop_sound(pa_turb)
          stop_sound(pa_descent)
          play_sound(pa_approach)
          pa_descent_played = 1
          pa_approach_played = 1
          stblt_chg_pos = 0
          chime = 0
        end
      end
    
      --Прибытие
      if spd < 40 and strob_lights == 0 and airborn == 1 and gnd_stat ~= 0 and paused == 0 and pa_destination_played == 0 then
        play_sound(pa_destination)
        pa_destination_played = 1
      end
      
      --Аплодисменты
      if in_air_fpm ~= 9999 and in_air_fpm ~= 0 and math.floor(in_air_fpm) > -250 and spd < 70 and airborn == 1 and gnd_stat ~= 0 and paused == 0 and ps_applause_played == 0 then
        play_sound(ps_applause)
        ps_applause_played = 1
      end 
      
    end
  end
  
  function everyframelogic() --каждый кадр
    if disable == 0 then
      
      --Определение вертикальной скорости при посадке
      if airborn == 1 and gnd_stat == 0 and fpm < 0 and spd > 10 then
        in_air_fpm = fpm
      end 
      
      --Определение смены позиции выключателя Пристегните ремни
      if stblt_last_pos ~= seatbelt_sign_pos then
        stblt_chg_pos = 1
        stblt_last_pos = seatbelt_sign_pos
      end
      
      --Определение чаймов
      if seatbelt_sign_pos == 0 then
        off_time = os.clock()
        second_click = 1
        chime = 0
      end
      
      if seatbelt_sign_pos == 2 and second_click == 0 then
        on_time = os.clock()
      end  
      
      if seatbelt_sign_pos == 2 and second_click == 1 then
        second_click = 0
        if off_time - on_time < 1 then
          chime = 1
        else
          chime = 0
        end
      end
    end
  end

  do_sometimes("tensecondlogic()") 
  do_often("onesecondlogic()") 
  do_every_frame ("everyframelogic()")
  
  print("Passenger Announcements 3.3 for FlightFactor A320 Ultimate loaded successfully") 

  local starting_time = os.clock()

  function on_screen_display() 
    local pos = 0 
    --pos = big_bubble(20, pos, result_volume) 
    --pos = big_bubble(20, pos, chime) 
  
    if os.clock() < starting_time + 10 then 
      pos = big_bubble(20, pos, "Passenger Announcements 3.3 for FlightFactor A320 Ultimate loaded successfully") 
    end 
  end 

  do_every_draw("on_screen_display()")

end

