using Plugin.Maui.Audio;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;


namespace MusiLib.Model
{
    public class Metronome
    {
        public AudioPlayer player;
        public bool isMusicPlaying;
        public bool isMusicBeginning;

        public Metronome()
        {
            player = DependencyService.Get<AudioPlayer>();
            isMusicPlaying = false;
        }

        public async Task Lancer(string cheminFichier)
        {
                var fichierAudio = (AudioPlayer)AudioManager.Current.CreatePlayer(await FileSystem.OpenAppPackageFileAsync(cheminFichier));
                player = fichierAudio;
                player.Play();
                isMusicPlaying=true;
                isMusicBeginning =true;
        }
        public void PlayMusic()
        {
            player.Play();
            isMusicPlaying = true;
        }
        public void PauseMusic()
        {
            player.Pause();
            isMusicPlaying = false;
        }
        public void StopMusic()
        {
            if (isMusicBeginning)
            {
                player.Stop();
                isMusicPlaying = false;
                isMusicBeginning = false;
            }
        }
        public void ReglerTempo(float tempo)
        {
            if(!isMusicBeginning)
            {
                return;
            }
            if(!isMusicPlaying)
            {
                player.Speed = tempo;
                player.Pause();
                return;
            }
            player.Speed = tempo;
        }
    }

}

