using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Banshee.Base;
using Banshee.MediaEngine;
using Banshee.Sources;
using Banshee.Configuration.Schema;
using Banshee.Plugins;
using Banshee.Winforms;
using Banshee.Base.Winforms.Widgets;
// This used to represent the GTK Treestore/Liststore model
namespace Banshee
{
    public enum RepeatMode
    {
        None,
        All,
        Single,
        ErrorHalt
    }
    public partial class PlaylistModel : Component
    {
        TrackSource source = new TrackSource();
        TrackInfo selected_track;

        public TrackInfo Selected_track
        {
            get { return selected_track; }
            set { selected_track = value; }
        }
        public TrackSource Source
        {
            get { return source; }
            set { source = value; }
        }
        private RepeatMode repeat = new RepeatMode();

        public RepeatMode Repeat
        {
            get { return repeat; }
            set { repeat = value; }
        }
        public PlaylistModel()
        {
            InitializeComponent();
        }

        public PlaylistModel(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }
        public void PlayTrack(string uri)
        {
            if (!string.IsNullOrEmpty(uri))
            {
                PlayerEngineCore.Open(new SafeUri(uri));
            }
        }
        public void PlayTrack(TrackInfo track)
        {
            if (track != null)
            {
                PlayerEngineCore.OpenPlay(track);
            }
        }
        int current_index = 0;

        public int Current_index
        {
            get { return current_index; }
            set { current_index = value; }
        }
        public void ChangeDirection(bool forward)
        {
            if (forward)
            {
                if (repeat == RepeatMode.All)
                {

                    current_index++;
                    TrackInfo track = source.Tracks[current_index];

                    selected_track = track;
                    if(current_index+1 != source.Tracks.Count)
                        PlayTrack(track);
                }
                else if (repeat == RepeatMode.Single)
                {
                    TrackInfo track = source.Tracks[current_index];
                    selected_track = track;
                    PlayTrack(track);
                }
            }
            else
            {
                if (repeat == RepeatMode.All)
                {
                    current_index--;
                    int index = current_index;
                    if (index >= 0)
                    {
                        TrackInfo track = source.Tracks[index];
                        selected_track = track;
                        PlayTrack(track);
                    }
                }
                else if (repeat == RepeatMode.Single)
                {
                    TrackInfo track = source.Tracks[current_index];
                    selected_track = track;
                    PlayTrack(track);
                }

            }
        }
    }
}
