using System.Collections.Generic;
using System.IO;
using System.Linq;
using Interfaces;

namespace RaceLapTimer.Extensions.FileExport
{
    class FileExportManager:IExportManager
    {
        private readonly List<IFileExporter> _exporters;

        public FileExportManager(IPluginLocator locator)
        {
            _exporters = locator.Locate<IFileExporter>();
        }

        public List<IFileExporter> GetExporters()
        {
            return _exporters;
        }

        public void Export(string exportType, string fileContent, string baseUrl, out Stream exportStream)
        {
            if (_exporters.Any())
            {
                var exporter = _exporters.FirstOrDefault(x => x.GetDetails().Type == exportType);
                if (exporter != null)
                {
                    exporter.Export(fileContent, baseUrl, out exportStream);
                }
                else
                {
                    exportStream = new MemoryStream();
                }
            }
            else
                exportStream = new MemoryStream();
        }

        public List<ExportProviderDetails> GetExporterDetails()
        {
            var ret = new List<ExportProviderDetails>();
            foreach (var exporter in _exporters)
            {
                ret.Add(exporter.GetDetails());
            }
            return ret;
        }
    }
}
