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

public class Unit : MonoBehaviour
{
    public float MovementSpeed = 5f;

    [SerializeField] private bool m_selected = false;
    [SerializeField] private GameObject m_selectModel = null;
    private Queue<Command> m_commandList;
    private Command m_currentCommand;

    public void Enqueue(Command command)
    {
        m_commandList.Enqueue(command);
    }

    public void ClearCommandQueue()
    {
        m_commandList.Clear();
        m_currentCommand?.Stop(this);
        m_currentCommand = null;
    }

    public void Select()
    {
        m_selected = !m_selected;
        m_selectModel.SetActive(m_selected);
    }

    public void LookAt(Tile t)
    {
        transform.LookAt(MapManager.TileToWorld(t), Vector3.up);
        transform.rotation *= Quaternion.Euler(0, 180, 0);
    }

    private void Start()
    {
        m_commandList = new Queue<Command>();
    }

    private void Update()
    {
        if (m_currentCommand == null)
        {
            if (m_commandList.Count == 0)
            {
                // Ask for new commands
            }
            else
            {
                m_currentCommand = m_commandList.Dequeue();
                m_currentCommand.Start(this);
            }
        }
        else if (m_currentCommand.Excecute(this))
        {
            m_currentCommand.Stop(this);
            m_currentCommand = null;
        }
    }

    private void OnDrawGizmos()
    {
        m_currentCommand?.DebugDraw();
    }
}