using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Mpeg2.Psi.Model;
using Tsubasa.IO;

namespace skyscraper5.Dvb.DataBroadcasting.SkyscraperVfs
{
    class ObjectCarouselMetadata : IFilesystem
    {
        public ObjectCarouselMetadata(ObjectCarouselEventHandler eventHandler, int sourcePid, ProgramMapping programMapping)
        {
            vfsDirectories = new List<VfsDirectory>();
            vfsFiles = new List<VfsFile>();
            this.EventHandler = eventHandler;
            this.SourcePid = sourcePid;
            this.ProgramMapping = programMapping;
            vfsEvents = new List<VfsEvent>();
        }

        public VfsDirectory rootDirectory;
        public List<VfsDirectory> vfsDirectories;
        public List<VfsFile> vfsFiles;
        public List<VfsEvent> vfsEvents;
        public ObjectCarouselEventHandler EventHandler { get; private set; }
        public int SourcePid { get; private set; }
        public ProgramMapping ProgramMapping { get; private set; }
        public int LastExtractions { get; set; }
        public int DirectoriesCreated { get; set; }

        #region yo3explorer Driver
        private string[] _wrappedFileList;
		public string[] FileList
        {
            get
            {
                if (_wrappedFileList == null)
                {
                    _wrappedFileList = vfsFiles.Select(x => x.ToString()).ToArray();
                }
                return _wrappedFileList;
            }
        }

		public string FilesystemName
        {
            get
            {
                return string.Format("DSM-CC Object Carousel;{0};", SourcePid);
            }
        }

		public bool FileExists(string filename)
		{
			if (string.IsNullOrEmpty(filename))
				return false;

            if (filename.StartsWith("/"))
                filename = filename.Replace('/', '\\');

            foreach (VfsFile file in vfsFiles)
            {
                if (file.ToString().Equals(filename))
                    return true;
            }
            return false;
		}

		public byte[] GetFile(string filename)
		{
			if (filename.StartsWith("/"))
				filename = filename.Replace('/', '\\');

			foreach (VfsFile file in vfsFiles)
			{
                if (file.ToString().Equals(filename))
                    return file.FileContent;
			}
            throw new FileNotFoundException(filename);
		}

		public void Dispose()
		{
			
		}
        #endregion
    }
}
