using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Brizco.Domain.Dtos.LargeDtos;
using Brizco.Domain.Dtos.SmallDtos;
using Brizco.Domain.Entities.Complexes;
using Mapster.Models;

namespace Brizco.Domain.Mappers
{
    public static partial class PositionMapper
    {
        public static Position AdaptToPosition(this PositionSDto p1)
        {
            return p1 == null ? null : new Position()
            {
                Name = p1.Name,
                Description = p1.Description,
                ComplexId = p1.ComplexId,
                SectionId = p1.SectionId,
                Id = p1.Id
            };
        }
        public static Position AdaptTo(this PositionSDto p2, Position p3)
        {
            if (p2 == null)
            {
                return null;
            }
            Position result = p3 ?? new Position();
            
            result.Name = p2.Name;
            result.Description = p2.Description;
            result.ComplexId = p2.ComplexId;
            result.SectionId = p2.SectionId;
            result.Id = p2.Id;
            return result;
            
        }
        public static Expression<Func<PositionSDto, Position>> ProjectToPosition => p4 => new Position()
        {
            Name = p4.Name,
            Description = p4.Description,
            ComplexId = p4.ComplexId,
            SectionId = p4.SectionId,
            Id = p4.Id
        };
        public static PositionSDto AdaptToSDto(this Position p5)
        {
            return p5 == null ? null : new PositionSDto()
            {
                Name = p5.Name,
                Description = p5.Description,
                ComplexId = p5.ComplexId,
                SectionId = p5.SectionId,
                Id = p5.Id
            };
        }
        public static PositionSDto AdaptTo(this Position p6, PositionSDto p7)
        {
            if (p6 == null)
            {
                return null;
            }
            PositionSDto result = p7 ?? new PositionSDto();
            
            result.Name = p6.Name;
            result.Description = p6.Description;
            result.ComplexId = p6.ComplexId;
            result.SectionId = p6.SectionId;
            result.Id = p6.Id;
            return result;
            
        }
        public static Expression<Func<Position, PositionSDto>> ProjectToSDto => p8 => new PositionSDto()
        {
            Name = p8.Name,
            Description = p8.Description,
            ComplexId = p8.ComplexId,
            SectionId = p8.SectionId,
            Id = p8.Id
        };
        public static Position AdaptToPosition(this PositionLDto p9)
        {
            return p9 == null ? null : new Position()
            {
                Name = p9.Name,
                Description = p9.Description,
                ComplexId = p9.ComplexId,
                Complex = new Complex() {Id = p9.ComplexId},
                SectionId = p9.SectionId,
                Section = new Section()
                {
                    Name = p9.SectionName,
                    Id = p9.SectionId
                },
                Permissions = funcMain1(p9.Permissions),
                Id = p9.Id
            };
        }
        public static Position AdaptTo(this PositionLDto p11, Position p12)
        {
            if (p11 == null)
            {
                return null;
            }
            Position result = p12 ?? new Position();
            
            result.Name = p11.Name;
            result.Description = p11.Description;
            result.ComplexId = p11.ComplexId;
            result.Complex = funcMain2(new Never(), result.Complex, p11);
            result.SectionId = p11.SectionId;
            result.Section = funcMain3(new Never(), result.Section, p11);
            result.Permissions = funcMain4(p11.Permissions, result.Permissions);
            result.Id = p11.Id;
            return result;
            
        }
        public static PositionLDto AdaptToLDto(this Position p19)
        {
            return p19 == null ? null : new PositionLDto()
            {
                Name = p19.Name,
                Description = p19.Description,
                ComplexId = p19.ComplexId,
                SectionId = p19.SectionId,
                SectionName = p19.Section != null ? p19.Section.Name : string.Empty,
                Permissions = funcMain5(p19.Permissions.Select<PositionPermission, string>(funcMain6)),
                Id = p19.Id
            };
        }
        public static PositionLDto AdaptTo(this Position p21, PositionLDto p22)
        {
            if (p21 == null)
            {
                return null;
            }
            PositionLDto result = p22 ?? new PositionLDto();
            
            result.Name = p21.Name;
            result.Description = p21.Description;
            result.ComplexId = p21.ComplexId;
            result.SectionId = p21.SectionId;
            result.SectionName = p21.Section != null ? p21.Section.Name : string.Empty;
            result.Permissions = funcMain7(p21.Permissions.Select<PositionPermission, string>(funcMain6), result.Permissions);
            result.Id = p21.Id;
            return result;
            
        }
        public static Expression<Func<Position, PositionLDto>> ProjectToLDto => p25 => new PositionLDto()
        {
            Name = p25.Name,
            Description = p25.Description,
            ComplexId = p25.ComplexId,
            SectionId = p25.SectionId,
            SectionName = p25.Section != null ? p25.Section.Name : string.Empty,
            Permissions = (List<string>)p25.Permissions.Select<PositionPermission, string>(p => p.Permission),
            Id = p25.Id
        };
        
        private static List<PositionPermission> funcMain1(List<string> p10)
        {
            if (p10 == null)
            {
                return null;
            }
            List<PositionPermission> result = new List<PositionPermission>(p10.Count);
            
            int i = 0;
            int len = p10.Count;
            
            while (i < len)
            {
                string item = p10[i];
                result.Add(item == null ? null : (PositionPermission)Convert.ChangeType((object)item, typeof(PositionPermission)));
                i++;
            }
            return result;
            
        }
        
        private static Complex funcMain2(Never p13, Complex p14, PositionLDto p11)
        {
            Complex result = p14 ?? new Complex();
            
            result.Id = p11.ComplexId;
            return result;
            
        }
        
        private static Section funcMain3(Never p15, Section p16, PositionLDto p11)
        {
            Section result = p16 ?? new Section();
            
            result.Name = p11.SectionName;
            result.Id = p11.SectionId;
            return result;
            
        }
        
        private static List<PositionPermission> funcMain4(List<string> p17, List<PositionPermission> p18)
        {
            if (p17 == null)
            {
                return null;
            }
            List<PositionPermission> result = new List<PositionPermission>(p17.Count);
            
            int i = 0;
            int len = p17.Count;
            
            while (i < len)
            {
                string item = p17[i];
                result.Add(item == null ? null : (PositionPermission)Convert.ChangeType((object)item, typeof(PositionPermission)));
                i++;
            }
            return result;
            
        }
        
        private static List<string> funcMain5(IEnumerable<string> p20)
        {
            if (p20 == null)
            {
                return null;
            }
            List<string> result = new List<string>();
            
            IEnumerator<string> enumerator = p20.GetEnumerator();
            
            while (enumerator.MoveNext())
            {
                string item = enumerator.Current;
                result.Add(item);
            }
            return result;
            
        }
        
        private static string funcMain6(PositionPermission p)
        {
            return p.Permission;
        }
        
        private static List<string> funcMain7(IEnumerable<string> p23, List<string> p24)
        {
            if (p23 == null)
            {
                return null;
            }
            List<string> result = new List<string>();
            
            IEnumerator<string> enumerator = p23.GetEnumerator();
            
            while (enumerator.MoveNext())
            {
                string item = enumerator.Current;
                result.Add(item);
            }
            return result;
            
        }
    }
}