using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace RhythmBullet.Audio
{
    internal delegate void SearchListener(MusicList musicList);

    internal class MusicList
    {
        public event SearchListener SearchCompleteEvent;
        private volatile bool work;
        private List<string> list = new List<string>();
        public List<string> List
        {
            get
            {
                if (!thread.IsAlive)
                {
                    lock (list)
                    {
                        return list;
                    }
                }
                else
                {
                    return null;
                }
            }
        }
        public string path;
        private Thread thread;
        public bool Searched
        {
            get
            {
                return (thread != null && !thread.IsAlive);
            }
        }

        internal MusicList()
        {

        }

        public void StartSearch()
        {
            if (thread == null || !thread.IsAlive)
            {
                work = true;
                thread = new Thread(Search);
                thread.IsBackground = true;
                thread.Name = "Music Search Thread";
                Debug.WriteLine("Starting music file search with path: " + path, GetType().Name);
                thread.Start();
            }
            else
            {
                StopSearch();
                StartSearch();
            }
        }

        public void StopSearch()
        {
            work = false;
            thread.Join();
        }

        private void Search()
        {
            lock (list)
            {
                list.Clear();
                list.AddRange(recursiveMusicSearch(path));
            }
            OnSearchComplete();
            Debug.WriteLine("Completed search. Found " + list.Count + " valid file(s).", GetType().Name);
        }

        private List<string> recursiveMusicSearch(string path)
        {
            List<string> musicFiles = new List<string>();
            string[] folders = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);

            for (int folderID = 0; folderID < folders.Length && work; folderID++)
            {
                musicFiles.AddRange(recursiveMusicSearch(folders[folderID]));
            }

            for (int fileID = 0; fileID < files.Length && work; fileID++)
            {
                string extensionType = Path.GetExtension(files[fileID]).ToUpper().Substring(1);
                SupportedFormats format;
                if (Enum.TryParse<SupportedFormats>(extensionType, out format))
                {
                    musicFiles.Add(files[fileID]);
                }
            }
            return musicFiles;
        }

        private void OnSearchComplete()
        {
            SearchCompleteEvent?.Invoke(this);
        }
    }
}
