/* Copyright (C) Itseez3D, Inc. - All Rights Reserved
* You may not use this file except in compliance with an authorized license
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* UNLESS REQUIRED BY APPLICABLE LAW OR AGREED BY ITSEEZ3D, INC. IN WRITING, SOFTWARE DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED
* See the License for the specific language governing permissions and limitations under the License.
* Written by Itseez3D, Inc. <support@avatarsdk.com>, June 2026
*/

using System;
using System.IO;
using UnityEngine;

namespace AvatarSDKMove
{
	public class GlbAnimationLoader : MonoBehaviour
	{
		public string glbFilePath = "model.glb";

		public GameObject sourceModel;

		private void Start()
		{
			string fullPath = Path.Combine(Application.streamingAssetsPath, glbFilePath);

			AnimationClip animationClip = LoadAnimation(fullPath);
			if (animationClip == null)
				return;

			Animation animation = sourceModel.GetComponent<Animation>();
			if (animation == null)
				animation = sourceModel.AddComponent<Animation>();

			animation.AddClip(animationClip, animationClip.name);
			animation.clip = animationClip;
			animation.Play();
		}

		public AnimationClip LoadAnimation(string filePath)
		{
			try
			{
				return GlbAnimationParser.Parse(File.ReadAllBytes(filePath));
			}
			catch (Exception ex)
			{
				Debug.LogError($"Failed to load GLB from {filePath}: {ex.Message}");
				return null;
			}
		}
	}
}
