﻿// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2025 Kybernetik //

#if LITE
#if ! UNITY_EDITOR
#pragma warning disable CS0618 // Type or member is obsolete (for Layers in Animancer Lite).
#endif
#endif
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace Animancer.Samples.Blending
{
    /// <summary>Demonstrates how to use <see cref="WeightedMaskLayers"/>.</summary>
    /// 
    /// <remarks>
    /// <strong>Sample:</strong>
    /// <see href="https://kybernetik.com.au/animancer/docs/samples/blending/weighted-mask-layers">
    /// Weighted Mask Layers</see>
    /// </remarks>
    /// 
    /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Blending/WeightedMaskLayerButtons
    /// 
    [AddComponentMenu(Strings.SamplesMenuPrefix + "Blending - Weighted Mask Layer Buttons")]
    [AnimancerHelpUrl(typeof(WeightedMaskLayerButtons))]
    public class WeightedMaskLayerButtons : MonoBehaviour
    {
        /************************************************************************************************************************/

        [SerializeField] private WeightedMaskLayers _Layers;
        [SerializeField] private Button _ButtonTemplate;
        [SerializeField] private float _ButtonSpacing = 10;
        [SerializeField] private float _FadeDuration = 0.25f;

        /************************************************************************************************************************/

        protected virtual void Awake()
        {
            if (_Layers == null || _Layers.Layers == null)
            {
                _ButtonTemplate.gameObject.SetActive(false);
                return;
            }

            int buttonIndex = 0;
            int layerCount = _Layers.LayerCount;
            for (int iLayer = 1; iLayer < layerCount; iLayer++)
            {
                int layerIndex = iLayer;
                int groupCount = _Layers.Definition.GroupCount;
                for (int iGroup = 0; iGroup < groupCount; iGroup++)
                {
                    int groupIndex = iGroup;
                    AddButton(
                        _ButtonTemplate,
                        $"Layer {iLayer} fade to group {groupIndex}",
                        buttonIndex++,
                        _ButtonSpacing,
                        () => _Layers.FadeWeights(layerIndex, groupIndex, _FadeDuration));
                }
            }
        }

        /************************************************************************************************************************/

        public static Button AddButton(
            Button template,
            string text,
            int index,
            float spacing,
            UnityAction onClick)
        {
            if (index > 0)
            {
                template = Instantiate(template, template.transform.parent);

                RectTransform rectTransform = template.GetComponent<RectTransform>();
                Vector2 position = rectTransform.anchoredPosition;
                position.y -= index * (rectTransform.sizeDelta.y + spacing);
                rectTransform.anchoredPosition = position;
            }

            template.onClick.AddListener(onClick);
            template.GetComponentInChildren<Text>().text = text;
            template.name = text;

            return template;
        }

        /************************************************************************************************************************/
    }
}