using System.Collections;
using DG.Tweening;
using UnityEngine;

public class ProjectileAttack : ChargeAttack
{
    public AttackProjectile Projectile;
    [SerializeField]
    private float _throwStrength;

    public override void OnButtonReleased(PlayerController player)
    {
        float totalChargeTime = (Time.time - _startChargeTime) / ChargeTime;
        totalChargeTime = Mathf.Clamp(totalChargeTime, _timeThreshold, 1);

        AttackProjectile projectile = Instantiate(Projectile, player.ShootingPoint.position, Quaternion.identity);
        projectile.transform.right = player.ShootingPoint.right;

        projectile.Throw(player, _throwStrength * totalChargeTime, ExecuteProjectile);

        _shootTime = Time.time;

        State = AttackState.COOLDOWN;
        player.OnCooldownStarted();
    }

    public void ExecuteProjectile(PlayerController player, Vector3 spawnPos)
    {
        Wave spawnedWave = Instantiate(Wave.gameObject, spawnPos, Quaternion.identity).GetComponent<Wave>();

        OnWaveSpawned(spawnedWave);

        spawnedWave.Launch(player != null ? player.Transform.right : Vector3.zero, Ease.OutQuint);
    }
}
