/**
 * 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.Json;
using UnityEngine;
namespace Live2D.Cubism.Framework.MotionFade
{
    public class CubismFadeMotionData : ScriptableObject
    {
        /// 
        /// Name of motion.
        /// 
        [SerializeField]
        public string MotionName;
        /// 
        /// Time to fade in.
        /// 
        [SerializeField]
        public float FadeInTime;
        /// 
        /// Time to fade out.
        /// 
        [SerializeField]
        public float FadeOutTime;
        /// 
        /// Parameter ids.
        /// 
        [SerializeField]
        public string[] ParameterIds;
        /// 
        /// Parameter curves.
        /// 
        [SerializeField]
        public AnimationCurve[] ParameterCurves;
        /// 
        /// Fade in time parameters.
        /// 
        [SerializeField]
        public float[] ParameterFadeInTimes;
        /// 
        /// Fade out time parameters.
        /// 
        [SerializeField]
        public float[] ParameterFadeOutTimes;
        /// 
        /// Motion length.
        /// 
        [SerializeField]
        public float MotionLength;
        /// 
        /// Create CubismFadeMotionData from CubismMotion3Json.
        /// 
        /// Motion3json as the creator.
        /// Motion name of interest.
        /// Length of target motion.
        /// Whether the original work flow or not.
        /// Whether it is a call from the model json.
        /// Fade data created based on motion3json.
        public static CubismFadeMotionData CreateInstance(
            CubismMotion3Json motion3Json, string motionName, float motionLength,
             bool shouldImportAsOriginalWorkflow = false, bool isCallFromModelJson = false)
        {
            var fadeMotion = CreateInstance();
            var curveCount = motion3Json.Curves.Length;
            fadeMotion.ParameterIds = new string[curveCount];
            fadeMotion.ParameterFadeInTimes = new float[curveCount];
            fadeMotion.ParameterFadeOutTimes = new float[curveCount];
            fadeMotion.ParameterCurves = new AnimationCurve[curveCount];
            return CreateInstance(fadeMotion, motion3Json, motionName, motionLength, shouldImportAsOriginalWorkflow, isCallFromModelJson);
        }
        /// 
        /// Put motion3json's fade information back into fade motion data.
        /// 
        /// Instance containing fade information.
        /// Target motion3json.
        /// Motion name of interest.
        /// Motion length.
        /// Whether the original work flow or not.
        /// Whether it is a call from the model json.
        /// Fade data created based on fademotiondata.
        public static CubismFadeMotionData CreateInstance(
            CubismFadeMotionData fadeMotion, CubismMotion3Json motion3Json, string motionName, float motionLength,
             bool shouldImportAsOriginalWorkflow = false, bool isCallFormModelJson = false)
        {
            fadeMotion.MotionName = motionName;
            fadeMotion.MotionLength = motionLength;
            fadeMotion.FadeInTime = (motion3Json.Meta.FadeInTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeInTime;
            fadeMotion.FadeOutTime = (motion3Json.Meta.FadeOutTime < 0.0f) ? 1.0f : motion3Json.Meta.FadeOutTime;
            for (var i = 0; i < motion3Json.Curves.Length; ++i)
            {
                var curve = motion3Json.Curves[i];
                // In original workflow mode, skip add part opacity curve when call not from model3.json.
                if (curve.Target == "PartOpacity" && shouldImportAsOriginalWorkflow && !isCallFormModelJson)
                {
                    continue;
                }
                fadeMotion.ParameterIds[i] = curve.Id;
                fadeMotion.ParameterFadeInTimes[i] = (curve.FadeInTime < 0.0f) ? -1.0f : curve.FadeInTime;
                fadeMotion.ParameterFadeOutTimes[i] = (curve.FadeOutTime < 0.0f) ? -1.0f : curve.FadeOutTime;
                fadeMotion.ParameterCurves[i] = new AnimationCurve(CubismMotion3Json.ConvertCurveSegmentsToKeyframes(curve.Segments));
            }
            return fadeMotion;
        }
    }
}