using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Mapster.Models;
using NetinaShop.Domain.Dtos.LargDtos;
using NetinaShop.Domain.Dtos.SmallDtos;
using NetinaShop.Domain.Entities.ProductCategories;

namespace NetinaShop.Domain.Mappers
{
    public static partial class ProductCategoryMapper
    {
        public static ProductCategory AdaptToProductCategory(this ProductCategoryLDto p1)
        {
            return p1 == null ? null : new ProductCategory()
            {
                Name = p1.Name,
                Description = p1.Description,
                ParentId = (Guid?)p1.ParentId,
                Files = funcMain1(p1.Files),
                Id = p1.Id,
                CreatedAt = p1.CreatedAt
            };
        }
        public static ProductCategory AdaptTo(this ProductCategoryLDto p3, ProductCategory p4)
        {
            if (p3 == null)
            {
                return null;
            }
            ProductCategory result = p4 ?? new ProductCategory();
            
            result.Name = p3.Name;
            result.Description = p3.Description;
            result.ParentId = (Guid?)p3.ParentId;
            result.Files = funcMain2(p3.Files, result.Files);
            result.Id = p3.Id;
            result.CreatedAt = p3.CreatedAt;
            return result;
            
        }
        public static Expression<Func<ProductCategoryLDto, ProductCategory>> ProjectToProductCategory => p7 => new ProductCategory()
        {
            Name = p7.Name,
            Description = p7.Description,
            ParentId = (Guid?)p7.ParentId,
            Files = p7.Files.Select<StorageFileSDto, ProductCategoryStorageFile>(p8 => new ProductCategoryStorageFile()
            {
                Name = p8.Name,
                FileLocation = p8.FileLocation,
                FileName = p8.FileName,
                IsHeader = p8.IsHeader,
                IsPrimary = p8.IsPrimary,
                FileType = p8.FileType,
                Id = p8.Id,
                CreatedAt = p8.CreatedAt
            }).ToList<ProductCategoryStorageFile>(),
            Id = p7.Id,
            CreatedAt = p7.CreatedAt
        };
        public static ProductCategoryLDto AdaptToLDto(this ProductCategory p9)
        {
            return p9 == null ? null : new ProductCategoryLDto()
            {
                Name = p9.Name,
                Description = p9.Description,
                ParentId = p9.ParentId == null ? default(Guid) : (Guid)p9.ParentId,
                Files = funcMain3(p9.Files),
                Id = p9.Id,
                CreatedAt = p9.CreatedAt
            };
        }
        public static ProductCategoryLDto AdaptTo(this ProductCategory p11, ProductCategoryLDto p12)
        {
            if (p11 == null)
            {
                return null;
            }
            ProductCategoryLDto result = p12 ?? new ProductCategoryLDto();
            
            result.Name = p11.Name;
            result.Description = p11.Description;
            result.ParentId = p11.ParentId == null ? default(Guid) : (Guid)p11.ParentId;
            result.Files = funcMain4(p11.Files, result.Files);
            result.Id = p11.Id;
            result.CreatedAt = p11.CreatedAt;
            return result;
            
        }
        public static Expression<Func<ProductCategory, ProductCategoryLDto>> ProjectToLDto => p15 => new ProductCategoryLDto()
        {
            Name = p15.Name,
            Description = p15.Description,
            ParentId = p15.ParentId == null ? default(Guid) : (Guid)p15.ParentId,
            Files = p15.Files.Select<ProductCategoryStorageFile, StorageFileSDto>(p16 => new StorageFileSDto()
            {
                Name = p16.Name,
                FileLocation = p16.FileLocation,
                FileName = p16.FileName,
                IsHeader = p16.IsHeader,
                IsPrimary = p16.IsPrimary,
                FileType = p16.FileType,
                Id = p16.Id
            }).ToList<StorageFileSDto>(),
            Id = p15.Id,
            CreatedAt = p15.CreatedAt
        };
        public static ProductCategory AdaptToProductCategory(this ProductCategorySDto p17)
        {
            return p17 == null ? null : new ProductCategory()
            {
                Name = p17.Name,
                Description = p17.Description,
                IsMain = p17.IsMain,
                ParentId = (Guid?)p17.ParentId,
                Parent = new ProductCategory()
                {
                    Name = p17.ParentName,
                    Id = p17.ParentId
                },
                Id = p17.Id,
                CreatedAt = p17.CreatedAt
            };
        }
        public static ProductCategory AdaptTo(this ProductCategorySDto p18, ProductCategory p19)
        {
            if (p18 == null)
            {
                return null;
            }
            ProductCategory result = p19 ?? new ProductCategory();
            
            result.Name = p18.Name;
            result.Description = p18.Description;
            result.IsMain = p18.IsMain;
            result.ParentId = (Guid?)p18.ParentId;
            result.Parent = funcMain5(new Never(), result.Parent, p18);
            result.Id = p18.Id;
            result.CreatedAt = p18.CreatedAt;
            return result;
            
        }
        public static ProductCategorySDto AdaptToSDto(this ProductCategory p22)
        {
            return p22 == null ? null : new ProductCategorySDto()
            {
                Name = p22.Name,
                Description = p22.Description,
                IsMain = p22.IsMain,
                ParentId = p22.ParentId == null ? default(Guid) : (Guid)p22.ParentId,
                ParentName = p22.Parent != null ? p22.Parent.Name : string.Empty,
                Id = p22.Id,
                CreatedAt = p22.CreatedAt
            };
        }
        public static ProductCategorySDto AdaptTo(this ProductCategory p23, ProductCategorySDto p24)
        {
            if (p23 == null)
            {
                return null;
            }
            ProductCategorySDto result = p24 ?? new ProductCategorySDto();
            
            result.Name = p23.Name;
            result.Description = p23.Description;
            result.IsMain = p23.IsMain;
            result.ParentId = p23.ParentId == null ? default(Guid) : (Guid)p23.ParentId;
            result.ParentName = p23.Parent != null ? p23.Parent.Name : string.Empty;
            result.Id = p23.Id;
            result.CreatedAt = p23.CreatedAt;
            return result;
            
        }
        public static Expression<Func<ProductCategory, ProductCategorySDto>> ProjectToSDto => p25 => new ProductCategorySDto()
        {
            Name = p25.Name,
            Description = p25.Description,
            IsMain = p25.IsMain,
            ParentId = p25.ParentId == null ? default(Guid) : (Guid)p25.ParentId,
            ParentName = p25.Parent != null ? p25.Parent.Name : string.Empty,
            Id = p25.Id,
            CreatedAt = p25.CreatedAt
        };
        
