using Microsoft.AspNetCore.Components;

namespace FNZCM.BlazorWasm.Helpers
{
    public class PlaylistHelper
    {
        public event Action Changed;

        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;
            Changed?.Invoke();
        }

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

        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;
            Changed?.Invoke();
        }

        public void Deleteplaylist(Guid playlistId)
        {
            localStorage.Remove($"playlist-{playlistId}");
            var pls = PlayListLoadSave;
            pls.Remove(playlistId);
            PlayListLoadSave = pls;
            Changed?.Invoke();
        }

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

        //TODO: Move order track in playlist

        public void ListMoveUp(int index)
        {
            if (index < 1) return;

            // let's say index = 3

            var pls = PlayListLoadSave;

            var re = new Dictionary<Guid, string>();

            foreach (var item in pls.Take(index - 1)) // take2  0 1
            {
                re.Add(item.Key, item.Value);
            }

            foreach (var item in pls.Skip(index).Take(1)) // skip3t1 3→2
            {
                re.Add(item.Key, item.Value);
            }

            foreach (var item in pls.Skip(index - 1).Take(1)) // skip2t1 2→3
            {
                re.Add(item.Key, item.Value);
            }

            foreach (var item in pls.Skip(index + 1)) // skip4 4 5
            {
                re.Add(item.Key, item.Value);
            }

            PlayListLoadSave = re;
            Changed?.Invoke();
        }

        public void ListMoveDown(int index)
        {
            var pls = PlayListLoadSave;
            if (index >= pls.Count - 1) return;

            // let's say index = 3 and count = 6

            var re = new Dictionary<Guid, string>();

            foreach (var item in pls.Take(index)) // take3 012→012
            {
                re.Add(item.Key, item.Value);
            }

            foreach (var item in pls.Skip(index+1).Take(1)) // skip4t1 4→3
            {
                re.Add(item.Key, item.Value);
            }

            foreach (var item in pls.Skip(index).Take(1)) // skip3t1 3→4
            {
                re.Add(item.Key, item.Value);
            }

            foreach (var item in pls.Skip(index + 2)) // skip5 5→5
            {
                re.Add(item.Key, item.Value);
            }

            PlayListLoadSave = re;
            Changed?.Invoke();
        }
    }
}
