using System.Collections.ObjectModel;
using Aurora.Models.Media;
using Aurora.Services;
using Aurora.Services.PlayerService;
using Xamarin.Forms;

namespace Aurora.Design.Views.Songs
{
    public class SongsViewModel : BaseViewModel
    {
        #region Fields
        private ObservableCollection<BaseMedia> _songsList;
        private BaseMedia _selectedSong;
        private PlayerService _player;

        #endregion Fields

        #region Constructor
        public SongsViewModel()
        {
            _player = PlayerService.Instance;
            _songsList = new ObservableCollection<BaseMedia>();
            PlayCommand = new Command(OnPlayButtonExecute, CanPlayButtonExecute);

            _player.PlaybackStateChanged += OnPlaybackStateChanged;

            Initialize();
        }

        #endregion Constructor

        #region Properties
        public ObservableCollection<BaseMedia> SongsList
        {
            get { return _songsList; }
            set { SetProperty(ref _songsList, value); }
        }

        public BaseMedia SelectedSong
        {
            get { return _selectedSong; }
            set { SetProperty(ref _selectedSong, value); }
        }

        public Command PlayCommand { get; private set; }

        #endregion Properties

        #region Methods
        public void Initialize()
        {

            SongsList = LibraryService.Instance.GetLibrary();
        }

        #endregion Methods

        #region Commmands
        public override bool CanPreviousButtonExecute()
        {
            return true;
        }
        public override void OnPreviousButtonExecute()
        {

        }

        public override bool CanPlayButtonExecute()
        {
            switch (_player.PlaybackState)
            {
                case PlaybackState.Buffering:
                    {
                        return true;
                    }
                case PlaybackState.Playing:
                    {
                        return true;
                    }
                case PlaybackState.Stopped:
                    {
                        return true;
                    }
            }
            return false;
        }

        public async override void OnPlayButtonExecute()
        {
            base.Media = _selectedSong;
            if (!_player.IsMediaLoaded(base.Media))
            {
                await _player.LoadMedia(base.Media).ConfigureAwait(true);
            }

            switch (_player.PlaybackState)
            {
                case PlaybackState.Buffering:
                    {
                        _player.Play();
                        break;
                    }
                case PlaybackState.Playing:
                    {
                        _player.Pause();
                        break;
                    }
                case PlaybackState.Stopped:
                    {
                        _player.Play();
                        break;
                    }
            }
        }

        public override bool CanNextButtonExecute()
        {
            return true;
        }

        public override void OnNextButtonExecute()
        {

        }

        #endregion Commands

        #region Events
        /// <summary>
        /// PlayerService playback state changed event handler.
        /// </summary>
        /// <param name="sender">The sending object.</param>
        /// <param name="args">Event arguments.</param>
        public void OnPlaybackStateChanged(object sender, PlaybackStateChangedEventArgs args)
        {
            OnPropertyChanged("PlayButtonText");
        }

        #endregion Events
    }
}
