using Exam.Infrastructure.Data.Entity;
using Exam.Infrastructure.Data.Interface;
using Exam.Infrastructure.Extensions;
using Hotline.Exams.Base;
using XF.Domain.Authentications;
using XF.Domain.Entities;
using XF.Domain.Repository;

namespace Hotline.Repository.SqlSugar.Exam.Extensions
{
    public static class ApiServiceExtension
    {
        public static void ToInsert<T>(this T entity, ISessionContext sessionContext = null) where T : class, IEntity<string>, new()
        {
            if (entity.Id.IsNullOrEmpty())
            {
                if (entity is Entity)
                {
                    (entity as Entity)?.InitId();
                }
            }

            if (entity is CreationEntity)
            {
                var creationEntity = entity as CreationEntity;
                if (creationEntity != null)
                {
                    creationEntity.CreationTime = DateTime.Now;

                }
            }

            if (entity is ExamBusinessEntity)
            {
                var creationEntity = entity as ExamBusinessEntity;
                if (creationEntity != null)
                {
                    creationEntity.Status = Share.Enums.Exams.EPublicStatus.Valid;

                }
            }

            if (sessionContext != null)
            {
                var current = entity as Entity;
                if (current != null)
                {
                    current.CreatorId = sessionContext.UserId;
                    current.CreatorOrgId = sessionContext.OrgId;
                    current.CreatorOrgName = sessionContext.OrgName;
                    current.CreatorOrgLevel = sessionContext.OrgLevel;
                    current.CreatorName = sessionContext.UserName;
                    current.AreaId = sessionContext.AreaId;
                }
            }

        }

        public static void ToInsert<T>(this List<T> entities, ISessionContext sessionContext = null) where T : class, IEntity<string>, new()
        {
            foreach (var entity in entities)
            {
                entity.ToInsert(sessionContext);
            }
        }

        public static void ToUpdate<T>(this T entity, ISessionContext sessionContext = null) where T : class, IEntity<string>, new()
        {
            if (entity is FullStateEntity)
            {
                var fullStateEntity = entity as FullStateEntity;
                if (fullStateEntity != null)
                    fullStateEntity.LastModificationTime = DateTime.Now;

                entity = fullStateEntity as T ?? entity;

            }

            if (sessionContext != null)
            {
                var current = entity as Entity;
                if (current != null)
                {
                    current.CreatorId = sessionContext.UserId;
                    current.CreatorOrgId = sessionContext.OrgId;
                    current.CreatorOrgName = sessionContext.OrgName;
                    current.CreatorOrgLevel = sessionContext.OrgLevel;
                    current.CreatorName = sessionContext.UserName;
                    current.AreaId = sessionContext.AreaId;
                }

            }
        }

        public static void ToUpdate<T>(this List<T> entities, ISessionContext sessionContext = null) where T : class, IEntity<string>, new()
        {
            foreach (var entity in entities)
            {
                entity.ToUpdate(sessionContext);
            }
        }
    }
}
