using UnityEngine;
using System.Collections;

public class Peon : MonoBehaviour {

    private Vector2 m_direction;

    public Vector2 m_lastPosition;

    public bool m_isFollowing = false;
    public int m_idMaster = -1;
    public Priest m_target;

    public bool m_move = true;

    public bool m_replace = false;

    public GameManager m_manager;

    // Use this for initialization
    void Start () {
        GenerateDirection(out m_direction);
    }
	
	// Update is called once per frame
	void Update () {
        if(!m_manager.GlobalGameEnd)
        {
            Vector2 currentPosition = transform.position;

            //Movement

            if (m_move == true)
            {
                if (!m_isFollowing)
                {
                    Vector3 rPeonMove = Vector3.right * m_direction.x * Time.deltaTime + Vector3.up * m_direction.y * Time.deltaTime;
                    transform.Translate(rPeonMove);
                }
                else
                {
                    GenerateDiretionToPlayer(out m_direction);

                    Vector3 rPeonMove = Vector3.right * m_direction.x * Time.deltaTime + Vector3.up * m_direction.y * Time.deltaTime;
                    transform.Translate(rPeonMove);
                }
            }
            else
            {
                float distance = 2.0f;
                if (m_target != null)
                    distance = Vector3.Distance(m_target.transform.position, transform.position);

                if (distance > 0.66f)
                    m_move = true;
            }


            m_lastPosition = currentPosition;
        }
    }   

    void GenerateDirection(out Vector2 currentDirection)
    {
        currentDirection.x = Random.Range(-1.0f, 1.0f);
        currentDirection.y = Random.Range(-1.0f, 1.0f);

        currentDirection.Normalize();
    }

    void GenerateDiretionToPlayer(out Vector2 currentDirection)
    {
        currentDirection.x = m_target.transform.position.x - transform.position.x;
        currentDirection.y = m_target.transform.position.y - transform.position.y;
        currentDirection.Normalize();

    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if(!m_isFollowing)
        {
            GenerateDirection(out m_direction);
        }
        else
        {
            if(coll.gameObject.tag == "Priest")
            {
                m_move = false;
            }

            if (coll.gameObject.tag == "Peon")
            {
                Peon otherPeon = coll.gameObject.GetComponent<Peon>();

                if (otherPeon.m_move == false)
                    m_move = false;
            }

        }
    }

    void OnCollisionStay2D(Collision2D coll)
    {
        GenerateDirection(out m_direction);
    }

    public void Convert(Player player, Priest priest)
    {
        m_target = priest;
        m_isFollowing = true;

        m_idMaster = player.m_playerID;

        SpriteRenderer renderer;
        Sprite newSprite;

        switch (m_idMaster)
        {
            case 0:
                renderer = GetComponent<SpriteRenderer>();
                newSprite = Resources.Load<Sprite>("Sprite/Peon_Bleu");
                renderer.sprite = newSprite;
                break;
            case 1:
                renderer = GetComponent<SpriteRenderer>();
                newSprite = Resources.Load<Sprite>("Sprite/Peon_Jaune");
                renderer.sprite = newSprite;
                break;
            case 2:
                renderer = GetComponent<SpriteRenderer>();
                newSprite = Resources.Load<Sprite>("Sprite/Peon_Vert");
                renderer.sprite = newSprite;
                break;
            case 3:
                renderer = GetComponent<SpriteRenderer>();
                newSprite = Resources.Load<Sprite>("Sprite/Peon_Rouge");
                renderer.sprite = newSprite;
                break;
        }
    }

    public void Reset()
    {
        m_target = null;
        m_isFollowing = false;

        m_replace = true;

        GenerateDirection(out m_direction);

        var renderer = GetComponent<SpriteRenderer>();
        Sprite newSprite = Resources.Load<Sprite>("Sprite/Peon_Neutral");
        renderer.sprite = newSprite;
        m_idMaster = -1;
    }
}
