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

public class Movable : MonoBehaviour
{
    // Destination-based movement fields
    private Vector3 startPosition;
    public Vector3 targetPosition { get; private set; }
    private float time;
    private float timeToReachTarget;
    // AnimationCurve-based movement field
    [SerializeField] private AnimationCurve animationCurve;
    private float value;
    //=====================================================

    // Direction-based movement fields
    private Vector3 direction;
    private float speed;
    private bool isOnWorldSpace;
    //=====================================================

    enum Mode
    {
        Destination,
        CurveMovement,
        Direction,
        Neutral
    }

    private Mode mode = Mode.Neutral;

    void Start()
    {
        startPosition = targetPosition = transform.position;
    }

    void Update()
    {
        if (mode == Mode.Destination)
        {
            time += Time.deltaTime / timeToReachTarget;
            transform.position = Vector3.Lerp(startPosition, targetPosition, time);
        }
        else if(mode == Mode.Direction)
        {
            transform.Translate(Vector3.Normalize(direction) * speed * Time.deltaTime, (isOnWorldSpace ? Space.World : Space.Self));
        }
        else if(mode == Mode.CurveMovement)
        {
            time += Time.deltaTime / timeToReachTarget;
            value = animationCurve.Evaluate(time);
            transform.position = Vector3.Lerp(startPosition, targetPosition, value);
        }
    }

    public void SetDestination(Vector3 destination, float time)
    {
        this.time = 0;
        startPosition = transform.position;
        timeToReachTarget = time;
        targetPosition = destination;
        mode = Mode.Destination;
    }

    public void SetDestinationWithCurve(Vector3 destination, float time)
    {
        SetDestination(destination, time);
        mode = Mode.CurveMovement;
    }

    public void SetDirection(Vector3 direction, float speed, bool isOnWorldSpace)
    {
        this.direction = direction;
        this.speed = speed;
        this.isOnWorldSpace = isOnWorldSpace;
        mode = Mode.Direction;
    }

    public void UnoReverseCard()
    {
        direction = -1 * direction;
    }
}
