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 BlogMapper
    {
        public static Blog AdaptToBlog(this BlogLDto p1)
        {
            return p1 == null ? null : new Blog()
            {
                Title = p1.Title,
                Content = p1.Content,
                Slug = p1.Slug,
                Tags = p1.Tags,
                ReadingTime = p1.ReadingTime,
                Summery = p1.Summery,
                IsSuggested = p1.IsSuggested,
                CategoryId = p1.CategoryId,
                Category = new BlogCategory()
                {
                    Name = p1.CategoryName,
                    Id = p1.CategoryId
                },
                Files = funcMain1(p1.Files),
                AuthorId = p1.AuthorId,
                Author = new ApplicationUser() {Id = p1.AuthorId},
                Id = p1.Id,
                CreatedAt = p1.CreatedAt
            };
        }
        public static Blog AdaptTo(this BlogLDto p3, Blog p4)
        {
            if (p3 == null)
            {
                return null;
            }
            Blog result = p4 ?? new Blog();
            
            result.Title = p3.Title;
            result.Content = p3.Content;
            result.Slug = p3.Slug;
            result.Tags = p3.Tags;
            result.ReadingTime = p3.ReadingTime;
            result.Summery = p3.Summery;
            result.IsSuggested = p3.IsSuggested;
            result.CategoryId = p3.CategoryId;
            result.Category = funcMain2(new Never(), result.Category, p3);
            result.Files = funcMain3(p3.Files, result.Files);
            result.AuthorId = p3.AuthorId;
            result.Author = funcMain4(new Never(), result.Author, p3);
            result.Id = p3.Id;
            result.CreatedAt = p3.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BlogLDto, Blog>> ProjectToBlog => p11 => new Blog()
        {
            Title = p11.Title,
            Content = p11.Content,
            Slug = p11.Slug,
            Tags = p11.Tags,
            ReadingTime = p11.ReadingTime,
            Summery = p11.Summery,
            IsSuggested = p11.IsSuggested,
            CategoryId = p11.CategoryId,
            Category = new BlogCategory()
            {
                Name = p11.CategoryName,
                Id = p11.CategoryId
            },
            Files = p11.Files.Select<StorageFileSDto, BlogStorageFile>(p12 => new BlogStorageFile()
            {
                Name = p12.Name,
                FileLocation = p12.FileLocation,
                FileName = p12.FileName,
                IsHeader = p12.IsHeader,
                IsPrimary = p12.IsPrimary,
                FileType = p12.FileType,
                Id = p12.Id,
                CreatedAt = p12.CreatedAt
            }).ToList<BlogStorageFile>(),
            AuthorId = p11.AuthorId,
            Author = new ApplicationUser() {Id = p11.AuthorId},
            Id = p11.Id,
            CreatedAt = p11.CreatedAt
        };
        public static BlogLDto AdaptToLDto(this Blog p13)
        {
            return p13 == null ? null : new BlogLDto()
            {
                Title = p13.Title,
                Content = p13.Content,
                Tags = p13.Tags,
                Slug = p13.Slug,
                ReadingTime = p13.ReadingTime,
                Summery = p13.Summery,
                MainImage = p13.Files.Count > 0 && p13.Files.Any<BlogStorageFile>(funcMain5) ? p13.Files.FirstOrDefault<BlogStorageFile>(funcMain6).FileName : string.Empty,
                IsSuggested = p13.IsSuggested,
                CategoryId = p13.CategoryId,
                CategoryName = p13.Category == null ? null : p13.Category.Name,
                AuthorId = p13.AuthorId,
                AuthorFullName = p13.Author != null ? p13.Author.FirstName + " " + p13.Author.LastName : string.Empty,
                Files = funcMain7(p13.Files),
                Id = p13.Id,
                CreatedAt = p13.CreatedAt
            };
        }
        public static BlogLDto AdaptTo(this Blog p15, BlogLDto p16)
        {
            if (p15 == null)
            {
                return null;
            }
            BlogLDto result = p16 ?? new BlogLDto();
            
            result.Title = p15.Title;
            result.Content = p15.Content;
            result.Tags = p15.Tags;
            result.Slug = p15.Slug;
            result.ReadingTime = p15.ReadingTime;
            result.Summery = p15.Summery;
            result.MainImage = p15.Files.Count > 0 && p15.Files.Any<BlogStorageFile>(funcMain5) ? p15.Files.FirstOrDefault<BlogStorageFile>(funcMain6).FileName : string.Empty;
            result.IsSuggested = p15.IsSuggested;
            result.CategoryId = p15.CategoryId;
            result.CategoryName = p15.Category == null ? null : p15.Category.Name;
            result.AuthorId = p15.AuthorId;
            result.AuthorFullName = p15.Author != null ? p15.Author.FirstName + " " + p15.Author.LastName : string.Empty;
            result.Files = funcMain8(p15.Files, result.Files);
            result.Id = p15.Id;
            result.CreatedAt = p15.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Blog, BlogLDto>> ProjectToLDto => p19 => new BlogLDto()
        {
            Title = p19.Title,
            Content = p19.Content,
            Tags = p19.Tags,
            Slug = p19.Slug,
            ReadingTime = p19.ReadingTime,
            Summery = p19.Summery,
            MainImage = p19.Files.Count > 0 && p19.Files.Any<BlogStorageFile>(f => f.IsPrimary) ? p19.Files.FirstOrDefault<BlogStorageFile>(f => f.IsPrimary).FileName : string.Empty,
            IsSuggested = p19.IsSuggested,
            CategoryId = p19.CategoryId,
            CategoryName = p19.Category.Name,
            AuthorId = p19.AuthorId,
            AuthorFullName = p19.Author != null ? p19.Author.FirstName + " " + p19.Author.LastName : string.Empty,
            Files = p19.Files.Select<BlogStorageFile, StorageFileSDto>(p20 => new StorageFileSDto()
            {
                Name = p20.Name,
                FileLocation = p20.FileLocation,
                FileName = p20.FileName,
                IsHeader = p20.IsHeader,
                IsPrimary = p20.IsPrimary,
                FileType = p20.FileType,
                Id = p20.Id
            }).ToList<StorageFileSDto>(),
            Id = p19.Id,
            CreatedAt = p19.CreatedAt
        };
        public static Blog AdaptToBlog(this BlogSDto p21)
        {
            return p21 == null ? null : new Blog()
            {
                Title = p21.Title,
                Slug = p21.Slug,
                Tags = p21.Tags,
                ReadingTime = p21.ReadingTime,
                Summery = p21.Summery,
                IsSuggested = p21.IsSuggested,
                CategoryId = p21.CategoryId,
                Category = new BlogCategory()
                {
                    Name = p21.CategoryName,
                    Id = p21.CategoryId
                },
                AuthorId = p21.AuthorId,
                Author = new ApplicationUser() {Id = p21.AuthorId},
                Id = p21.Id,
                CreatedAt = p21.CreatedAt,
                ModifiedAt = p21.ModifiedAt
            };
        }
        public static Blog AdaptTo(this BlogSDto p22, Blog p23)
        {
            if (p22 == null)
            {
                return null;
            }
            Blog result = p23 ?? new Blog();
            
            result.Title = p22.Title;
            result.Slug = p22.Slug;
            result.Tags = p22.Tags;
            result.ReadingTime = p22.ReadingTime;
            result.Summery = p22.Summery;
            result.IsSuggested = p22.IsSuggested;
            result.CategoryId = p22.CategoryId;
            result.Category = funcMain9(new Never(), result.Category, p22);
            result.AuthorId = p22.AuthorId;
            result.Author = funcMain10(new Never(), result.Author, p22);
            result.Id = p22.Id;
            result.CreatedAt = p22.CreatedAt;
            result.ModifiedAt = p22.ModifiedAt;
            return result;
            
        }
        public static BlogSDto AdaptToSDto(this Blog p28)
        {
            return p28 == null ? null : new BlogSDto()
            {
                Title = p28.Title,
                Slug = p28.Slug,
                Tags = p28.Tags,
                ReadingTime = p28.ReadingTime,
                Summery = p28.Summery,
                IsSuggested = p28.IsSuggested,
                CategoryId = p28.CategoryId,
                CategoryName = p28.Category == null ? null : p28.Category.Name,
                MainImage = p28.Files.Count > 0 && p28.Files.Any<BlogStorageFile>(funcMain11) ? p28.Files.FirstOrDefault<BlogStorageFile>(funcMain12).FileName : string.Empty,
                ModifiedAt = p28.ModifiedAt,
                AuthorId = p28.AuthorId,
                Id = p28.Id,
                CreatedAt = p28.CreatedAt
            };
        }
        public static BlogSDto AdaptTo(this Blog p29, BlogSDto p30)
        {
            if (p29 == null)
            {
                return null;
            }
            BlogSDto result = p30 ?? new BlogSDto();
            
            result.Title = p29.Title;
            result.Slug = p29.Slug;
            result.Tags = p29.Tags;
            result.ReadingTime = p29.ReadingTime;
            result.Summery = p29.Summery;
            result.IsSuggested = p29.IsSuggested;
            result.CategoryId = p29.CategoryId;
            result.CategoryName = p29.Category == null ? null : p29.Category.Name;
            result.MainImage = p29.Files.Count > 0 && p29.Files.Any<BlogStorageFile>(funcMain11) ? p29.Files.FirstOrDefault<BlogStorageFile>(funcMain12).FileName : string.Empty;
            result.ModifiedAt = p29.ModifiedAt;
            result.AuthorId = p29.AuthorId;
            result.Id = p29.Id;
            result.CreatedAt = p29.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Blog, BlogSDto>> ProjectToSDto => p31 => new BlogSDto()
        {
            Title = p31.Title,
            Slug = p31.Slug,
            Tags = p31.Tags,
            ReadingTime = p31.ReadingTime,
            Summery = p31.Summery,
            IsSuggested = p31.IsSuggested,
            CategoryId = p31.CategoryId,
            CategoryName = p31.Category.Name,
            MainImage = p31.Files.Count > 0 && p31.Files.Any<BlogStorageFile>(f => f.IsPrimary) ? p31.Files.FirstOrDefault<BlogStorageFile>(f => f.IsPrimary).FileName : string.Empty,
            ModifiedAt = p31.ModifiedAt,
            AuthorId = p31.AuthorId,
            Id = p31.Id,
            CreatedAt = p31.CreatedAt
        };
        
        private static List<BlogStorageFile> funcMain1(List<StorageFileSDto> p2)
        {
            if (p2 == null)
            {
                return null;
            }
            List<BlogStorageFile> result = new List<BlogStorageFile>(p2.Count);
            
            int i = 0;
            int len = p2.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p2[i];
                result.Add(item == null ? null : new BlogStorageFile()
                {
                    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 BlogCategory funcMain2(Never p5, BlogCategory p6, BlogLDto p3)
        {
            BlogCategory result = p6 ?? new BlogCategory();
            
            result.Name = p3.CategoryName;
            result.Id = p3.CategoryId;
            return result;
            
        }
        
        private static List<BlogStorageFile> funcMain3(List<StorageFileSDto> p7, List<BlogStorageFile> p8)
        {
            if (p7 == null)
            {
                return null;
            }
            List<BlogStorageFile> result = new List<BlogStorageFile>(p7.Count);
            
            int i = 0;
            int len = p7.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p7[i];
                result.Add(item == null ? null : new BlogStorageFile()
                {
                    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 ApplicationUser funcMain4(Never p9, ApplicationUser p10, BlogLDto p3)
        {
            ApplicationUser result = p10 ?? new ApplicationUser();
            
            result.Id = p3.AuthorId;
            return result;
            
        }
        
        private static bool funcMain5(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static bool funcMain6(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static List<StorageFileSDto> funcMain7(List<BlogStorageFile> p14)
        {
            if (p14 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p14.Count);
            
            int i = 0;
            int len = p14.Count;
            
            while (i < len)
            {
                BlogStorageFile item = p14[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> funcMain8(List<BlogStorageFile> p17, List<StorageFileSDto> p18)
        {
            if (p17 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p17.Count);
            
            int i = 0;
            int len = p17.Count;
            
            while (i < len)
            {
                BlogStorageFile item = p17[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 BlogCategory funcMain9(Never p24, BlogCategory p25, BlogSDto p22)
        {
            BlogCategory result = p25 ?? new BlogCategory();
            
            result.Name = p22.CategoryName;
            result.Id = p22.CategoryId;
            return result;
            
        }
        
        private static ApplicationUser funcMain10(Never p26, ApplicationUser p27, BlogSDto p22)
        {
            ApplicationUser result = p27 ?? new ApplicationUser();
            
            result.Id = p22.AuthorId;
            return result;
            
        }
        
        private static bool funcMain11(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static bool funcMain12(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
    }
}