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

public class Janitor : MonoBehaviour {

    public Transform LaundryRoom;
    public int kidGatherAmount = 10;
    public MeshRenderer trashLegs;
    public GameObject kidDecal;

    private int kidCount = 0;
    private NavMeshAgent agent;
    private JanitorState state = JanitorState.Available;
    private Animator anim;
    public LayerMask kidMask;

    private float AnimTimer;
    private Queue<KidController> kidQueue;

    [Header("Sounds")]
    public AudioClip PickupKid;
    enum JanitorState
    {
        Available,
        InPursuit,
        ReturningHome,
        GrabbingKid
    }

	// Use this for initialization
	void Start ()
    {
        kidQueue = new Queue<KidController>();
        agent = GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>();
        AnimTimer = 0.0f;
        if (transform.position != LaundryRoom.position)
        {
            agent.destination = LaundryRoom.position;
            state = JanitorState.ReturningHome;
        }
	}
	
	// Update is called once per frame
	void Update () { 
        anim.SetFloat("Speed", agent.velocity.magnitude);

        trashLegs.enabled = kidCount > 0;

        switch (state)
        {
            case JanitorState.Available:
                CheckMouse();
                if (kidQueue.Count != 0)
                {
                    agent.destination = kidQueue.Peek().transform.position;                
                    state = JanitorState.InPursuit;
                }
                break;
            case JanitorState.InPursuit:
                CheckMouse();
                if (Vector3.Distance(transform.position, agent.destination) < 3f)
                {
                    agent.isStopped = true;
                    state = JanitorState.GrabbingKid;
                    AudioSource.PlayClipAtPoint(PickupKid, transform.position);
                    anim.SetTrigger("GrabKid");
                    AnimTimer = 0.0f;
                }
                break;
            case JanitorState.ReturningHome:
                CheckMouse();
                if(kidQueue.Count != 0)
                {
                    agent.destination = kidQueue.Peek().transform.position;
                    state = JanitorState.InPursuit;
                    break;
                }

                if(Vector3.Distance(transform.position, LaundryRoom.position) < 3f)
                {
                    state = JanitorState.Available;
                    agent.isStopped = true;
                    GameManager.instance.HarvestedKids += (kidGatherAmount * kidCount);
                    kidCount = 0;
                }
                break;
            case JanitorState.GrabbingKid:
                CheckMouse();
                AnimTimer += Time.deltaTime;
                if(AnimTimer >= 1.0f)
                {
                    kidCount++;
                    
                    Destroy(kidQueue.Dequeue().gameObject);

                    if(kidQueue.Count != 0)
                    {
                        agent.destination = kidQueue.Peek().transform.position;
                        state = JanitorState.InPursuit;
                    }
                    else
                    {
                        agent.destination = LaundryRoom.position;
                        state = JanitorState.ReturningHome;
                    }
                    
                    agent.isStopped = false;
                }
                break;
        }   
	}

    void CheckMouse()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hit, 1000f, kidMask , QueryTriggerInteraction.Ignore))
            {
                if (hit.collider.CompareTag("kid"))
                {

                    KidController kid = hit.collider.GetComponent<KidController>();
                    if (kid.state == KidController.KidState.Stunned && !kidQueue.Contains(kid))
                    {
                        kid.StunTimeBeforeDeath = 100f;
                        kidQueue.Enqueue(kid);
                        Instantiate(kidDecal, kid.transform.position + Vector3.down, kid.transform.rotation, kid.transform);
                        if (state == JanitorState.Available)
                        {
                            agent.destination = kid.transform.position;
                        }
                        agent.isStopped = false;
                        state = JanitorState.InPursuit;
                    }
                }
            }
        }
    }
}
