/* 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>, August 2026
*/

using UnityEngine;
using UnityEngine.UI;

namespace AvatarSDKMove
{
	public class RuntimeAnimationGenerationSample : MonoBehaviour
	{
		public AnimationGenerator animationGenerator;
		public GameObject targetModel;

		public InputField promptInputField;
		public Button generateButton;
		public Text statusText;

		public float duration = 5.0f;

		private void Awake()
		{
			generateButton.onClick.AddListener(OnGenerateClicked);
			animationGenerator.onProgress.AddListener(SetStatus);
			animationGenerator.onFailed.AddListener(OnFailed);

			if (!AuthUtils.HasSession())
			{
				generateButton.interactable = false;
				SetStatus("Not authenticated. Sign in with Avatar SDK Move -> Account in the Editor before building.");
			}
		}

		private void OnDestroy()
		{
			generateButton.onClick.RemoveListener(OnGenerateClicked);
			animationGenerator.onProgress.RemoveListener(SetStatus);
			animationGenerator.onFailed.RemoveListener(OnFailed);
		}

		private async void OnGenerateClicked()
		{
			string prompt = promptInputField.text;
			if (string.IsNullOrEmpty(prompt))
			{
				SetStatus("Describe the animation you want to create.");
				return;
			}

			generateButton.interactable = false;
			try
			{
				await animationGenerator.Generate(targetModel, prompt, duration);
			}
			finally
			{
				generateButton.interactable = true;
			}
		}

		private void OnFailed(string message)
		{
			SetStatus("Generation failed: " + message);
		}

		private void SetStatus(string message)
		{
			if (statusText != null)
				statusText.text = message;
		}
	}
}
