using NAudio.Wave;

namespace Linaris;

public partial class FooterPage : ContentView
{
    WaveOutEvent outputDevice;
    AudioFileReader audioFile;
    System.Timers.Timer timer;
    bool changementManuel = true;
    bool closing = false;
    string morceauEnCours;

    public FooterPage()
	{
        InitializeComponent();
        outputDevice = new WaveOutEvent();

        // (s,a) = convention, s = sender, a = arguments, si appli fermée, on free tout
        outputDevice.PlaybackStopped += PlaybackStoppedHandler;

        /*morceauEnCours = "Resources/Musics/peaches.mp3";
        audioFile = new AudioFileReader(morceauEnCours);
        outputDevice.Init(audioFile);*/
    }

    public void PlayButton_Clicked(object sender, EventArgs e)
    {
        string url = ((FileImageSource)Play.Source).File;
        if (url == "play.png")
        {
            outputDevice?.Play();
            Play.Source = "pause.png";
            Timer_Elapsed(sender, e);
            timer = new System.Timers.Timer(1000);
            timer.Elapsed += Timer_Elapsed;
            timer.Enabled = true;
        }
        else
        {
            outputDevice?.Pause();
            Play.Source = "play.png";
        }
    }

    public void RewindButton_Clicked(Object sender, EventArgs e)
    {
        audioFile.Position = 0;
        outputDevice?.Play();
        Play.Source = "pause.png";
        Timer_Elapsed(sender, e);
        timer = new System.Timers.Timer(1000);
        timer.Elapsed += Timer_Elapsed;
        timer.Enabled = true;
    }

    public void NextButton_Clicked(Object sender, EventArgs e)
    {
        audioFile.Position = audioFile.Length;
    }

    public void StopButton_Clicked(object sender, EventArgs e)
    {
        outputDevice.Stop();
        audioFile.Position = 0;
    }

    public void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        outputDevice.Volume = (float)e.NewValue;
    }

    public void Bar_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        if (changementManuel)
        {
            double totalSeconds = audioFile.TotalTime.TotalSeconds;
            double newPosition = e.NewValue * totalSeconds;

            audioFile.CurrentTime = TimeSpan.FromSeconds(newPosition);
        }
        else
        {
            changementManuel = true;
        }
    }

    private void PlaybackStoppedHandler(object sender, StoppedEventArgs e)
    {
        if (closing)
        {
            outputDevice.Dispose();
            audioFile.Dispose();
        }
        else
        {
            Play.Dispatcher.Dispatch(() => Play.Source = "play.png");
        }
    }

    private void Timer_Elapsed(object sender, EventArgs e)
    {
        TimeSpan totalTimeSpan = audioFile.TotalTime;
        TimeSpan currentTimeSpan = audioFile.CurrentTime;

        string totalTimeFormatted = totalTimeSpan.ToString(@"hh\:mm\:ss");
        string currentTimeFormatted = currentTimeSpan.ToString(@"hh\:mm\:ss");

        changementManuel = false;

        bar.Dispatcher.Dispatch(() => bar.Value = audioFile.CurrentTime.TotalSeconds / audioFile.TotalTime.TotalSeconds);
        currentTime.Dispatcher.Dispatch(() => currentTime.Text = currentTimeFormatted);
        endTime.Dispatcher.Dispatch(() => endTime.Text = totalTimeFormatted);

    }

    private void LoadNewAudioFile(string filePath)
    {
        if (outputDevice.PlaybackState == PlaybackState.Playing)
        {
            outputDevice.Stop();
        }
        audioFile = new AudioFileReader(filePath);
        outputDevice.Init(audioFile);
    }
}