using AutoMapper;
using PARR.Core.Infrastructure.Mapping.RobotTask.Resolvers;
using PARR.Domain.DTOs.RobotTask;
using PARR.Domain.Entities;
using PARR.Domain.Settings;

namespace PARR.Core.Infrastructure.Mapping.RobotTask
{
    /// <summary>
    /// Маппинг RobotTask
    /// </summary>
    internal class RobotTaskMappingProfile : Profile
    {
        public RobotTaskMappingProfile()
        {

            CreateMap<RobotConfiguration, RobotTaskBase>()
                .Include<RobotConfiguration, RobotTaskTemplate>()
                .Include<RobotConfiguration, RobotTaskSchedule>()
                .ForMember(d => d.TaskId, o => o.MapFrom(s => s.Id));

            // Задание роботам, шаблоны
            CreateMap<RobotConfiguration, RobotTaskTemplate>()
                .ForMember(d => d.IsActive, o => o.MapFrom(s => s.Template!.IsActiveTemplate))
                //.ForMember(d => d.ClosingCode, o => o.MapFrom<RobotTaskTemplateClosingCodeResolver>())
                .ForMember(d => d.FullDescription, o => o.MapFrom(s => s.Template!.Job!.Group!.FullDescription))
                .ForMember(d => d.ShortDescription, o => o.MapFrom(s => s.Template!.Job!.Group!.ShortDescription))
                .ForMember(d => d.Solution, o => o.MapFrom(s => s.Template!.Job!.Group!.Solution))
                //ЗО берем у группы а не у хоста
                .ForMember(d => d.ResponseArea, o => o.MapFrom(s => s.Template!.Unit!.BaseFields!.ResponseArea))
                .ForMember(d => d.TemplateDuration, o => o.MapFrom(s => s.Template!.Job!.Group!.TemplateDuration))
                //.ForMember(d => d.Initiator, o => o.MapFrom<RobotTaskTemplateInitiatorResolver>())
                //.ForMember(d => d.Category, o => o.MapFrom<RobotTaskTemplateCategoryResolver>())
                .ForMember(d => d.WorkGroup, o => o.MapFrom(s => s.Template!.Job!.WorkGroupMask))
                .ForMember(d => d.Ek, o => o.MapFrom(s => s.Template!.Unit!.Name))
                .ForMember(d => d.Name, o => o.MapFrom(s => s.Template!.Name))
                .ForMember(d => d.ProcessName, o => o.MapFrom(s => s.Template!.Job!.Tnk!.Subprocess!.Process!.Name))
                .ForMember(d => d.ProcessEsppId, o => o.MapFrom(s => s.Template!.Job!.Tnk!.Subprocess!.Process!.EsppId))
                .ForMember(d => d.SubprocessName, o => o.MapFrom(s => s.Template!.Job!.Tnk!.Subprocess!.Name))
                .ForMember(d => d.SubprocessEsppId, o => o.MapFrom(s => s.Template!.Job!.Tnk!.Subprocess!.EsppId))
                .ForMember(d => d.TnkName, o => o.MapFrom(s => s.Template!.Job!.Tnk!.Name))
                .ForMember(d => d.TnkEsppId, o => o.MapFrom(s =>
                                                                s.Template!.Job!.Tnk!.EsppId.HasValue
                                                                ? s.Template!.Job!.Tnk!.EsppId.Value.ToString()
                                                                : string.Empty
                                                            ))
                .ForMember(d => d.WorkName, o => o.MapFrom(s => s.Template!.Job!.WorkName))
                .ForMember(d => d.ScheduleEsppId, o => o.MapFrom(s => s.Template!.ScheduleEsppId));


            // Задание роботам, расписания
            CreateMap<RobotConfiguration, RobotTaskSchedule>()
                 .ForMember(d => d.EsppId, o => o.MapFrom(s => s.Template!.ScheduleEsppId))
                 .ForMember(d => d.ScheduleName, o => o.MapFrom(s => s.Template!.Name))
                 .ForMember(d => d.IsActive, o => o.MapFrom(s => s.Template!.IsActiveSchedule))
                 .ForMember(d => d.ResponseArea, o => o.MapFrom(s => s.Template!.Unit!.BaseFields!.ResponseArea))
                 .ForMember(d => d.TemplateName, o => o.MapFrom(s => s.Template!.Name))
                 .ForMember(d => d.TemplateId, o => o.MapFrom(s => s.Template!.Id))
                 .ForMember(d => d.WorkGroup, o => o.MapFrom(s => s.Template!.Job!.WorkGroupMask))
                 .ForMember(d => d.Exclude, o => o.MapFrom(s => s.Template!.Job!.Group!.ScheduleExcludeType!.EsppName))
                 .ForMember(d => d.ExcludeCalendar, o => o.MapFrom(s => s.Template!.Job!.Group!.ScheduleExcludeTypeCalendar != null ? s.Template!.Job!.Group!.ScheduleExcludeTypeCalendar.EsppName : null))
                 //.ForMember(d => d.RepeatRange, o => o.MapFrom<RobotTaskScheduleRepeatRangeResolver>())
                 .ForMember(d => d.ScheduleType, o => o.MapFrom(s =>
                        s.Template!.Job!.Group!.EsppSchValues!.First()!.EsppSchTypeConfig!.EsppSchTypeSchedule!.Description))
                 .ForMember(d => d.ScheduleTypeCode, o => o.MapFrom(s =>
                        s.Template!.Job!.Group!.EsppSchValues!.First()!.EsppSchTypeConfig!.EsppSchTypeSchedule!.Id))
                 .ForMember(d => d.Schedule, o => o.MapFrom<RobotTaskScheduleSchResolver>());
        }
    }
}
