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;
    //=====================================================

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

    enum Mode
    {
        Destination,
        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));
        }
    }

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

    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;
    }
}
