using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace MiniBar2k.Common
{
    class FoobarPlaylistItem
    {
        private FoobarController controller = null;
        public int PlaylistIndex = 0;
        public int TrackIndex = 0;
        public int QueueIndex = -1;

        public FoobarPlaylistItem(FoobarController fbc)
        {
            controller = fbc;
        }

        public FoobarPlaylistItem Become(int playlistindex, int trackindex)
        {
            if (playlistindex == -1)
            {
                fbcBaseEntry entry = controller.Manager.Queue(false)[trackindex - 1];
                PlaylistIndex = entry.PlaylistIndex;
                TrackIndex = entry.TrackIndex;
                QueueIndex = trackindex;
            }
            else
            {
                PlaylistIndex = playlistindex;
                TrackIndex = trackindex;
                QueueIndex = -1;
            }

            return this;
        }

        public String Title 
        {
            get
            {
                if (controller != null)
                {
                    try
                    {
                        if (QueueIndex == -1) { controller.Manager.PrecacheEntries(PlaylistIndex, TrackIndex); }
                        return controller.FormatTitle(PlaylistIndex, TrackIndex, (QueueIndex == -1));
                    }
                    catch
                    {
                        return "<error>";
                    }
                }
                else
                {
                    return "<error>";
                }
            }
        }
    }

    class FoobarPlaylist : IList
    {
        private FoobarController controller = null;
        private FoobarPlaylistItem item = null;
        private int playlist = 0;
        private int lastcount = 0;

        public int lastplaylist = 0;
        public int lasttrack = 0;

        public FoobarPlaylist(FoobarController fbc)
        {
            controller = fbc;
            item = new FoobarPlaylistItem(controller);
            playlist = 0;
        }

        // -1 = Queue
        public void Become(int playlistindex) {
            playlist = playlistindex;

            if (playlist == -1)
            {
                if (controller.isConnected())
                {
                    controller.Manager.Queue(true);
                }
            }
        }

        private Object GetItem(int index) 
        { 
            lastplaylist = playlist;
            lasttrack = index + 1;
            return item.Become(lastplaylist, lasttrack); 
        }
        
        private int GetCount() 
        {
            if (!controller.isConnected())
            {
                return lastcount;
            }

            if (playlist == -1) 
            {
                lastcount = controller.Manager.Queue(false).Length;
                return lastcount;
            } 
            else 
            {
                try
                {
                    lastcount = controller.Manager.Playlists()[playlist].Entries;
                    return lastcount;
                }
                catch
                {
                    return lastcount;
                }
            }
        }

        // IEnumerable
        public IEnumerator GetEnumerator() { return null; }

        // ICollection
        public int Count { get { return GetCount(); } }
        public bool IsSynchronized { get { return false; } }
        public Object SyncRoot { get { return this; } }

        public void CopyTo(Array array, int index) { }

        // IList
        public Object this[int index] { get { return GetItem(index); } set { throw new NotSupportedException(); } }
        public bool IsFixedSize { get { return true; } }
        public bool IsReadOnly { get { return true; } }

        public int Add(Object obj) { throw new NotSupportedException(); }
        public void Clear() { throw new NotSupportedException(); }
        public bool Contains(Object obj) { return false; }
        public int IndexOf(Object obj) { return -1; }
        public void Insert(int index, Object obj) { throw new NotSupportedException(); }
        public void Remove(Object obj) { throw new NotSupportedException(); }
        public void RemoveAt(int index) { throw new NotSupportedException(); }
    }
}
