using Netina.Domain.Entities.Blogs;
using Netina.Domain.Entities.ProductCategories;

namespace Netina.Core.CoreServices.WebSiteServices;

public class GetWebSiteNavBarCommandHandler : IRequestHandler<GetWebSiteNavBarCommand , List<NavMenuItem>>
{
    private readonly IRepositoryWrapper _repositoryWrapper;
    private readonly ISettingService _settingService;
    private readonly SiteSettings _siteSetting;

    public GetWebSiteNavBarCommandHandler(IRepositoryWrapper repositoryWrapper , ISettingService settingService,IOptionsSnapshot<SiteSettings> optionsSnapshot)
    {
        _repositoryWrapper = repositoryWrapper;
        _settingService = settingService;
        _siteSetting = optionsSnapshot.Value;
    }

    public async Task<List<NavMenuItem>> Handle(GetWebSiteNavBarCommand request, CancellationToken cancellationToken)
    {
        var navBarSetting = await _settingService.GetSettingAsync(nameof(NavMenuSetting), cancellationToken) as NavMenuSetting;
        var navBarItems = new List<NavMenuItem>();
        if (navBarSetting == null)
            return new List<NavMenuItem>();

        navBarSetting.NavMenuItems.ForEach(n =>
        {
            n.Children = navBarSetting.NavMenuItems.Where(ni => ni.ParentId == n.Id).ToList();
        });
        navBarSetting.NavMenuItems.Where(n => n.ParentId != default).ToList().ForEach(ni => navBarSetting.NavMenuItems.Remove(ni));
        if (navBarSetting.ShowBlogCategories)
        {

            var baseCategories = await _repositoryWrapper.SetRepository<BlogCategory>()
                .TableNoTracking
                .OrderByDescending(c => c.CreatedAt)
                .Select(BlogCategoryMapper.ProjectToSDto)
                .ToListAsync(cancellationToken);

            return baseCategories.Where(c=>c.IsMain).Select(b => new NavMenuItem
            {
                Title = b.Name,
                Url = $"{_siteSetting.WebSiteUrl}/categories/{b.Id}/{b.Slug}",
                Id = b.Id,
                ParentId = b.ParentId,
                Children =  baseCategories.Where(c=>c.ParentId == b.Id).Select(c=>new NavMenuItem
                {

                    Title = c.Name,
                    Url = $"{_siteSetting.WebSiteUrl}/categories/{c.Id}/{c.Slug}",
                    Id = c.Id,
                }).ToList()
            }).ToList();

        }
        else if (navBarSetting.ShowProductCategories)
        {
            var baseCategories = await _repositoryWrapper.SetRepository<ProductCategory>()
                .TableNoTracking
                .OrderByDescending(c => c.CreatedAt)
                .Select(ProductCategoryMapper.ProjectToSDto)
                .ToListAsync(cancellationToken);

            return baseCategories.Where(c => c.IsMain).Select(b => new NavMenuItem
            {
                Title = b.Name,
                Url = $"{_siteSetting.WebSiteUrl}/categories/{b.Id}/{b.Slug}",
                Id = b.Id,
                ParentId = b.ParentId,
                Children = baseCategories.Where(c => c.ParentId == b.Id).Select(c => new NavMenuItem
                {

                    Title = c.Name,
                    Url = $"{_siteSetting.WebSiteUrl}/categories/{c.Id}/{c.Slug}",
                    Id = c.Id,
                }).ToList()
            }).ToList();
        }
        return navBarSetting.NavMenuItems;
    }
}