using DualScreenDemo;
using System;
using System.IO;
using System.Windows.Navigation;
namespace DBObj
{
    public class SongList
    {
        private static bool isWelcome = true;
        public static SongData welcome;
        public static SongData close ;
        private static SongData publicPlaying=null;
        private static List<SongData> publicSong = new();
        private static SongData playing=null;
        private static List<SongData> not_played = new List<SongData>();
        private static List<SongData> played = new List<SongData>();
        public static List<SongData> PublicSong() => publicSong;

        public static SongData Current()
        {
            Console.WriteLine(not_played.Count + " Current " + playing);
            return (playing ==null) ? publicPlaying : playing;
        }
        public static SongData Next()
        {
            if (playing != null)
            {
                playing.SetState(PlayState.Played);
                played.Add(playing);
                playing = null;
            }
            if (not_played.Count <= 0)
                return NextPublicSong();
            else
                return NextUserSong();
        }
        private static SongData NextUserSong()
        {
            playing = not_played[0];
            not_played.RemoveAt(0);
            playing.SetState(PlayState.Playing);
            UpdateNextSongLabel();
            return playing;
        }
        private static SongData NextPublicSong()
        {
            if (Program.room.IsClose()) {
                publicPlaying = close;
            } else if(Program.room.IsOpen() && isWelcome){
                isWelcome = false;
                publicPlaying = welcome;
            } else {
                publicPlaying = publicSong[0];
                publicSong.RemoveAt(0);
                publicSong.Add(publicPlaying);
            }
            
            return publicPlaying;
        }

        public static List<SongData> GetHistory()
        {
            List<SongData> History = [.. played];
            if (playing != null) History.Add(playing);
            History.AddRange(not_played);
            return History;
        }

        public static void Add(SongData song)
        {
            not_played.Add(new SongData(song,PlayState.NotPlayed));
            // PrimaryForm.Instance.AddSongCount(songData.Number);
            chkCut();
        }
        public static void Insert(SongData song)
        {
            not_played.Insert(0, new SongData(song,PlayState.InsertPlayback));
            chkCut();
        }
        private static void chkCut()
        {
            if (playing == null)
            {
                if (PrimaryForm.Instance.InvokeRequired)
                    PrimaryForm.Instance.Invoke(new System.Action(() => PrimaryForm.Instance.videoPlayerForm.PlayNextSong()));
                else
                    PrimaryForm.Instance.videoPlayerForm.PlayNextSong();
            } else {
                UpdateNextSongLabel();
            }
        }
        
        public static void UpdateNextSongLabel()
        {
            VideoPlayerForm.overlayForm.UpdateTopLeftLabel(
                (not_played.Count > 0) ? not_played[0].next_song_text() : "目前沒有下一首，請踴躍點歌!!!"
            );
        }
        public static void clearSong()
        {
            
            isWelcome = true;
            not_played.Clear();
            played.Clear();
        }
    }
}
