using Microsoft.Maui.Controls.PlatformConfiguration;
using MusiLib.Model;
using System.Diagnostics;

namespace MusiLib.Views;


public partial class Partition : ContentPage
{

    public Manager MyManager => (App.Current as App).MyManager;


	public Partition()
	{
		InitializeComponent();
        BindingContext = MyManager;
    }

    private void Play_Music(object sender, EventArgs e)
    {
        Button button = (Button)sender;
        if (!IPlayMusic.isMusicBeginning)
        {
            IPlayMusic.beginMusic();
            button.Text = "Pause";
        }
        else if(!IPlayMusic.isMusicPlaying)
        {
            IPlayMusic.playMusic();
            button.Text = "Pause";
        }
        else
        {
            IPlayMusic.pauseMusic();
            button.Text = "Jouer";
        }
    }

    private void Stop_Music(object sender, EventArgs e)
    {
        IPlayMusic.stopMusic();
        Button button = (Button)FindByName("play_music_button");
        button.Text = "Jouer";
    }

    protected override bool OnBackButtonPressed()
    {
        IPlayMusic.stopMusic();
        return base.OnBackButtonPressed();
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        IPlayMusic.stopMusic();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Shell.Current.Navigating += Shell_Navigating;
    }

    private void Shell_Navigating(object sender, ShellNavigatingEventArgs e)
    {
        if (e.Source == ShellNavigationSource.ShellItemChanged)
        {
            IPlayMusic.stopMusic();
        }
    }
}
