using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnapBackAni : MonoBehaviour
{
    [System.Serializable]
    public class PieceInfo
    {
        public GameObject piece; // The piece to be animated
        public Vector3 originalPosition; // The original position to snap back to
    }

    public List<PieceInfo> pieces = new List<PieceInfo>(); // List of pieces and their original positions
    public float snapDuration = 0.5f; // Duration of each snap animation
    public float delayBetweenSnaps = 0.2f; // Delay between each piece snapping
    public LeanTweenType ease;
    private void Start()
    {
        LeanTween.alphaCanvas(GetComponent<CanvasGroup>(), 1f, 0.1f);
        // Start the snapping animation
        StartCoroutine(SnapPiecesBack());
    }

    private IEnumerator SnapPiecesBack()
    {
        // Loop through each piece in the list
        foreach (var pieceInfo in pieces)
        {
            CanvasGroup canvasGroup = pieceInfo.piece.GetComponent<CanvasGroup>();

            if (canvasGroup != null)
            {
                LeanTween.alphaCanvas(canvasGroup, 1f, 0.1f);
            }
            // Snap the piece to its original position using LeanTween
            LeanTween.move(pieceInfo.piece, pieceInfo.originalPosition, snapDuration)
                .setEase(ease); // Add a smooth ease-out effect

            // Wait for the specified delay before snapping the next piece
            yield return new WaitForSeconds(delayBetweenSnaps);
        }
    }
    [ContextMenu("Setup Pieces From Children")]
    private void SetupPiecesFromChildren()
    {
        pieces.Clear(); // Clear the list to avoid duplicates

        foreach (Transform child in transform) // Loop through all child GameObjects
        {
            pieces.Add(new PieceInfo
            {
                piece = child.gameObject,                // Set the piece to the child GameObject
                originalPosition = child.position        // Set the original position to the child's current position
            });
        }
    }
    
}