using System;
using System.Collections;
using System.Collections.Generic;
using ScriptableObjectArchitecture;
using UnityEngine;

public class Scenario1 : MonoBehaviour
{
    [Header("Triggers")]

    [Header("Events")]
    [SerializeField] private GameEvent _startScenario1Event = default;
    [SerializeField] private GameEvent _p1_knobUpdateEvent = default;
    [SerializeField] private GameEvent _p1_switchUpdateEvent = default;
    [SerializeField] private GameEvent _p2_sliderKnobUpdateEvent = default;

    [Header("Audio Clips")]
    [SerializeField] private AudioClip _openingClip = default;
    [SerializeField] private AudioClip _OF_1 = default;
    [SerializeField] private AudioClip _PL_hello = default;
    [SerializeField] private AudioClip _OF_2 = default;
    [SerializeField] private AudioClip _OF_3 = default;

    [Header("Refs")]
    [SerializeField] private AudioSource _primaryAudio = default;
    [SerializeField] private CameraControls _cameraControls = default;

    [Header("Panel1 Values")]
    [SerializeField] private IntVariable _p1_knobValue = default;
    [SerializeField] private BoolVariable _p1_switchValue = default;

    [Header("Panel2 Values")]
    [SerializeField] private IntVariable _p2_slider1Value = default;
    [SerializeField] private IntVariable _p2_slider2Value = default;
    [SerializeField] private IntVariable _p2_slider3Value = default;
    [SerializeField] private IntVariable _p2_knob1Value = default;
    [SerializeField] private IntVariable _p2_knob2Value = default;
    [SerializeField] private IntVariable _p2_knob3Value = default;
    [SerializeField] private IntVariable _p2_guage1Value = default;
    [SerializeField] private IntVariable _p2_guage2Value = default;
    [SerializeField] private IntVariable _p2_guage3Value = default;
    [SerializeField] private IntVariable _p2_guage4Value = default;

    private void OnEnable()
    {
        _startScenario1Event.AddListener(RunOpeningDialogue);
    }

    private void OnDestroy()
    {
        // Always remove all listeners on disable, regardless of whether they have been added or not.
        RemoveAllEventListeners();
    }

    private void RemoveAllEventListeners()
    {
        _startScenario1Event.RemoveListener(RunOpeningDialogue);
    }

    private void RunOpeningDialogue()
    {
        StartCoroutine(PlaySoundFile(_openingClip, FinishOpeningDialogue));
    }

    private void FinishOpeningDialogue()
    {
        StartCoroutine(PlaySoundFile(_OF_1, Finish_OF_1));
    }

    private void Finish_OF_1()
    {
        StartCoroutine(PlaySoundFile(_PL_hello, Finish_PL_hello));
    }

    private void Finish_PL_hello()
    {
        StartCoroutine(PlaySoundFile(_OF_2, Finish_OF_2));
    }

    private void Finish_OF_2()
    {
        _cameraControls.ChangeCamera(2);
        StartCoroutine(PlaySoundFile(_OF_3, Finish_OF_3));
    }

    private void Finish_OF_3()
    {
        _cameraControls.ChangeCamera(2);
    }

    private IEnumerator PlaySoundFile(AudioClip soundFile, Action action)
    {
        _primaryAudio.clip = soundFile;
        _primaryAudio.Play();
        yield return new WaitForSeconds(soundFile.length + .5f);
        action();
    }
}
