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


public class Employee : MonoBehaviour {

    [HideInInspector]
    public List<GameObject> tasks = new List<GameObject>();
    public int taskSeconds = 10;

    public int frustration;
    [HideInInspector]
	public int papersInFax;

    private NavMeshAgent agent;
    private Transform goal;
	private GameObject main;

    private int taskTicks;
    private int taskTicksLeft;


    public void AddTaskSeconds(int seconds)
    {
        taskTicksLeft += seconds * 50;
    }


    public void ChangeGoal(Transform g)
    {
        agent.destination = g.position;
    }


    private void Start()
    {
        main = GameObject.FindGameObjectWithTag("Main");
        agent = GetComponent<NavMeshAgent>();

        taskTicks = taskSeconds * 50;
        taskTicksLeft = 0;

        frustration = 0;
        papersInFax = 0;;
    }


    private void GoToTransform(Transform t)
    {
        agent.destination = t.position;
    }


    private void AddTextCard()
    {
        GameObject.FindGameObjectWithTag("Typing Handler").GetComponent<TypingManager>().AddTextCard(gameObject);
        papersInFax++;
    }


	private void HandleSpecialTasks(GameObject task)
	{
        if (task.tag == "Task-Fax")
        {
            AddTextCard();
        }
	}


    protected bool pathComplete()
    {
        if (Vector3.Distance(agent.destination, agent.transform.position) <= 3f)
        {
            return true;
        }

        return false;
    }


    private void FixedUpdate ()
	{
        if (pathComplete())
        {
            taskTicksLeft--;

            if (taskTicksLeft == Convert.ToInt32(taskTicks / 2))
            {
                frustration += papersInFax;
            }
        }

        if (taskTicksLeft <= 0)
        {
            if (tasks.Count > 0)
            {
                GoToTransform(tasks[0].transform);
                HandleSpecialTasks(tasks[0]);
                tasks.RemoveAt(0);
                taskTicksLeft = taskTicks;
            }
        }
	}

}
