/**
 * Copyright(c) Live2D Inc. All rights reserved.
 *
 * Use of this source code is governed by the Live2D Open Software license
 * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
 */
using Live2D.Cubism.Framework.UserData;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Live2D.Cubism.Framework.Json
{
    /// 
    /// Handles user data from cdi3.json.
    /// 
    [Serializable]
    public sealed class CubismUserData3Json
    {
        /// 
        /// Loads a cdi3.json asset.
        /// 
        /// cdi3.json to deserialize.
        /// Deserialized cdi3.json on success;  otherwise.
        public static CubismUserData3Json LoadFrom(string userData3Json)
        {
            return (string.IsNullOrEmpty(userData3Json))
                ? null
                : JsonUtility.FromJson(userData3Json);
        }
        /// 
        /// Loads a cdi3.json asset.
        /// 
        /// cdi3.json to deserialize.
        /// Deserialized cdi3.json on success;  otherwise.
        public static CubismUserData3Json LoadFrom(TextAsset userData3JsonAsset)
        {
            return (userData3JsonAsset == null)
                ? null
                : LoadFrom(userData3JsonAsset.text);
        }
        /// 
        /// Makes  array that was selected by .
        /// 
        /// Target object type.
        ///  array. Selected by .
        public CubismUserDataBody[] ToBodyArray(CubismUserDataTargetType targetType)
        {
            var userDataList = new List();
            for (var i = 0; i < UserData.Length; ++i)
            {
                var body = new CubismUserDataBody
                {
                    Id = UserData[i].Id,
                    Value = UserData[i].Value
                };
                switch (targetType)
                {
                    case CubismUserDataTargetType.ArtMesh:
                    {
                        // Only drawables.
                        if (UserData[i].Target == "ArtMesh")
                        {
                            userDataList.Add(body);
                        }
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return userDataList.ToArray();
        }
        #region Json Data
        /// 
        /// Json file format version.
        /// 
        [SerializeField]
        public int Version;
        /// 
        /// Additional data describing physics.
        /// 
        [SerializeField]
        public SerializableMeta Meta;
        /// 
        /// Array of user data.
        /// 
        [SerializeField]
        public SerializableUserData[] UserData;
        #endregion
        #region Json Helpers
        /// 
        /// Additional data describing user data.
        /// 
        [Serializable]
        public struct SerializableMeta
        {
            /// 
            /// Number of user data.
            /// 
            [SerializeField]
            public int UserDataCount;
            /// 
            /// Total number of user data.
            /// 
            [SerializeField]
            public int TotalUserDataCount;
        }
        /// 
        /// User data.
        /// 
        [Serializable]
        public struct SerializableUserData
        {
            /// 
            /// Type of target object.
            /// 
            [SerializeField]
            public string Target;
            /// 
            /// Name of target object.
            /// 
            [SerializeField]
            public string Id;
            /// 
            /// Value.
            /// 
            [SerializeField]
            public string Value;
        }
        #endregion
    }
}