using UnityEngine;
using System.Collections;


public class PawnScript : MonoBehaviour {

	bool pawnAlive = true;
	public float velocity = 0.2f;
    public Transform modelTransform;
    //public GameObject bloodSpatter;

    Vector3 lastPos;
    public bool isGood;

    public AudioClip[] pawnEnters;
    public AudioClip[] pawnExits;
    public AudioClip[] pawnDies;


	public Vector2 Direction 
	{
		get 
		{ 

			Vector2 tDir = Vector2.zero;
            tDir.x = modelTransform.forward.x;
            tDir.y = modelTransform.forward.z;

			return(tDir);
		}

		set 
		{

			Vector2 tDir = value;
			Vector3 tForward = Vector3.zero;
			tForward.x = tDir.x;
			tForward.y = 0;
			tForward.z = tDir.y;
            modelTransform.forward = tForward;
		}
	}



	// Use this for initialization
	void Start () {
        //PlayRandomMusic(pawnEnters);
	}
	
	// Update is called once per frame
	void Update () {
		if (pawnAlive) 
		{
			MovePawnForward();
            CallDestinationTile();
            CallCurrentTile();
            SavePreviousFramePos();
        }
	}

	void MovePawnForward()
	{
        Vector3 tPos = modelTransform.forward;
		transform.position = Vector3.MoveTowards(transform.position, transform.position+tPos, velocity * Time.deltaTime); 
	}

	void SavePreviousFramePos()
	{
        lastPos = transform.position;
	}

	void CallDestinationTile()
	{

		if (Mathf.Floor (lastPos.x) != Mathf.Floor (transform.position.x)
		    ||
		    Mathf.Floor (lastPos.z) != Mathf.Floor (transform.position.z))
		{
            //Debug.Log("New destination: " + TargetTile());
            if (GameManager.Instance.map.GetTile(TargetTile()).IsOn) {
                //Debug.Log("Calling starts towards of type " + GameManager.Instance.map.GetTile(TargetTile()).Type.GetType().ToString());
                GameManager.Instance.map.GetTile(TargetTile()).Type.PawnStartsTowards(this);
            }
		}
	}

	void CallCurrentTile()
	{
        if (GameManager.Instance.map.GetTile(CurrentTile()).IsOn) {
            //Debug.Log("Calling PawnEnters of type " + GameManager.Instance.map.GetTile(CurrentTile()).Type.GetType().ToString());
            GameManager.Instance.map.GetTile(CurrentTile()).Type.PawnEnters(this);
        }
	}

	private Vector2 TargetTile()
	{
        return (new Vector2(Mathf.Round(transform.position.x + modelTransform.forward.x), Mathf.Round(transform.position.z + modelTransform.forward.z)));  
	}

	private Vector2 CurrentTile()
	{
		return (new Vector2(Mathf.Round(transform.position.x), Mathf.Round(transform.position.z)));  
	}


	public Vector2 PawnTile()
	{
		Vector2 pawnPos = Vector2.zero;

		pawnPos.x = Mathf.Round (transform.position.x);
		pawnPos.y = Mathf.Round (transform.position.z);

		return(pawnPos);
	}

	public void Killed() {

        LevelManager.instance.PawnKilled(this);
        PlayRandomMusic(pawnDies);
		Destroy (this.gameObject);
	}
	
	public void ArrivedAtObjective(TileType objectiveTileType) {
        if (isGood)
        {
            PlayRandomMusic(pawnExits);
            LevelManager.instance.PawnArrivedObjective(); 
        }
        else
        { LevelManager.instance.BadPawnEnteredObjective(); }
		Destroy (this.gameObject);

	}

    public void DarkenShader(bool toDarken)
    {
        if (toDarken)
        {
            Renderer[] allRenderers = this.gameObject.GetComponentsInChildren<Renderer>();
            foreach (Renderer rend in allRenderers)
            {
                Color tempColor = rend.material.color;

                tempColor.b *= 0.5f;
                tempColor.g *= 0.5f;
                tempColor.r = 1f;

                rend.material.color = tempColor;
            }
        }
    }


    private void PlayRandomMusic(AudioClip[] options) {
        AudioSource.PlayClipAtPoint(options[Random.Range(0, options.Length)], Camera.main.transform.position);
    }

}
