using System;
using System.Collections.Generic;
using System.Linq;
using MudBlazor;
using SharpRss;
using SharpRss.Models;
using ToolQit;

namespace WebSharpRSS.Models
{
    public class TreeItemData
    {
        public TreeItemData(CategoryModel categoryModel)
        {
            CategoryModel = categoryModel;
            Title = categoryModel.Name;
            Icon = categoryModel.Icon == string.Empty ? Icons.Material.Filled.RssFeed : categoryModel.Icon;
            HasChildren = CategoryModel.Syndications.Count > 0;
            if (HasChildren)
                CategoryModel.Syndications.Select(x => x.ItemCount).ToList().ForEach(i => TotalItems += i);
        }

        public TreeItemData(SyndicationModel syndicationModel)
        {
            SyndicationModel = syndicationModel;
            Title = syndicationModel.Title ?? string.Empty;
            if (SyndicationModel.EncodedUrl == null) return;
            FaviconUrl = string.Format(Caretaker.Settings["Paths"].GetString("FaviconResolveUrl"), new Uri(SyndicationManager.DecodeUrlFromBase64(SyndicationModel.EncodedUrl)).Host);
            TotalItems = SyndicationModel.ItemCount;
        }
        public readonly CategoryModel? CategoryModel;
        public readonly SyndicationModel? SyndicationModel;

        public HashSet<TreeItemData>? Children { get; set; }
        public string Title { get; set; }
        public bool IsSelected { get; set; }
        public string? Icon { get; set; }
        public string? FaviconUrl { get; set; }
        public bool HasChildren { get; set; }
        public bool Loading { get; set; }
        public bool IsExpanded { get; set; }
        public int TotalItems { get; set; }
    }
}