using System.Collections.Generic;
using System.IO;

namespace SessionZero.Data;

public class Dataset
{
    public DatasetMetadata Metadata { get; private set; }

    private Dictionary<string, SzDataObject> _objectLookup = [];

    private string _path = "";
    
    public Dataset(string name, string id, string dataType, string templateId, string iconPath = "", string description = "")
    {
        Metadata = new()
        {
            Name = name,
            Id = id,
            DatasetType = dataType,
            Description =  description,
            ObjectTemplateId =  templateId,
            Icon =  iconPath
        };
    }

    public void Save(string path)
    {
        _path = path;
        
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        
        Directory.CreateDirectory(Path.Combine(path, "objects"));

        if (!File.Exists($"{path}/dataset.meta"))
        {
            File.WriteAllText($"{path}/dataset.meta", Metadata.ToString());
        }
    }

    public void AddObject(SzDataObject obj)
    {
        var success = _objectLookup.TryAdd(obj.Id, obj);
        if (success)
        {
            File.WriteAllText($"{_path}/objects/{obj.Id}.szo", obj.ToString());
        }
    }
}