using Gremlin.GremlinData.EntityClasses;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using static Gremlin.GremlinData.EntityClasses.Enums;

namespace Gremlin.GremlinData.DBClasses
{
    public static class MetaDataSetter
    {
        private static readonly DateTime FarInTheFuture = DateTime.Parse("2050-12-31t00:00:00.000000z", CultureInfo.CurrentCulture); 
        
        public static IMetadata ForImport(
            IMetadata entity, 
            string datamodifiedby = "", 
            string dataversioncomment = "", 
            [CallerMemberName] string callername = "")
        {
            SetMetaData(entity, datamodifiedby, dataversioncomment, callername);
            return entity;
        }

        public static List<IMetadata> ForImport(
            List<IMetadata> entities,
            string datamodifiedby = "",
            string dataversioncomment = "",
            [CallerMemberName] string callername = "")
        {
            //check if entities implements IMetaData:
            //Ist das überhaupt nötig?
            if ((entities is IMetadata) == false)
            {
                //no action / return list unchanged
                return entities;
            }

            //set metadata
            foreach (var entity in entities)
            {
                SetMetaData(entity, datamodifiedby, dataversioncomment, callername);
            }
            return entities;
        }

        private static IMetadata SetMetaData(
            IMetadata entity,
            string datamodifiedby = "",
            string dataversioncomment = "",
            [CallerMemberName] string callername = "")
        {
            entity.DataCreationDate = DateTime.Now;
            entity.DataModificationDate = DateTime.Now;
            entity.DataModificationByUser = datamodifiedby == "" ? callername : datamodifiedby;
            entity.DataStatus = Status.Active.ToString();
            entity.DataValidFrom = DateTime.Now;
            entity.DataValidUntil = FarInTheFuture;
            entity.DataVersionNumber++;
            entity.DataVersionComment = dataversioncomment;

            return entity;

        }
    }
}
