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

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

        #endregion Fields

        #region Constructor
        public SongsViewModel()
        {
            _songsList = new ObservableCollection<BaseMedia>();
            DoubleClickCommand = new Command(OnDoubleClickExecute, OnDoubleClickCanExecute);

            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 DoubleClickCommand { 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()
        {
            return true;
        }

        public override void OnPlayButtonExecute()
        {
            if (_selectedSong == null)
            {
                return;
            }
            if (base.IsPlaying())
            {
                base.ChangePlayerState(_selectedSong, Main.PlayAction.Pause);
            }
            else
            {
                base.ChangePlayerState(_selectedSong, Main.PlayAction.Play);
            }

        }

        public override bool CanNextButtonExecute()
        {
            return true;
        }

        public override void OnNextButtonExecute()
        {

        }


        public void OnDoubleClickExecute()
        {
            if (_selectedSong == null)
            {
                return;
            }

            base.ChangePlayerState(_selectedSong, Main.PlayAction.Play);
        }

        public bool OnDoubleClickCanExecute()
        {
            return true;
        }
        #endregion Commands
    }
}