        private static List<ProductCategoryStorageFile> funcMain1(List<StorageFileSDto> p2)
        {
            if (p2 == null)
            {
                return null;
            }
            List<ProductCategoryStorageFile> result = new List<ProductCategoryStorageFile>(p2.Count);
            
            int i = 0;
            int len = p2.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p2[i];
                result.Add(item == null ? null : new ProductCategoryStorageFile()
                {
                    Name = item.Name,
                    FileLocation = item.FileLocation,
                    FileName = item.FileName,
                    IsHeader = item.IsHeader,
                    IsPrimary = item.IsPrimary,
                    FileType = item.FileType,
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static List<ProductCategoryStorageFile> funcMain2(List<StorageFileSDto> p5, List<ProductCategoryStorageFile> p6)
        {
            if (p5 == null)
            {
                return null;
            }
            List<ProductCategoryStorageFile> result = new List<ProductCategoryStorageFile>(p5.Count);
            
            int i = 0;
            int len = p5.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p5[i];
                result.Add(item == null ? null : new ProductCategoryStorageFile()
                {
                    Name = item.Name,
                    FileLocation = item.FileLocation,
                    FileName = item.FileName,
                    IsHeader = item.IsHeader,
                    IsPrimary = item.IsPrimary,
                    FileType = item.FileType,
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static List<StorageFileSDto> funcMain3(List<ProductCategoryStorageFile> p10)
        {
            if (p10 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p10.Count);
            
            int i = 0;
            int len = p10.Count;
            
            while (i < len)
            {
                ProductCategoryStorageFile item = p10[i];
                result.Add(item == null ? null : new StorageFileSDto()
                {
                    Name = item.Name,
                    FileLocation = item.FileLocation,
                    FileName = item.FileName,
                    IsHeader = item.IsHeader,
                    IsPrimary = item.IsPrimary,
                    FileType = item.FileType,
                    Id = item.Id
                });
                i++;
            }
            return result;
            
        }
        
        private static List<StorageFileSDto> funcMain4(List<ProductCategoryStorageFile> p13, List<StorageFileSDto> p14)
        {
            if (p13 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p13.Count);
            
            int i = 0;
            int len = p13.Count;
            
            while (i < len)
            {
                ProductCategoryStorageFile item = p13[i];
                result.Add(item == null ? null : new StorageFileSDto()
                {
                    Name = item.Name,
                    FileLocation = item.FileLocation,
                    FileName = item.FileName,
                    IsHeader = item.IsHeader,
                    IsPrimary = item.IsPrimary,
                    FileType = item.FileType,
                    Id = item.Id
                });
                i++;
            }
            return result;
            
        }
        
        private static ProductCategory funcMain5(Never p20, ProductCategory p21, ProductCategorySDto p18)
        {
            ProductCategory result = p21 ?? new ProductCategory();
            
            result.Name = p18.ParentName;
            result.Id = p18.ParentId;
            return result;
            
        }
    }
}