using CsvHelper;
using GreenTree.Maschinenbestellungen.Web.Models.Import;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace GreenTree.Maschinenbestellungen.Web.Helper
{
    public static class ImportHelper
    {
        /// <summary>
        /// Checks an ImportModel instance for a valid import file for the specified data type <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="TEntity">The data type.</typeparam>
        /// <param name="model">The import model.</param>
        public static void CheckImport<TEntity>(ImportModel model)
        {
            CheckImport<TEntity, TEntity>(model);
        }

        /// <summary>
        /// Checks an ImportModel instance for a valid import file for the specified data type <typeparamref name="T"/>
        /// </summary>
        /// <typeparam name="TEntity">The data type.</typeparam>
        /// <typeparam name="TMap">The data map type.</typeparam>
        /// <param name="model">The import model.</param>
        public static void CheckImport<TEntity, TMap>(ImportModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model", "The importModel must not be NULL.");

            if (model.ImportFile == null)
                throw new ArgumentException("The importModel does not contain a file.", "model");

            var tempfile = Path.GetTempFileName();

            using var writer = new FileStream(tempfile, FileMode.Append);

            model.ImportFile.CopyTo(writer);

            writer.Close();

            using (var reader = new StreamReader(tempfile))
            {
                using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);

                csv.Configuration.Delimiter = ";";
                csv.Configuration.BadDataFound = null;

                if (typeof(TEntity) != typeof(TMap))
                    csv.Configuration.RegisterClassMap(typeof(TMap));

                csv.Read();
                csv.ReadHeader();

                csv.ValidateHeader<TEntity>();
            }
        }

        /// <summary>
        /// Gets an object collection of <typeparamref name="T"/> from the import file
        /// </summary>
        /// <typeparam name="TEntity">The data type.</typeparam>
        /// <param name="model">The import model.</param>
        public static IEnumerable<TEntity> GetFromImport<TEntity>(ImportModel model)
        {
            return GetFromImport<TEntity, TEntity>(model);
        }

        /// <summary>
        /// Gets an object collection of <typeparamref name="T"/> from the import file
        /// </summary>
        /// <typeparam name="TEntity">The data type.</typeparam>
        /// <typeparam name="TMap">The data map type.</typeparam>
        /// <param name="model">The import model.</param>
        public static IEnumerable<TEntity> GetFromImport<TEntity, TMap>(ImportModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model", "The importModel must not be NULL.");

            if (model.ImportFile == null)
                throw new ArgumentException("The importModel does not contain a file.", "model");

            var tempfile = Path.GetTempFileName();

            using var writer = new FileStream(tempfile, FileMode.Append);

            model.ImportFile.CopyTo(writer);

            writer.Close();

            using (var reader = new StreamReader(tempfile))
            {
                using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);

                csv.Configuration.Delimiter = ";";
                csv.Configuration.BadDataFound = null;

                if (typeof(TEntity) != typeof(TMap))
                    csv.Configuration.RegisterClassMap(typeof(TMap));

                csv.Read();
                csv.ReadHeader();

                csv.ValidateHeader<TEntity>();

                var result = csv.GetRecords<TEntity>().ToArray();

                return result;
            }
        }
    }
}
