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;

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),
                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.Id = p3.Id;
            result.CreatedAt = p3.CreatedAt;
            return result;
            
        }
        public static Expression<Func<BlogLDto, Blog>> ProjectToBlog => p9 => new Blog()
        {
            Title = p9.Title,
            Content = p9.Content,
            Slug = p9.Slug,
            Tags = p9.Tags,
            ReadingTime = p9.ReadingTime,
            Summery = p9.Summery,
            IsSuggested = p9.IsSuggested,
            CategoryId = p9.CategoryId,
            Category = new BlogCategory()
            {
                Name = p9.CategoryName,
                Id = p9.CategoryId
            },
            Files = p9.Files.Select<StorageFileSDto, BlogStorageFile>(p10 => new BlogStorageFile()
            {
                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<BlogStorageFile>(),
            Id = p9.Id,
            CreatedAt = p9.CreatedAt
        };
        public static BlogLDto AdaptToLDto(this Blog p11)
        {
            return p11 == null ? null : new BlogLDto()
            {
                Title = p11.Title,
                Content = p11.Content,
                Tags = p11.Tags,
                Slug = p11.Slug,
                ReadingTime = p11.ReadingTime,
                Summery = p11.Summery,
                MainImage = p11.Files.Count > 0 && p11.Files.Any<BlogStorageFile>(funcMain4) ? p11.Files.FirstOrDefault<BlogStorageFile>(funcMain5).FileName : string.Empty,
                IsSuggested = p11.IsSuggested,
                CategoryId = p11.CategoryId,
                CategoryName = p11.Category == null ? null : p11.Category.Name,
                Files = funcMain6(p11.Files),
                Id = p11.Id,
                CreatedAt = p11.CreatedAt
            };
        }
        public static BlogLDto AdaptTo(this Blog p13, BlogLDto p14)
        {
            if (p13 == null)
            {
                return null;
            }
            BlogLDto result = p14 ?? new BlogLDto();
            
            result.Title = p13.Title;
            result.Content = p13.Content;
            result.Tags = p13.Tags;
            result.Slug = p13.Slug;
            result.ReadingTime = p13.ReadingTime;
            result.Summery = p13.Summery;
            result.MainImage = p13.Files.Count > 0 && p13.Files.Any<BlogStorageFile>(funcMain4) ? p13.Files.FirstOrDefault<BlogStorageFile>(funcMain5).FileName : string.Empty;
            result.IsSuggested = p13.IsSuggested;
            result.CategoryId = p13.CategoryId;
            result.CategoryName = p13.Category == null ? null : p13.Category.Name;
            result.Files = funcMain7(p13.Files, result.Files);
            result.Id = p13.Id;
            result.CreatedAt = p13.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Blog, BlogLDto>> ProjectToLDto => p17 => new BlogLDto()
        {
            Title = p17.Title,
            Content = p17.Content,
            Tags = p17.Tags,
            Slug = p17.Slug,
            ReadingTime = p17.ReadingTime,
            Summery = p17.Summery,
            MainImage = p17.Files.Count > 0 && p17.Files.Any<BlogStorageFile>(f => f.IsPrimary) ? p17.Files.FirstOrDefault<BlogStorageFile>(f => f.IsPrimary).FileName : string.Empty,
            IsSuggested = p17.IsSuggested,
            CategoryId = p17.CategoryId,
            CategoryName = p17.Category.Name,
            Files = p17.Files.Select<BlogStorageFile, 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 Blog AdaptToBlog(this BlogSDto p19)
        {
            return p19 == null ? null : new Blog()
            {
                Title = p19.Title,
                Slug = p19.Slug,
                Tags = p19.Tags,
                ReadingTime = p19.ReadingTime,
                Summery = p19.Summery,
                IsSuggested = p19.IsSuggested,
                CategoryId = p19.CategoryId,
                Category = new BlogCategory()
                {
                    Name = p19.CategoryName,
                    Id = p19.CategoryId
                },
                Id = p19.Id,
                CreatedAt = p19.CreatedAt,
                ModifiedAt = p19.ModifiedAt
            };
        }
        public static Blog AdaptTo(this BlogSDto p20, Blog p21)
        {
            if (p20 == null)
            {
                return null;
            }
            Blog result = p21 ?? new Blog();
            
            result.Title = p20.Title;
            result.Slug = p20.Slug;
            result.Tags = p20.Tags;
            result.ReadingTime = p20.ReadingTime;
            result.Summery = p20.Summery;
            result.IsSuggested = p20.IsSuggested;
            result.CategoryId = p20.CategoryId;
            result.Category = funcMain8(new Never(), result.Category, p20);
            result.Id = p20.Id;
            result.CreatedAt = p20.CreatedAt;
            result.ModifiedAt = p20.ModifiedAt;
            return result;
            
        }
        public static BlogSDto AdaptToSDto(this Blog p24)
        {
            return p24 == null ? null : new BlogSDto()
            {
                Title = p24.Title,
                Slug = p24.Slug,
                Tags = p24.Tags,
                ReadingTime = p24.ReadingTime,
                Summery = p24.Summery,
                IsSuggested = p24.IsSuggested,
                CategoryId = p24.CategoryId,
                CategoryName = p24.Category == null ? null : p24.Category.Name,
                MainImage = p24.Files.Count > 0 && p24.Files.Any<BlogStorageFile>(funcMain9) ? p24.Files.FirstOrDefault<BlogStorageFile>(funcMain10).FileName : string.Empty,
                ModifiedAt = p24.ModifiedAt,
                Id = p24.Id,
                CreatedAt = p24.CreatedAt
            };
        }
        public static BlogSDto AdaptTo(this Blog p25, BlogSDto p26)
        {
            if (p25 == null)
            {
                return null;
            }
            BlogSDto result = p26 ?? new BlogSDto();
            
            result.Title = p25.Title;
            result.Slug = p25.Slug;
            result.Tags = p25.Tags;
            result.ReadingTime = p25.ReadingTime;
            result.Summery = p25.Summery;
            result.IsSuggested = p25.IsSuggested;
            result.CategoryId = p25.CategoryId;
            result.CategoryName = p25.Category == null ? null : p25.Category.Name;
            result.MainImage = p25.Files.Count > 0 && p25.Files.Any<BlogStorageFile>(funcMain9) ? p25.Files.FirstOrDefault<BlogStorageFile>(funcMain10).FileName : string.Empty;
            result.ModifiedAt = p25.ModifiedAt;
            result.Id = p25.Id;
            result.CreatedAt = p25.CreatedAt;
            return result;
            
        }
        public static Expression<Func<Blog, BlogSDto>> ProjectToSDto => p27 => new BlogSDto()
        {
            Title = p27.Title,
            Slug = p27.Slug,
            Tags = p27.Tags,
            ReadingTime = p27.ReadingTime,
            Summery = p27.Summery,
            IsSuggested = p27.IsSuggested,
            CategoryId = p27.CategoryId,
            CategoryName = p27.Category.Name,
            MainImage = p27.Files.Count > 0 && p27.Files.Any<BlogStorageFile>(f => f.IsPrimary) ? p27.Files.FirstOrDefault<BlogStorageFile>(f => f.IsPrimary).FileName : string.Empty,
            ModifiedAt = p27.ModifiedAt,
            Id = p27.Id,
            CreatedAt = p27.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 bool funcMain4(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static bool funcMain5(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static List<StorageFileSDto> funcMain6(List<BlogStorageFile> p12)
        {
            if (p12 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p12.Count);
            
            int i = 0;
            int len = p12.Count;
            
            while (i < len)
            {
                BlogStorageFile 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> funcMain7(List<BlogStorageFile> 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)
            {
                BlogStorageFile 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 BlogCategory funcMain8(Never p22, BlogCategory p23, BlogSDto p20)
        {
            BlogCategory result = p23 ?? new BlogCategory();
            
            result.Name = p20.CategoryName;
            result.Id = p20.CategoryId;
            return result;
            
        }
        
        private static bool funcMain9(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
        
        private static bool funcMain10(BlogStorageFile f)
        {
            return f.IsPrimary;
        }
    }
}