namespace FNZCM.BlazorWasm.Helpers
{
    public class PlaylistHelper
    {
        private readonly LocalStorageHelper localStorage;

        public PlaylistHelper(LocalStorageHelper localStorage) => this.localStorage = localStorage;

        public Dictionary<Guid, string> PlayListLoadSave { get => localStorage.Get<Dictionary<Guid, string>>() ?? new Dictionary<Guid, string>(); set => localStorage.Set(value); }

        public List<string> this[Guid playlistId]
        {
            get => localStorage.Get<List<string>>($"playlist-{playlistId}") ?? new List<string>();
            set => localStorage.Set(value, $"playlist-{playlistId}");
        }

        public void CreateNewPlaylist(string name)
        {
            var pls = PlayListLoadSave;
            pls[Guid.NewGuid()] = name;
            PlayListLoadSave = pls;
        }

        public void AddTrackToPlaylist(string trackPath, Guid playlistId)
        {
            var p = this[playlistId];
            p.Add(trackPath);
            this[playlistId] = p;
        }

        public void DeleteTrackInPlaylist(Guid playlistId, int index, string trackPathForConfirm)
        {
            var p = this[playlistId];
            if (index < p.Count && p[index] == trackPathForConfirm) p.RemoveAt(index);
            this[playlistId] = p;
        }

        public void Deleteplaylist(Guid playlistId)
        {
            localStorage.Remove($"playlist-{playlistId}");

            var pls = PlayListLoadSave;
            pls.Remove(playlistId);
            PlayListLoadSave = pls;
        }

        public void UpdatePlaylistName(Guid playlistId,string name)
        {
            var pls = PlayListLoadSave;
            pls[playlistId] = name;
            PlayListLoadSave = pls;
        }

        //TODO: Move order track in playlist

    }
}
