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.Blogs;
using Netina.Domain.Entities.Users;

namespace Netina.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,
                Slug = p8.Slug,
                Tags = p8.Tags,
                ReadingTime = p8.ReadingTime,
                Summery = p8.Summery,
                IsSuggested = p8.IsSuggested,
                CategoryId = p8.CategoryId,
                Category = new BlogCategory()
                {
                    Name = p8.CategoryName,
                    Id = p8.CategoryId
                },
                AuthorId = p8.AuthorId,
                Author = new ApplicationUser() {Id = p8.AuthorId},
                Id = p8.Id,
                CreatedAt = p8.CreatedAt,
                ModifiedAt = p8.ModifiedAt
            }).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,
                Slug = p16.Slug,
                Tags = p16.Tags,
                ReadingTime = p16.ReadingTime,
                Summery = p16.Summery,
                IsSuggested = p16.IsSuggested,
                CategoryId = p16.CategoryId,
                CategoryName = p16.Category.Name,
                MainImage = p16.Files.Count > 0 && p16.Files.Any<BlogStorageFile>(f => f.IsPrimary) ? p16.Files.FirstOrDefault<BlogStorageFile>(f => f.IsPrimary).FileName : string.Empty,
                ModifiedAt = p16.ModifiedAt,
                AuthorId = p16.AuthorId,
                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,
                Slug = p17.Slug,
                Description = p17.Description,
                IsMain = p17.IsMain,
                ParentId = (Guid?)p17.ParentId,
                Parent = new BlogCategory() {Id = p17.ParentId},
                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.Slug = p18.Slug;
            result.Description = p18.Description;
            result.IsMain = p18.IsMain;
            result.ParentId = (Guid?)p18.ParentId;
            result.Parent = funcMain7(new Never(), result.Parent, p18);
            result.Id = p18.Id;
            result.CreatedAt = p18.CreatedAt;
            return result;
            
        }
        public static BlogCategorySDto AdaptToSDto(this BlogCategory p22)
        {
            return p22 == null ? null : new BlogCategorySDto()
            {
                Name = p22.Name,
                BlogCount = funcMain8(p22.Blogs == null ? null : (int?)p22.Blogs.Count),
                Description = p22.Description,
                Slug = p22.Slug,
                IsMain = p22.IsMain,
                ParentId = p22.ParentId == null ? default(Guid) : (Guid)p22.ParentId,
                Id = p22.Id,
                CreatedAt = p22.CreatedAt
            };
        }
        public static BlogCategorySDto AdaptTo(this BlogCategory p24, BlogCategorySDto p25)
        {
            if (p24 == null)
            {
                return null;
            }
            BlogCategorySDto result = p25 ?? new BlogCategorySDto();
            
            result.Name = p24.Name;
            result.BlogCount = funcMain9(p24.Blogs == null ? null : (int?)p24.Blogs.Count, result.BlogCount);
            result.Description = p24.Description;
            result.Slug = p24.Slug;
            result.IsMain = p24.IsMain;
            result.ParentId = p24.ParentId == null ? default(Guid) : (Guid)p24.ParentId;
            result.Id = p24.Id;
            result.CreatedAt = p24.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BlogCategory, BlogCategorySDto>> ProjectToSDto => p28 => new BlogCategorySDto()
        {
            Name = p28.Name,
            BlogCount = p28.Blogs.Count,
            Description = p28.Description,
            Slug = p28.Slug,
            IsMain = p28.IsMain,
            ParentId = p28.ParentId == null ? default(Guid) : (Guid)p28.ParentId,
            Id = p28.Id,
            CreatedAt = p28.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,
                    Slug = item.Slug,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    Category = new BlogCategory()
                    {
                        Name = item.CategoryName,
                        Id = item.CategoryId
                    },
                    AuthorId = item.AuthorId,
                    Author = new ApplicationUser() {Id = item.AuthorId},
                    Id = item.Id,
                    CreatedAt = item.CreatedAt,
                    ModifiedAt = item.ModifiedAt
                });
                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,
                    Slug = item.Slug,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    Category = new BlogCategory()
                    {
                        Name = item.CategoryName,
                        Id = item.CategoryId
                    },
                    AuthorId = item.AuthorId,
                    Author = new ApplicationUser() {Id = item.AuthorId},
                    Id = item.Id,
                    CreatedAt = item.CreatedAt,
                    ModifiedAt = item.ModifiedAt
                });
                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,
                    Slug = item.Slug,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    CategoryName = item.Category == null ? null : item.Category.Name,
                    MainImage = item.Files.Count > 0 && item.Files.Any<BlogStorageFile>(funcMain4) ? item.Files.FirstOrDefault<BlogStorageFile>(funcMain5).FileName : string.Empty,
                    ModifiedAt = item.ModifiedAt,
                    AuthorId = item.AuthorId,
                    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,
                    Slug = item.Slug,
                    Tags = item.Tags,
                    ReadingTime = item.ReadingTime,
                    Summery = item.Summery,
                    IsSuggested = item.IsSuggested,
                    CategoryId = item.CategoryId,
                    CategoryName = item.Category == null ? null : item.Category.Name,
                    MainImage = item.Files.Count > 0 && item.Files.Any<BlogStorageFile>(funcMain4) ? item.Files.FirstOrDefault<BlogStorageFile>(funcMain5).FileName : string.Empty,
                    ModifiedAt = item.ModifiedAt,
                    AuthorId = item.AuthorId,
                    Id = item.Id,
                    CreatedAt = item.CreatedAt
                });
                i++;
            }
            return result;
            
        }
        
        private static BlogCategory funcMain7(Never p20, BlogCategory p21, BlogCategorySDto p18)
        {
            BlogCategory result = p21 ?? new BlogCategory();
            
            result.Id = p18.ParentId;
            return result;
            
        }
        
        private static int funcMain8(int? p23)
        {
            return p23 == null ? 0 : (int)p23;
        }
        
        private static int funcMain9(int? p26, int p27)
        {
            return p26 == null ? 0 : (int)p26;
        }
        
        private static bool funcMain4(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static bool funcMain5(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
    }
}