using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Mapster.Models;
using NetinaShop.Domain.Dtos.LargDtos;
using NetinaShop.Domain.Dtos.SmallDtos;
using NetinaShop.Domain.Entities.Blogs;

namespace NetinaShop.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,
                Tags = p1.Tags,
                ReadingTime = p1.ReadingTime,
                Summery = p1.Summery,
                IsSuggested = p1.IsSuggested,
                CategoryId = p1.CategoryId,
                Files = funcMain1(p1.Files),
                Id = p1.Id
            };
        }
        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.Tags = p3.Tags;
            result.ReadingTime = p3.ReadingTime;
            result.Summery = p3.Summery;
            result.IsSuggested = p3.IsSuggested;
            result.CategoryId = p3.CategoryId;
            result.Files = funcMain2(p3.Files, result.Files);
            result.Id = p3.Id;
            return result;
            
        }
        public static Expression<Func<BlogLDto, Blog>> ProjectToBlog => p7 => new Blog()
        {
            Title = p7.Title,
            Content = p7.Content,
            Tags = p7.Tags,
            ReadingTime = p7.ReadingTime,
            Summery = p7.Summery,
            IsSuggested = p7.IsSuggested,
            CategoryId = p7.CategoryId,
            Files = p7.Files.Select<StorageFileSDto, BlogStorageFile>(p8 => new BlogStorageFile()
            {
                Name = p8.Name,
                FileLocation = p8.FileLocation,
                FileName = p8.FileName,
                IsHeader = p8.IsHeader,
                IsPrimary = p8.IsPrimary,
                FileType = p8.FileType,
                Id = p8.Id
            }).ToList<BlogStorageFile>(),
            Id = p7.Id
        };
        public static BlogLDto AdaptToLDto(this Blog p9)
        {
            return p9 == null ? null : new BlogLDto()
            {
                Title = p9.Title,
                Content = p9.Content,
                Tags = p9.Tags,
                ReadingTime = p9.ReadingTime,
                Summery = p9.Summery,
                IsSuggested = p9.IsSuggested,
                CategoryId = p9.CategoryId,
                CategoryName = p9.Category == null ? null : p9.Category.Name,
                Files = funcMain3(p9.Files),
                Id = p9.Id
            };
        }
        public static BlogLDto AdaptTo(this Blog p11, BlogLDto p12)
        {
            if (p11 == null)
            {
                return null;
            }
            BlogLDto result = p12 ?? new BlogLDto();
            
            result.Title = p11.Title;
            result.Content = p11.Content;
            result.Tags = p11.Tags;
            result.ReadingTime = p11.ReadingTime;
            result.Summery = p11.Summery;
            result.IsSuggested = p11.IsSuggested;
            result.CategoryId = p11.CategoryId;
            result.CategoryName = p11.Category == null ? null : p11.Category.Name;
            result.Files = funcMain4(p11.Files, result.Files);
            result.Id = p11.Id;
            return result;
            
        }
        public static Expression<Func<Blog, BlogLDto>> ProjectToLDto => p15 => new BlogLDto()
        {
            Title = p15.Title,
            Content = p15.Content,
            Tags = p15.Tags,
            ReadingTime = p15.ReadingTime,
            Summery = p15.Summery,
            IsSuggested = p15.IsSuggested,
            CategoryId = p15.CategoryId,
            CategoryName = p15.Category.Name,
            Files = p15.Files.Select<BlogStorageFile, 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
        };
        public static Blog AdaptToBlog(this BlogSDto p17)
        {
            return p17 == null ? null : new Blog()
            {
                Title = p17.Title,
                Content = p17.Content,
                Tags = p17.Tags,
                ReadingTime = p17.ReadingTime,
                Summery = p17.Summery,
                IsSuggested = p17.IsSuggested,
                CategoryId = p17.CategoryId,
                Category = new BlogCategory()
                {
                    Name = p17.CategoryName,
                    Id = p17.CategoryId
                },
                Id = p17.Id
            };
        }
        public static Blog AdaptTo(this BlogSDto p18, Blog p19)
        {
            if (p18 == null)
            {
                return null;
            }
            Blog result = p19 ?? new Blog();
            
            result.Title = p18.Title;
            result.Content = p18.Content;
            result.Tags = p18.Tags;
            result.ReadingTime = p18.ReadingTime;
            result.Summery = p18.Summery;
            result.IsSuggested = p18.IsSuggested;
            result.CategoryId = p18.CategoryId;
            result.Category = funcMain5(new Never(), result.Category, p18);
            result.Id = p18.Id;
            return result;
            
        }
        public static BlogSDto AdaptToSDto(this Blog p22)
        {
            return p22 == null ? null : new BlogSDto()
            {
                Title = p22.Title,
                Content = p22.Content,
                Tags = p22.Tags,
                ReadingTime = p22.ReadingTime,
                Summery = p22.Summery,
                IsSuggested = p22.IsSuggested,
                CategoryId = p22.CategoryId,
                CategoryName = p22.Category == null ? null : p22.Category.Name,
                HeaderFileName = p22.Files.Count > 0 && p22.Files.Any<BlogStorageFile>(funcMain6) ? p22.Files.FirstOrDefault<BlogStorageFile>(funcMain7).FileName : string.Empty,
                Id = p22.Id
            };
        }
        public static BlogSDto AdaptTo(this Blog p23, BlogSDto p24)
        {
            if (p23 == null)
            {
                return null;
            }
            BlogSDto result = p24 ?? new BlogSDto();
            
            result.Title = p23.Title;
            result.Content = p23.Content;
            result.Tags = p23.Tags;
            result.ReadingTime = p23.ReadingTime;
            result.Summery = p23.Summery;
            result.IsSuggested = p23.IsSuggested;
            result.CategoryId = p23.CategoryId;
            result.CategoryName = p23.Category == null ? null : p23.Category.Name;
            result.HeaderFileName = p23.Files.Count > 0 && p23.Files.Any<BlogStorageFile>(funcMain6) ? p23.Files.FirstOrDefault<BlogStorageFile>(funcMain7).FileName : string.Empty;
            result.Id = p23.Id;
            return result;
            
        }
        public static Expression<Func<Blog, BlogSDto>> ProjectToSDto => p25 => new BlogSDto()
        {
            Title = p25.Title,
            Content = p25.Content,
            Tags = p25.Tags,
            ReadingTime = p25.ReadingTime,
            Summery = p25.Summery,
            IsSuggested = p25.IsSuggested,
            CategoryId = p25.CategoryId,
            CategoryName = p25.Category.Name,
            HeaderFileName = p25.Files.Count > 0 && p25.Files.Any<BlogStorageFile>(f => f.IsHeader) ? p25.Files.FirstOrDefault<BlogStorageFile>(f => f.IsHeader).FileName : string.Empty,
            Id = p25.Id
        };
        
        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
                });
                i++;
            }
            return result;
            
        }
        
        private static List<BlogStorageFile> funcMain2(List<StorageFileSDto> p5, List<BlogStorageFile> p6)
        {
            if (p5 == null)
            {
                return null;
            }
            List<BlogStorageFile> result = new List<BlogStorageFile>(p5.Count);
            
            int i = 0;
            int len = p5.Count;
            
            while (i < len)
            {
                StorageFileSDto item = p5[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
                });
                i++;
            }
            return result;
            
        }
        
        private static List<StorageFileSDto> funcMain3(List<BlogStorageFile> p10)
        {
            if (p10 == null)
            {
                return null;
            }
            List<StorageFileSDto> result = new List<StorageFileSDto>(p10.Count);
            
            int i = 0;
            int len = p10.Count;
            
            while (i < len)
            {
                BlogStorageFile 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<BlogStorageFile> 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)
            {
                BlogStorageFile 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 BlogCategory funcMain5(Never p20, BlogCategory p21, BlogSDto p18)
        {
            BlogCategory result = p21 ?? new BlogCategory();
            
            result.Name = p18.CategoryName;
            result.Id = p18.CategoryId;
            return result;
            
        }
        
        private static bool funcMain6(BlogStorageFile f)
        {
            return f.IsHeader;
        }
        
        private static bool funcMain7(BlogStorageFile f)
        {
            return f.IsHeader;
        }
    }
}