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

namespace Netina.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,
                IsMain = p1.IsMain,
                ParentId = (Guid?)p1.ParentId,
                Parent = new ProductCategory()
                {
                    Name = p1.ParentName,
                    Id = 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.IsMain = p3.IsMain;
            result.ParentId = (Guid?)p3.ParentId;
            result.Parent = funcMain2(new Never(), result.Parent, p3);
            result.Files = funcMain3(p3.Files, result.Files);
            result.Id = p3.Id;
            result.CreatedAt = p3.CreatedAt;
            return result;
            
        }
        public static Expression<Func<ProductCategoryLDto, ProductCategory>> ProjectToProductCategory => p9 => new ProductCategory()
        {
            Name = p9.Name,
            Description = p9.Description,
            IsMain = p9.IsMain,
            ParentId = (Guid?)p9.ParentId,
            Parent = new ProductCategory()
            {
                Name = p9.ParentName,
                Id = p9.ParentId
            },
            Files = p9.Files.Select<StorageFileSDto, ProductCategoryStorageFile>(p10 => new ProductCategoryStorageFile()
            {
                Name = p10.Name,
                FileLocation = p10.FileLocation,
                FileName = p10.FileName,
                IsHeader = p10.IsHeader,
                IsPrimary = p10.IsPrimary,
                FileType = p10.FileType,
                Id = p10.Id,
                CreatedAt = p10.CreatedAt
            }).ToList<ProductCategoryStorageFile>(),
            Id = p9.Id,
            CreatedAt = p9.CreatedAt
        };
        public static ProductCategoryLDto AdaptToLDto(this ProductCategory p11)
        {
            return p11 == null ? null : new ProductCategoryLDto()
            {
                Name = p11.Name,
                Description = p11.Description,
                ParentId = p11.ParentId == null ? default(Guid) : (Guid)p11.ParentId,
                ParentName = p11.Parent != null ? p11.Parent.Name : string.Empty,
                IsMain = p11.IsMain,
                Files = funcMain4(p11.Files),
                Id = p11.Id,
                CreatedAt = p11.CreatedAt
            };
        }
        public static ProductCategoryLDto AdaptTo(this ProductCategory p13, ProductCategoryLDto p14)
        {
            if (p13 == null)
            {
                return null;
            }
            ProductCategoryLDto result = p14 ?? new ProductCategoryLDto();
            
            result.Name = p13.Name;
            result.Description = p13.Description;
            result.ParentId = p13.ParentId == null ? default(Guid) : (Guid)p13.ParentId;
            result.ParentName = p13.Parent != null ? p13.Parent.Name : string.Empty;
            result.IsMain = p13.IsMain;
            result.Files = funcMain5(p13.Files, result.Files);
            result.Id = p13.Id;
            result.CreatedAt = p13.CreatedAt;
            return result;
            
        }
        public static Expression<Func<ProductCategory, ProductCategoryLDto>> ProjectToLDto => p17 => new ProductCategoryLDto()
        {
            Name = p17.Name,
            Description = p17.Description,
            ParentId = p17.ParentId == null ? default(Guid) : (Guid)p17.ParentId,
            ParentName = p17.Parent != null ? p17.Parent.Name : string.Empty,
            IsMain = p17.IsMain,
            Files = p17.Files.Select<ProductCategoryStorageFile, StorageFileSDto>(p18 => new StorageFileSDto()
            {
                Name = p18.Name,
                FileLocation = p18.FileLocation,
                FileName = p18.FileName,
                IsHeader = p18.IsHeader,
                IsPrimary = p18.IsPrimary,
                FileType = p18.FileType,
                Id = p18.Id
            }).ToList<StorageFileSDto>(),
            Id = p17.Id,
            CreatedAt = p17.CreatedAt
        };
        public static ProductCategory AdaptToProductCategory(this ProductCategorySDto p19)
        {
            return p19 == null ? null : new ProductCategory()
            {
                Name = p19.Name,
                Slug = p19.Slug,
                Description = p19.Description,
                IsMain = p19.IsMain,
                ParentId = (Guid?)p19.ParentId,
                Parent = new ProductCategory()
                {
                    Name = p19.ParentName,
                    Id = p19.ParentId
                },
                Id = p19.Id,
                CreatedAt = p19.CreatedAt
            };
        }
        public static ProductCategory AdaptTo(this ProductCategorySDto p20, ProductCategory p21)
        {
            if (p20 == null)
            {
                return null;
            }
            ProductCategory result = p21 ?? new ProductCategory();
            
            result.Name = p20.Name;
            result.Slug = p20.Slug;
            result.Description = p20.Description;
            result.IsMain = p20.IsMain;
            result.ParentId = (Guid?)p20.ParentId;
            result.Parent = funcMain6(new Never(), result.Parent, p20);
            result.Id = p20.Id;
            result.CreatedAt = p20.CreatedAt;
            return result;
            
        }
        public static ProductCategorySDto AdaptToSDto(this ProductCategory p24)
        {
            return p24 == null ? null : new ProductCategorySDto()
            {
                Name = p24.Name,
                Description = p24.Description,
                Slug = p24.Slug,
                IsMain = p24.IsMain,
                ParentId = p24.ParentId == null ? default(Guid) : (Guid)p24.ParentId,
                ParentName = p24.Parent != null ? p24.Parent.Name : string.Empty,
                MainImage = p24.Files.FirstOrDefault<ProductCategoryStorageFile>(funcMain7) != null ? p24.Files.FirstOrDefault<ProductCategoryStorageFile>(funcMain8).FileLocation : (p24.Files.Count > 0 ? p24.Files.FirstOrDefault<ProductCategoryStorageFile>().FileLocation : string.Empty),
                Id = p24.Id,
                CreatedAt = p24.CreatedAt
            };
        }
        public static ProductCategorySDto AdaptTo(this ProductCategory p25, ProductCategorySDto p26)
        {
            if (p25 == null)
            {
                return null;
            }
            ProductCategorySDto result = p26 ?? new ProductCategorySDto();
            
            result.Name = p25.Name;
            result.Description = p25.Description;
            result.Slug = p25.Slug;
            result.IsMain = p25.IsMain;
            result.ParentId = p25.ParentId == null ? default(Guid) : (Guid)p25.ParentId;
            result.ParentName = p25.Parent != null ? p25.Parent.Name : string.Empty;
            result.MainImage = p25.Files.FirstOrDefault<ProductCategoryStorageFile>(funcMain7) != null ? p25.Files.FirstOrDefault<ProductCategoryStorageFile>(funcMain8).FileLocation : (p25.Files.Count > 0 ? p25.Files.FirstOrDefault<ProductCategoryStorageFile>().FileLocation : string.Empty);
            result.Id = p25.Id;
            result.CreatedAt = p25.CreatedAt;
            return result;
            
        }
        public static Expression<Func<ProductCategory, ProductCategorySDto>> ProjectToSDto => p27 => new ProductCategorySDto()
        {
            Name = p27.Name,
            Description = p27.Description,
            Slug = p27.Slug,
            IsMain = p27.IsMain,
            ParentId = p27.ParentId == null ? default(Guid) : (Guid)p27.ParentId,
            ParentName = p27.Parent != null ? p27.Parent.Name : string.Empty,
            MainImage = p27.Files.FirstOrDefault<ProductCategoryStorageFile>(f => f.IsPrimary) != null ? p27.Files.FirstOrDefault<ProductCategoryStorageFile>(f => f.IsPrimary).FileLocation : (p27.Files.Count > 0 ? p27.Files.FirstOrDefault<ProductCategoryStorageFile>().FileLocation : string.Empty),
            Id = p27.Id,
            CreatedAt = p27.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 ProductCategory funcMain2(Never p5, ProductCategory p6, ProductCategoryLDto p3)
        {
            ProductCategory result = p6 ?? new ProductCategory();
            
            result.Name = p3.ParentName;
            result.Id = p3.ParentId;
            return result;
            
        }
        
        private static List<ProductCategoryStorageFile> funcMain3(List<StorageFileSDto> p7, List<ProductCategoryStorageFile> p8)
        {
            if (p7 == null)
            {
                return null;
            }
            List<ProductCategoryStorageFile> result = new List<ProductCategoryStorageFile>(p7.Count);
            
            int i = 0;
            int len = p7.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p7[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> funcMain4(List<ProductCategoryStorageFile> p12)
        {
            if (p12 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p12.Count);
            
            int i = 0;
            int len = p12.Count;
            
            while (i < len)
            {
                ProductCategoryStorageFile item = p12[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> funcMain5(List<ProductCategoryStorageFile> p15, List<StorageFileSDto> p16)
        {
            if (p15 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p15.Count);
            
            int i = 0;
            int len = p15.Count;
            
            while (i < len)
            {
                ProductCategoryStorageFile item = p15[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 funcMain6(Never p22, ProductCategory p23, ProductCategorySDto p20)
        {
            ProductCategory result = p23 ?? new ProductCategory();
            
            result.Name = p20.ParentName;
            result.Id = p20.ParentId;
            return result;
            
        }
        
        private static bool funcMain7(ProductCategoryStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static bool funcMain8(ProductCategoryStorageFile f)
        {
            return f.IsPrimary;
        }
    }
}