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

public class Player : MonoBehaviour
{
    public Booth[] _path;
    private Vector3[] _locInNode;
    public static int currTime;
    private int _vaccineCount = 1;
    private int _flagCount = 3;
    public Booth firstBooth;
    public bool isMoving;
    private Animator _animator;

    public GameObject movementArrowPrefab;

    private List<GameObject> arrows = new List<GameObject>();

    // Use this for initialization
    void Start ()
    {
        _animator = gameObject.GetComponent<Animator>();
        _animator.Play("Idle");
        gameObject.tag = "Player";
        currTime = 0;
        _path[currTime] = firstBooth;
        moved();
    }
	
	// Update is called once per frame
	void Update ()
    {
	}

    public void move(Booth newbooth)
    {
        //Phsyical Movement!
        //gameObject.transform.position = newbooth.transform.position;
        _path[currTime] = newbooth;
        StartCoroutine(phsyicalMovement(newbooth.transform.position + new Vector3(0.0f, 1.5f), true));
        //regen our arrows
        moved();
    }

	public bool IsMoving
	{
		get { return isMoving; }
		set
		{
			isMoving = value;
			var gm = GameObject.FindObjectOfType<GameManager>();
			gm.PlayMusic(isMoving);
		}
	}


    void moved()
    {
        //clear all the exsisting arrows
        foreach(GameObject arrow in arrows)
        {
            Destroy(arrow);
        }

        //Create the new arrows
        foreach(Booth booth in _path[currTime].adjacentBooths)
        {
            GameObject arrow = Instantiate(movementArrowPrefab, booth.transform.position, Quaternion.identity) as GameObject;
            arrows.Add(arrow);
            arrow.GetComponent<ArrowBoothHolder>().representing = booth;
        }
    }

    public void Vaccinate(Npc target)
    {
        target.ReceiveVaccine();
        _vaccineCount--;
    }

    public bool FlagNPC(Npc target)
    {
        if (_flagCount > 0)
        {
            target.Flag = true;
            _flagCount--;
            return true;
        }
        else
        {
            return false;
        }
    }

    IEnumerator phsyicalMovement(Vector3 endPos, bool forward)
    {
        IsMoving = true;

        //store start and end points
        Vector3 startPos = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
        int numberOfSteps = 240;

        float startTime = Time.time;
        float journeyLength = Vector3.Distance(startPos, endPos);

        _animator.Play("Walk");

        if (endPos.x <= startPos.x && gameObject.transform.rotation.y == 0)
        {
            gameObject.transform.Rotate(Vector3.up, 180);
        }
        else if (endPos.x > startPos.x && gameObject.transform.rotation.y == 1)
        {
            gameObject.transform.Rotate(Vector3.up, -180);
        }

        for (int i = 0; i < numberOfSteps; i++)
        {
            float increment = (float)i / (float)numberOfSteps;
            gameObject.transform.position = Vector3.Lerp(startPos, endPos, increment);
            yield return new WaitForEndOfFrame();
        }

        gameObject.transform.position = endPos;

        IsMoving = false;
        _animator.Play("Idle");
    }
}
