using System;
using System.Collections.Generic;
using System.IO;
using ExifLibrary;
using JetBrains.Annotations;
using PhotoRenamer.Base.Types;

namespace PhotoRenamer.Base
{
    public static class FilesHelper
    {
        private static readonly string[] SupportedFileExtensions = {".jpg", ".cr2", ".mp4"};

        public static IEnumerable<string> FindSupportedFilesRecursively(string path)
        {
            var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var fileExt = Path.GetExtension(file);
                if(fileExt == null) continue;
                foreach (var supportedFileExtension in SupportedFileExtensions)
                {
                    if (fileExt.Equals(supportedFileExtension, StringComparison.InvariantCultureIgnoreCase)) yield return file;
                }
            }
        }

        public static IEnumerable<MediaFile> GetMediaFiles(IEnumerable<string> files)
        {
            if (files == null) throw new ArgumentNullException(nameof(files));
            
            foreach (var file in files)
            {
                var imageFile = ImageFile.FromFile(file);
                var dateTime = imageFile.Properties.Get<ExifDateTime>(ExifTag.DateTimeOriginal);
                var mediaFile  = new MediaFile(file, dateTime.Value);
                yield return mediaFile;
            }
        }
    }
}
