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 Brizco.Domain.Entities.Users;

namespace Brizco.Domain.Mappers
{
    public static partial class ComplexMapper
    {
        public static Complex AdaptToComplex(this ComplexSDto p1)
        {
            return p1 == null ? null : new Complex()
            {
                Name = p1.Name,
                Address = p1.Address,
                SupportPhone = p1.SupportPhone,
                Id = p1.Id
            };
        }
        public static Complex AdaptTo(this ComplexSDto p2, Complex p3)
        {
            if (p2 == null)
            {
                return null;
            }
            Complex result = p3 ?? new Complex();
            
            result.Name = p2.Name;
            result.Address = p2.Address;
            result.SupportPhone = p2.SupportPhone;
            result.Id = p2.Id;
            return result;
            
        }
        public static Expression<Func<ComplexSDto, Complex>> ProjectToComplex => p4 => new Complex()
        {
            Name = p4.Name,
            Address = p4.Address,
            SupportPhone = p4.SupportPhone,
            Id = p4.Id
        };
        public static ComplexSDto AdaptToSDto(this Complex p5)
        {
            return p5 == null ? null : new ComplexSDto()
            {
                Name = p5.Name,
                Address = p5.Address,
                SupportPhone = p5.SupportPhone,
                Id = p5.Id
            };
        }
        public static ComplexSDto AdaptTo(this Complex p6, ComplexSDto p7)
        {
            if (p6 == null)
            {
                return null;
            }
            ComplexSDto result = p7 ?? new ComplexSDto();
            
            result.Name = p6.Name;
            result.Address = p6.Address;
            result.SupportPhone = p6.SupportPhone;
            result.Id = p6.Id;
            return result;
            
        }
        public static Expression<Func<Complex, ComplexSDto>> ProjectToSDto => p8 => new ComplexSDto()
        {
            Name = p8.Name,
            Address = p8.Address,
            SupportPhone = p8.SupportPhone,
            Id = p8.Id
        };
        public static Complex AdaptToComplex(this ComplexLDto p9)
        {
            return p9 == null ? null : new Complex()
            {
                Name = p9.Name,
                Address = p9.Address,
                SupportPhone = p9.SupportPhone,
                Users = funcMain1(p9.Users),
                Id = p9.Id
            };
        }
        public static Complex AdaptTo(this ComplexLDto p11, Complex p12)
        {
            if (p11 == null)
            {
                return null;
            }
            Complex result = p12 ?? new Complex();
            
            result.Name = p11.Name;
            result.Address = p11.Address;
            result.SupportPhone = p11.SupportPhone;
            result.Users = funcMain2(p11.Users, result.Users);
            result.Id = p11.Id;
            return result;
            
        }
        public static ComplexLDto AdaptToLDto(this Complex p15)
        {
            return p15 == null ? null : new ComplexLDto()
            {
                Name = p15.Name,
                Address = p15.Address,
                SupportPhone = p15.SupportPhone,
                Users = funcMain3(p15.Users),
                Id = p15.Id
            };
        }
        public static ComplexLDto AdaptTo(this Complex p17, ComplexLDto p18)
        {
            if (p17 == null)
            {
                return null;
            }
            ComplexLDto result = p18 ?? new ComplexLDto();
            
            result.Name = p17.Name;
            result.Address = p17.Address;
            result.SupportPhone = p17.SupportPhone;
            result.Users = funcMain4(p17.Users, result.Users);
            result.Id = p17.Id;
            return result;
            
        }
        public static Expression<Func<Complex, ComplexLDto>> ProjectToLDto => p21 => new ComplexLDto()
        {
            Name = p21.Name,
            Address = p21.Address,
            SupportPhone = p21.SupportPhone,
            Users = p21.Users.Select<ComplexUser, ComplexUserSDto>(p22 => new ComplexUserSDto()
            {
                FirstName = p22.User != null ? p22.User.FirstName : string.Empty,
                LastName = p22.User != null ? p22.User.LastName : string.Empty,
                NationalId = p22.User != null ? p22.User.NationalId : string.Empty,
                ComplexName = p22.Complex != null ? p22.Complex.Name : string.Empty,
                UserId = p22.UserId,
                ComplexId = p22.ComplexId,
                Id = p22.Id
            }).ToList<ComplexUserSDto>(),
            Id = p21.Id
        };
        
        private static List<ComplexUser> funcMain1(List<ComplexUserSDto> p10)
        {
            if (p10 == null)
            {
                return null;
            }
            List<ComplexUser> result = new List<ComplexUser>(p10.Count);
            
            int i = 0;
            int len = p10.Count;
            
            while (i < len)
            {
                ComplexUserSDto item = p10[i];
                result.Add(item == null ? null : new ComplexUser()
                {
                    UserId = item.UserId,
                    ComplexId = item.ComplexId,
                    User = new ApplicationUser() {Id = item.UserId},
                    Complex = new Complex()
                    {
                        Name = item.ComplexName,
                        Id = item.ComplexId
                    },
                    Id = item.Id
                });
                i++;
            }
            return result;
            
        }
        
        private static List<ComplexUser> funcMain2(List<ComplexUserSDto> p13, List<ComplexUser> p14)
        {
            if (p13 == null)
            {
                return null;
            }
            List<ComplexUser> result = new List<ComplexUser>(p13.Count);
            
            int i = 0;
            int len = p13.Count;
            
            while (i < len)
            {
                ComplexUserSDto item = p13[i];
                result.Add(item == null ? null : new ComplexUser()
                {
                    UserId = item.UserId,
                    ComplexId = item.ComplexId,
                    User = new ApplicationUser() {Id = item.UserId},
                    Complex = new Complex()
                    {
                        Name = item.ComplexName,
                        Id = item.ComplexId
                    },
                    Id = item.Id
                });
                i++;
            }
            return result;
            
        }
        
        private static List<ComplexUserSDto> funcMain3(List<ComplexUser> p16)
        {
            if (p16 == null)
            {
                return null;
            }
            List<ComplexUserSDto> result = new List<ComplexUserSDto>(p16.Count);
            
            int i = 0;
            int len = p16.Count;
            
            while (i < len)
            {
                ComplexUser item = p16[i];
                result.Add(item == null ? null : new ComplexUserSDto()
                {
                    FirstName = item.User != null ? item.User.FirstName : string.Empty,
                    LastName = item.User != null ? item.User.LastName : string.Empty,
                    NationalId = item.User != null ? item.User.NationalId : string.Empty,
                    ComplexName = item.Complex != null ? item.Complex.Name : string.Empty,
                    UserId = item.UserId,
                    ComplexId = item.ComplexId,
                    Id = item.Id
                });
                i++;
            }
            return result;
            
        }
        
        private static List<ComplexUserSDto> funcMain4(List<ComplexUser> p19, List<ComplexUserSDto> p20)
        {
            if (p19 == null)
            {
                return null;
            }
            List<ComplexUserSDto> result = new List<ComplexUserSDto>(p19.Count);
            
            int i = 0;
            int len = p19.Count;
            
            while (i < len)
            {
                ComplexUser item = p19[i];
                result.Add(item == null ? null : new ComplexUserSDto()
                {
                    FirstName = item.User != null ? item.User.FirstName : string.Empty,
                    LastName = item.User != null ? item.User.LastName : string.Empty,
                    NationalId = item.User != null ? item.User.NationalId : string.Empty,
                    ComplexName = item.Complex != null ? item.Complex.Name : string.Empty,
                    UserId = item.UserId,
                    ComplexId = item.ComplexId,
                    Id = item.Id
                });
                i++;
            }
            return result;
            
        }
    }
}