using RobotNet.MapShares.Property;
using RobotNet.Script.Expressions;

namespace RobotNet.MapShares;

public static class MapManagerExtensions
{
    public static Script.Expressions.ElementProperties GetElementProperties(bool isOpen, string content)
    { 
        var dbool = new Dictionary<string, bool>();
        var ddouble = new Dictionary<string, double>();
        var dint = new Dictionary<string, int>();
        var dstr = new Dictionary<string, string>();

        var elementProperties = System.Text.Json.JsonSerializer.Deserialize<ElementProperty[]>(content, JsonOptionExtends.Read) ?? [];
        
        foreach (var ep in elementProperties)
        {
            if(ep.Type == "System.Boolean" || ep.Type == "Boolean")
            {
                dbool.Add(ep.Name, bool.Parse(ep.DefaultValue));
            }
            else if(ep.Type == "System.Double" || ep.Type == "Double")
            {
                ddouble.Add(ep.Name, double.Parse(ep.DefaultValue));
            }
            else if(ep.Type == "System.Int" || ep.Type == "Int")
            {
                dint.Add(ep.Name, int.Parse(ep.DefaultValue));
            }
            else if(ep.Type == "System.String" || ep.Type == "String")
            {
                dstr.Add(ep.Name, ep.DefaultValue);
            }
            else
            {
                throw new InvalidOperationException($"Unsupported property type: {ep.Type}");
            }
        }

        return new ElementProperties(isOpen, dbool, ddouble, dint, dstr);
    }
}
