using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NetinaShop.Domain.Dtos.LargDtos;
using NetinaShop.Domain.Dtos.SmallDtos;
using NetinaShop.Domain.Entities.Brands;

namespace NetinaShop.Domain.Mappers
{
    public static partial class BrandMapper
    {
        public static Brand AdaptToBrand(this BrandLDto p1)
        {
            return p1 == null ? null : new Brand()
            {
                PersianName = p1.PersianName,
                EnglishName = p1.EnglishName,
                Description = p1.Description,
                HasSpecialPage = p1.HasSpecialPage,
                PageUrl = p1.PageUrl,
                Files = funcMain1(p1.Files),
                Id = p1.Id,
                CreatedAt = p1.CreatedAt
            };
        }
        public static Brand AdaptTo(this BrandLDto p3, Brand p4)
        {
            if (p3 == null)
            {
                return null;
            }
            Brand result = p4 ?? new Brand();
            
            result.PersianName = p3.PersianName;
            result.EnglishName = p3.EnglishName;
            result.Description = p3.Description;
            result.HasSpecialPage = p3.HasSpecialPage;
            result.PageUrl = p3.PageUrl;
            result.Files = funcMain2(p3.Files, result.Files);
            result.Id = p3.Id;
            result.CreatedAt = p3.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BrandLDto, Brand>> ProjectToBrand => p7 => new Brand()
        {
            PersianName = p7.PersianName,
            EnglishName = p7.EnglishName,
            Description = p7.Description,
            HasSpecialPage = p7.HasSpecialPage,
            PageUrl = p7.PageUrl,
            Files = p7.Files.Select<StorageFileSDto, BrandStorageFile>(p8 => new BrandStorageFile()
            {
                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<BrandStorageFile>(),
            Id = p7.Id,
            CreatedAt = p7.CreatedAt
        };
        public static BrandLDto AdaptToLDto(this Brand p9)
        {
            return p9 == null ? null : new BrandLDto()
            {
                PersianName = p9.PersianName,
                EnglishName = p9.EnglishName,
                Description = p9.Description,
                HasSpecialPage = p9.HasSpecialPage,
                PageUrl = p9.PageUrl,
                Files = funcMain3(p9.Files),
                Id = p9.Id,
                CreatedAt = p9.CreatedAt
            };
        }
        public static BrandLDto AdaptTo(this Brand p11, BrandLDto p12)
        {
            if (p11 == null)
            {
                return null;
            }
            BrandLDto result = p12 ?? new BrandLDto();
            
            result.PersianName = p11.PersianName;
            result.EnglishName = p11.EnglishName;
            result.Description = p11.Description;
            result.HasSpecialPage = p11.HasSpecialPage;
            result.PageUrl = p11.PageUrl;
            result.Files = funcMain4(p11.Files, result.Files);
            result.Id = p11.Id;
            result.CreatedAt = p11.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Brand, BrandLDto>> ProjectToLDto => p15 => new BrandLDto()
        {
            PersianName = p15.PersianName,
            EnglishName = p15.EnglishName,
            Description = p15.Description,
            HasSpecialPage = p15.HasSpecialPage,
            PageUrl = p15.PageUrl,
            Files = p15.Files.Select<BrandStorageFile, 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 Brand AdaptToBrand(this BrandSDto p17)
        {
            return p17 == null ? null : new Brand()
            {
                PersianName = p17.PersianName,
                EnglishName = p17.EnglishName,
                Description = p17.Description,
                HasSpecialPage = p17.HasSpecialPage,
                PageUrl = p17.PageUrl,
                Id = p17.Id,
                CreatedAt = p17.CreatedAt
            };
        }
        public static Brand AdaptTo(this BrandSDto p18, Brand p19)
        {
            if (p18 == null)
            {
                return null;
            }
            Brand result = p19 ?? new Brand();
            
            result.PersianName = p18.PersianName;
            result.EnglishName = p18.EnglishName;
            result.Description = p18.Description;
            result.HasSpecialPage = p18.HasSpecialPage;
            result.PageUrl = p18.PageUrl;
            result.Id = p18.Id;
            result.CreatedAt = p18.CreatedAt;
            return result;
            
        }
        public static BrandSDto AdaptToSDto(this Brand p20)
        {
            return p20 == null ? null : new BrandSDto()
            {
                PersianName = p20.PersianName,
                EnglishName = p20.EnglishName,
                Description = p20.Description,
                HasSpecialPage = p20.HasSpecialPage,
                PageUrl = p20.PageUrl,
                HeaderFileName = p20.Files.Count > 0 && p20.Files.Any<BrandStorageFile>(funcMain5) ? p20.Files.FirstOrDefault<BrandStorageFile>(funcMain6).FileName : string.Empty,
                Id = p20.Id,
                CreatedAt = p20.CreatedAt
            };
        }
        public static BrandSDto AdaptTo(this Brand p21, BrandSDto p22)
        {
            if (p21 == null)
            {
                return null;
            }
            BrandSDto result = p22 ?? new BrandSDto();
            
            result.PersianName = p21.PersianName;
            result.EnglishName = p21.EnglishName;
            result.Description = p21.Description;
            result.HasSpecialPage = p21.HasSpecialPage;
            result.PageUrl = p21.PageUrl;
            result.HeaderFileName = p21.Files.Count > 0 && p21.Files.Any<BrandStorageFile>(funcMain5) ? p21.Files.FirstOrDefault<BrandStorageFile>(funcMain6).FileName : string.Empty;
            result.Id = p21.Id;
            result.CreatedAt = p21.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Brand, BrandSDto>> ProjectToSDto => p23 => new BrandSDto()
        {
            PersianName = p23.PersianName,
            EnglishName = p23.EnglishName,
            Description = p23.Description,
            HasSpecialPage = p23.HasSpecialPage,
            PageUrl = p23.PageUrl,
            HeaderFileName = p23.Files.Count > 0 && p23.Files.Any<BrandStorageFile>(f => f.IsHeader) ? p23.Files.FirstOrDefault<BrandStorageFile>(f => f.IsHeader).FileName : string.Empty,
            Id = p23.Id,
            CreatedAt = p23.CreatedAt
        };
        
        private static List<BrandStorageFile> funcMain1(List<StorageFileSDto> p2)
        {
            if (p2 == null)
            {
                return null;
            }
            List<BrandStorageFile> result = new List<BrandStorageFile>(p2.Count);
            
            int i = 0;
            int len = p2.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p2[i];
                result.Add(item == null ? null : new BrandStorageFile()
                {
                    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<BrandStorageFile> funcMain2(List<StorageFileSDto> p5, List<BrandStorageFile> p6)
        {
            if (p5 == null)
            {
                return null;
            }
            List<BrandStorageFile> result = new List<BrandStorageFile>(p5.Count);
            
            int i = 0;
            int len = p5.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p5[i];
                result.Add(item == null ? null : new BrandStorageFile()
                {
                    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<BrandStorageFile> p10)
        {
            if (p10 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p10.Count);
            
            int i = 0;
            int len = p10.Count;
            
            while (i < len)
            {
                BrandStorageFile 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<BrandStorageFile> 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)
            {
                BrandStorageFile 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 bool funcMain5(BrandStorageFile f)
        {
            return f.IsHeader;
        }
        
        private static bool funcMain6(BrandStorageFile f)
        {
            return f.IsHeader;
        }
    }
}