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

public class AntiExplosion : MonoBehaviour
{
    // List to store the sprites and their initial/final positions
    [System.Serializable]
    public class SpriteInfo
    {
        public GameObject sprite;
        public Vector3 initialPosition;
    }

    public List<SpriteInfo> sprites; // List to store sprites' data
   public float animationDuration = 2.0f; // Duration of the reverse explosion animation
    public LeanTweenType easingType = LeanTweenType.easeOutBounce; // Easing type for LeanTween

    private Vector3 screenCenter; // Center of the screen for quadrant calculations

    private void Start()
    {
        // Calculate the screen center in world space
        screenCenter = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, 0));
        screenCenter.z = 0; // Set z to 0 since it's a 2D space

        // Store initial positions and move sprites to quadrant-based scattered positions
        foreach (var spriteInfo in sprites)
        {
            spriteInfo.initialPosition = spriteInfo.sprite.transform.position; // Capture initial position
            spriteInfo.sprite.transform.position = GetRandomPositionBasedOnQuadrant(spriteInfo.initialPosition); // Move to a random position in its quadrant
        }

        // Start the reverse explosion animation using LeanTween
        AnimateSpritesWithLeanTween();
    }

    // Function to determine a random position within a specific quadrant
    private Vector3 GetRandomPositionBasedOnQuadrant(Vector3 initialPosition)
    {
        float scatterRange = 5.0f; // Adjust this value to control how far the pieces scatter
        float xPos, yPos;

        // Determine the quadrant based on the initial position relative to the screen center
        if (initialPosition.x < screenCenter.x && initialPosition.y > screenCenter.y) // Top-left quadrant
        {
            xPos = Random.Range(-scatterRange, -scatterRange / 2);
            yPos = Random.Range(scatterRange / 2, scatterRange);
        }
        else if (initialPosition.x > screenCenter.x && initialPosition.y > screenCenter.y) // Top-right quadrant
        {
            xPos = Random.Range(scatterRange / 2, scatterRange);
            yPos = Random.Range(scatterRange / 2, scatterRange);
        }
        else if (initialPosition.x < screenCenter.x && initialPosition.y < screenCenter.y) // Bottom-left quadrant
        {
            xPos = Random.Range(-scatterRange, -scatterRange / 2);
            yPos = Random.Range(-scatterRange, -scatterRange / 2);
        }
        else // Bottom-right quadrant
        {
            xPos = Random.Range(scatterRange / 2, scatterRange);
            yPos = Random.Range(-scatterRange, -scatterRange / 2);
        }

        return new Vector3(xPos, yPos, 0);
    }

    // Function to animate all sprites back to their initial positions using LeanTween
    private void AnimateSpritesWithLeanTween()
    {
        foreach (var spriteInfo in sprites)
        {
            LeanTween.move(spriteInfo.sprite, spriteInfo.initialPosition, animationDuration)
                     .setEase(easingType); // Apply the chosen easing effect
        }
    }
}