using Newtonsoft.Json;
using System;
using UnityEngine;

public sealed class PlanetData
{
    [JsonProperty("p"), JsonConverter(typeof(Vector3Converter))]
    public Vector3 Position;
    [JsonProperty("r")]
    public float Radius;
    [JsonProperty("c")]
    public string ColorCode;

    [JsonIgnore]
    public Color Color {
        get => GetColor(ColorCode);
        set => ColorCode = ColorUtility.ToHtmlStringRGB(value);
    }

    private Color GetColor(string colorCode)
    {
        if (ColorUtility.TryParseHtmlString($"#{colorCode}", out var color))
            return color;
        return Color.white;
    }
}
