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

public abstract class BaseProjectile : MonoBehaviour
{
    [SerializeField] private TrailRenderer trailRenderer;
    [SerializeField] protected Transform hitVFXPrefab;
    protected float moveSpeed = 200f;
    protected Vector3 targetPosition;
    private Action onHitCompleteAction;
    public void Setup(Vector3 targetPosition, Action onHitCompleteAction) {
        this.targetPosition = targetPosition;
        SetDefaultMoveSpeed();
        SetupSpecific();
        this.onHitCompleteAction = onHitCompleteAction;
    }

    protected abstract void SetupSpecific();
    protected abstract float GetCurve();
    private void Update() {
        Vector3 moveDir = (targetPosition - transform.position).normalized;
        Vector3 moveTarget = transform.position + (moveDir * moveSpeed * Time.deltaTime);
        moveTarget.y = GetCurve();

        float distanceBeforeMoving = Vector3.Distance(transform.position, targetPosition);
        if (distanceBeforeMoving <= moveSpeed * Time.deltaTime + 0.2f) {
            moveTarget = targetPosition;
        }

        transform.position = moveTarget;
        
        if (transform.position == targetPosition) {
            OnHitEffect();
            Destroy(gameObject);
            onHitCompleteAction();
            trailRenderer.transform.parent = null;
        }
    }

    protected abstract void OnHitEffect();
    protected abstract void SetDefaultMoveSpeed();
}