using Lada.Framework.Data.Domains.BaseEntities;
using Lada.Framework.Infrastructures.Helpers;

namespace Lada.Framework.Data.Domains
{
    public static class EntityExtensions
    {
        


        public static T UpdateCommonInt<T>(this T entity, int updateBy = 0, string updateByUserName = "") where T : BaseEntityWithKey<int>
        {
            var currentDateTime = DateTimeHelper.CurrentTime();
            if (entity.Id > 0)
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = updateBy;
                    entity.UpdatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = currentDateTime;
            }
            else
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = entity.CreatedBy = updateBy;
                    entity.UpdatedByUserName = entity.CreatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = entity.CreatedTime = currentDateTime;
            }
            return entity;
        }

        public static T UpdateCommonUInt<T>(this T entity, int updateBy = 0, string updateByUserName = "") where T : BaseEntityWithKey<uint>
        {
            if (entity.Id > 0)
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = updateBy;
                    entity.UpdatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = DateTimeOffset.Now;
            }
            else
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = entity.CreatedBy = updateBy;
                    entity.UpdatedByUserName = entity.CreatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = entity.CreatedTime = DateTimeOffset.Now;
            }
            return entity;
        }

        public static T UpdateCommonLong<T>(this T entity, int updateBy = 0, string updateByUserName = "") where T : BaseEntityWithKey<long>
        {
            if (entity.Id > 0)
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = updateBy;
                    entity.UpdatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = DateTimeOffset.Now;
            }
            else
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = entity.CreatedBy = updateBy;
                    entity.UpdatedByUserName = entity.CreatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = entity.CreatedTime = DateTimeOffset.Now;
            }
            return entity;
        }

        public static T UpdateCommonGuid<T>(this T entity, int updateBy = 0, string updateByUserName = "") where T : BaseEntityWithKey<Guid>
        {
            if (entity.Id != Guid.Empty)
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = updateBy;
                    entity.UpdatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = DateTimeOffset.Now;
            }
            else
            {
                if (updateBy > 0 && !string.IsNullOrEmpty(updateByUserName))
                {
                    entity.UpdatedBy = entity.CreatedBy = updateBy;
                    entity.UpdatedByUserName = entity.CreatedByUserName = updateByUserName;
                }
                entity.UpdatedTime = entity.CreatedTime = DateTimeOffset.Now;
            }
            return entity;
        }

        public static T UpdateCommonInt<T>(this T entity) where T : BaseEntityWithKey<int>
        {
            if (entity.Id > 0)
            {
                entity.UpdatedTime = DateTimeOffset.Now;
            }
            else
            {
                entity.UpdatedTime = entity.CreatedTime = DateTimeOffset.Now;
            }
            return entity;
        }
        public static T UpdateCommonUInt<T>(this T entity) where T : BaseEntityWithKey<uint>
        {
            if (entity.Id > 0)
            {
                entity.UpdatedTime = DateTimeOffset.Now;
            }
            else
            {
                entity.UpdatedTime = entity.CreatedTime = DateTimeOffset.Now;
            }
            return entity;
        }

        public static T UpdateCommonlong<T>(this T entity) where T : BaseEntityWithKey<long>
        {
            if (entity.Id > 0)
            {
                entity.UpdatedTime = DateTimeOffset.Now;
            }
            else
            {
                entity.UpdatedTime = entity.CreatedTime = DateTimeOffset.Now;
            }
            return entity;
        }

        public static BaseEntity UpdateCreation(this BaseEntity entity, int updateBy = 0, string updateByUserName = "")
        {
            var currentDateTime = DateTimeHelper.CurrentTime();
            entity.CreatedBy = updateBy;
            entity.CreatedByUserName = updateByUserName;
            entity.UpdatedBy = updateBy;
            entity.UpdatedByUserName = updateByUserName;
            entity.UpdatedTime = entity.CreatedTime = currentDateTime;
            
            return entity;
        }
        public static BaseEntity UpdateUpdation(this BaseEntity entity, int updateBy = 0, string updateByUserName = "")
        {
            var currentDateTime = DateTimeHelper.CurrentTime();
            
            entity.UpdatedBy = updateBy;
            entity.UpdatedByUserName = updateByUserName;
            entity.UpdatedTime = currentDateTime;
            return entity;
        }

    }
}
