using Hawkeye.VisionBuilder.Workflow.ColorStudioClasses.Filters;

namespace Hawkeye.VisionBuilder.Workflow.ColorStudioClasses;

public class CategoryItem:IFilterParent
{
    public string Name { get; set; }
    public List<IFilter> Filters { get; set; } = new List<IFilter>();

    public void Save(BinaryWriter writer)
    {
        writer.Write(Name);
        writer.Write(Filters.Count);
        foreach (var filter in Filters)
        {
            var type = filter.GetType();
            writer.Write(type.FullName);
            filter.Save(writer);
        }
    }

    public static CategoryItem Load(BinaryReader reader)
    {
        var category = new CategoryItem();
        category.Name = reader.ReadString();
        var count = reader.ReadInt32();
        for (int i = 0; i < count; i++)
        {
            var typeName = reader.ReadString();
            var type = Type.GetType(typeName);
            var filter = (IFilter)Activator.CreateInstance(type);
            filter.Load(reader);
            filter.Parent = category;
            category.Filters.Add(filter);
        }
        return category;
    }

    public override string ToString()
    {
        return Name;
    }
}

