using DG.Tweening;
using UnityEngine;

public class Wave : MonoBehaviour
{
    public const float STRENGTH_PER_SECOND = 50;

    public WaveParameters Parameters;

    private float _currentStrength;

    private Rigidbody2D _rigidbody;

    void Awake()
    {
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    void Start()
    {
        _currentStrength = Parameters.LifeTime * STRENGTH_PER_SECOND;
    }

    public void SetDuration(float duration)
    {
        Parameters.LifeTime = duration;
        _currentStrength = Parameters.LifeTime * STRENGTH_PER_SECOND;
    }

    public void Launch(Vector3 direction, Ease scaleEasing = Ease.OutQuad)
    {
        transform.right = direction;

        Ease easing = Ease.OutQuad;

        float currentSpeed = Parameters.Speed;
        DOTween.To(() => currentSpeed, x => _rigidbody.velocity = transform.right * x , 0, Parameters.LifeTime).SetEase(easing);

        transform.DOScale(Parameters.FinalScale, Parameters.LifeTime).SetEase(scaleEasing);

        Transform s = transform.FindChild("Sprite");
        if (s != null)
            s.GetComponent<SpriteRenderer>().DOFade(0, Parameters.LifeTime).SetEase(scaleEasing);

        DOTween.To(() => _currentStrength, x => _currentStrength = x, 0, Parameters.LifeTime).SetEase(scaleEasing).OnComplete(() => Destroy(gameObject));
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        other.GetComponent<PlayerController>().OnWaveCollision(_currentStrength);
    }
}

[System.Serializable]
public struct WaveParameters
{
    public float LifeTime;
    public float Speed;
    public float FinalScale;
}