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.Blogs;

namespace NetinaShop.Domain.Mappers
{
    public static partial class BlogCategoryMapper
    {
        public static BlogCategory AdaptToBlogCategory(this BlogCategoryLDto p1)
        {
            return p1 == null ? null : new BlogCategory()
            {
                Name = p1.Name,
                Description = p1.Description,
                Blogs = funcMain1(p1.Blogs),
                Id = p1.Id,
                CreatedAt = p1.CreatedAt
            };
        }
        public static BlogCategory AdaptTo(this BlogCategoryLDto p3, BlogCategory p4)
        {
            if (p3 == null)
            {
                return null;
            }
            BlogCategory result = p4 ?? new BlogCategory();
            
            result.Name = p3.Name;
            result.Description = p3.Description;
            result.Blogs = funcMain2(p3.Blogs, result.Blogs);
            result.Id = p3.Id;
            result.CreatedAt = p3.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BlogCategoryLDto, BlogCategory>> ProjectToBlogCategory => p7 => new BlogCategory()
        {
            Name = p7.Name,
            Description = p7.Description,
            Blogs = p7.Blogs.Select<BlogSDto, Blog>(p8 => new Blog()
            {
                Title = p8.Title,
                Content = p8.Content,
                Tags = p8.Tags,
                ReadingTime = p8.ReadingTime,
                Summery = p8.Summery,
                IsSuggested = p8.IsSuggested,
                CategoryId = p8.CategoryId,
                Category = new BlogCategory()
                {
                    Name = p8.CategoryName,
                    Id = p8.CategoryId
                },
                Id = p8.Id,
                CreatedAt = p8.CreatedAt
            }).ToList<Blog>(),
            Id = p7.Id,
            CreatedAt = p7.CreatedAt
        };
        public static BlogCategoryLDto AdaptToLDto(this BlogCategory p9)
        {
            return p9 == null ? null : new BlogCategoryLDto()
            {
                Name = p9.Name,
                Description = p9.Description,
                Blogs = funcMain3(p9.Blogs),
                Id = p9.Id,
                CreatedAt = p9.CreatedAt
            };
        }
        public static BlogCategoryLDto AdaptTo(this BlogCategory p11, BlogCategoryLDto p12)
        {
            if (p11 == null)
            {
                return null;
            }
            BlogCategoryLDto result = p12 ?? new BlogCategoryLDto();
            
            result.Name = p11.Name;
            result.Description = p11.Description;
            result.Blogs = funcMain6(p11.Blogs, result.Blogs);
            result.Id = p11.Id;
            result.CreatedAt = p11.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BlogCategory, BlogCategoryLDto>> ProjectToLDto => p15 => new BlogCategoryLDto()
        {
            Name = p15.Name,
            Description = p15.Description,
            Blogs = p15.Blogs.Select<Blog, BlogSDto>(p16 => new BlogSDto()
            {
                Title = p16.Title,
                Content = p16.Content,
                Tags = p16.Tags,
                ReadingTime = p16.ReadingTime,
                Summery = p16.Summery,
                IsSuggested = p16.IsSuggested,
                CategoryId = p16.CategoryId,
                CategoryName = p16.Category.Name,
                HeaderFileName = p16.Files.Count > 0 && p16.Files.Any<BlogStorageFile>(f => f.IsHeader) ? p16.Files.FirstOrDefault<BlogStorageFile>(f => f.IsHeader).FileName : string.Empty,
                Id = p16.Id,
                CreatedAt = p16.CreatedAt
            }).ToList<BlogSDto>(),
            Id = p15.Id,
            CreatedAt = p15.CreatedAt
        };
        public static BlogCategory AdaptToBlogCategory(this BlogCategorySDto p17)
        {
            return p17 == null ? null : new BlogCategory()
            {
                Name = p17.Name,
                Description = p17.Description,
                Id = p17.Id,
                CreatedAt = p17.CreatedAt
            };
        }
        public static BlogCategory AdaptTo(this BlogCategorySDto p18, BlogCategory p19)
        {
            if (p18 == null)
            {
                return null;
            }
            BlogCategory result = p19 ?? new BlogCategory();
            
            result.Name = p18.Name;
            result.Description = p18.Description;
            result.Id = p18.Id;
            result.CreatedAt = p18.CreatedAt;
            return result;
            
        }
        public static BlogCategorySDto AdaptToSDto(this BlogCategory p20)
        {
            return p20 == null ? null : new BlogCategorySDto()
            {
                Name = p20.Name,
                Description = p20.Description,
                Id = p20.Id,
                CreatedAt = p20.CreatedAt
            };
        }
        public static BlogCategorySDto AdaptTo(this BlogCategory p21, BlogCategorySDto p22)
        {
            if (p21 == null)
            {
                return null;
            }
            BlogCategorySDto result = p22 ?? new BlogCategorySDto();
            
            result.Name = p21.Name;
            result.Description = p21.Description;
            result.Id = p21.Id;
            result.CreatedAt = p21.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BlogCategory, BlogCategorySDto>> ProjectToSDto => p23 => new BlogCategorySDto()
        {
            Name = p23.Name,
            Description = p23.Description,
            Id = p23.Id,
            CreatedAt = p23.CreatedAt
        };
        
        private static List<Blog> funcMain1(List<BlogSDto> p2)
        {
            if (p2 == null)
            {
                return null;
            }
            List<Blog> result = new List<Blog>(p2.Count);
            
            int i = 0;
            int len = p2.Count;
            
            while (i < len)
            {
                BlogSDto item = p2[i];
                result.Add(item == null ? null : new Blog()
                {
                    Title = item.Title,
                    Content = item.Content,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    Category = new BlogCategory()
                    {
                        Name = item.CategoryName,
                        Id = item.CategoryId
                    },
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static List<Blog> funcMain2(List<BlogSDto> p5, List<Blog> p6)
        {
            if (p5 == null)
            {
                return null;
            }
            List<Blog> result = new List<Blog>(p5.Count);
            
            int i = 0;
            int len = p5.Count;
            
            while (i < len)
            {
                BlogSDto item = p5[i];
                result.Add(item == null ? null : new Blog()
                {
                    Title = item.Title,
                    Content = item.Content,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    Category = new BlogCategory()
                    {
                        Name = item.CategoryName,
                        Id = item.CategoryId
                    },
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static List<BlogSDto> funcMain3(List<Blog> p10)
        {
            if (p10 == null)
            {
                return null;
            }
            List<BlogSDto> result = new List<BlogSDto>(p10.Count);
            
            int i = 0;
            int len = p10.Count;
            
            while (i < len)
            {
                Blog item = p10[i];
                result.Add(item == null ? null : new BlogSDto()
                {
                    Title = item.Title,
                    Content = item.Content,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    CategoryName = item.Category == null ? null : item.Category.Name,
                    HeaderFileName = item.Files.Count > 0 && item.Files.Any<BlogStorageFile>(funcMain4) ? item.Files.FirstOrDefault<BlogStorageFile>(funcMain5).FileName : string.Empty,
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static List<BlogSDto> funcMain6(List<Blog> p13, List<BlogSDto> p14)
        {
            if (p13 == null)
            {
                return null;
            }
            List<BlogSDto> result = new List<BlogSDto>(p13.Count);
            
            int i = 0;
            int len = p13.Count;
            
            while (i < len)
            {
                Blog item = p13[i];
                result.Add(item == null ? null : new BlogSDto()
                {
                    Title = item.Title,
                    Content = item.Content,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    CategoryName = item.Category == null ? null : item.Category.Name,
                    HeaderFileName = item.Files.Count > 0 && item.Files.Any<BlogStorageFile>(funcMain4) ? item.Files.FirstOrDefault<BlogStorageFile>(funcMain5).FileName : string.Empty,
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static bool funcMain4(BlogStorageFile f)
        {
            return f.IsHeader;
        }
        
        private static bool funcMain5(BlogStorageFile f)
        {
            return f.IsHeader;
        }
    }
}