using System;
using System.Globalization;
using UnityEngine;

namespace pdinklag
{
    /// <summary>
    /// A localization asset.
    /// </summary>
    public class LocalizationAsset : ScriptableObject
    {
#pragma warning disable 649
        [Serializable] private class Header { public string lang; }
#pragma warning restore 649

        [SerializeField]
        private string _lang;

        [SerializeField]
        [TextArea]
        private string _json;

        /// <summary>
        /// The JSON content of the localization.
        /// </summary>
        public string JsonContent
        {
            get => _json;
            set
            {
                var header = new Header();
                JSON.FillObject(header, value);
                
                _lang = header.lang;
                new CultureInfo(_lang); // throw if unsupported
                _json = value;
            }
        }

        /// <summary>
        /// Attempts to retrieve the culture associated to the "lang" field of the JSON root element.
        /// </summary>
        public CultureInfo Culture => _lang.Length > 0 ? new CultureInfo(_lang) : CultureInfo.InvariantCulture;
    }
}
