using UnityEngine;
using System.Collections;

public class scrTask : MonoBehaviour {

	public string strTaskName;
	public string strTaskDescr;

	Transform tr;

	void Awake() {

		tr = transform;

		if ( strTaskName != "PISS" ) {
			
			Camera.main.GetComponent<scrTaskManager>().liTasks.Add(tr);

		}

	}

	void OnTriggerEnter2D(Collider2D _col) {

		if ( strTaskName == "PISS" ) {

			tr.tag = _col.tag;

		}

		if ( _col.gameObject.layer == 8 && _col.tag == tr.tag ) {

			try {

				scrPlayer _scr = _col.gameObject.GetComponent<scrPlayer>();
				bool _isAlreadyInList = false;

				//Check if this Task is already in the player's list of tasks-in-range
				for (int i = 0; i < _scr.liTasks.Count; i++) {

					if ( _scr.liTasks[i] == tr ) {

						_isAlreadyInList = true;
						break;

					}

				}

				//Check if the player already completed this task
				for (int i = 0; i < _scr.liCompletedTasks.Count; i++) {

					if ( _scr.liCompletedTasks[i] == tr ) {

						_isAlreadyInList = true;
						break;

					}

				}

				if ( !_isAlreadyInList ) {

					_scr.liTasks.Add(tr);

				}

			} catch {

				Debug.LogError("Task triggered on enter by non-player object");

			}

		}

	}

	void OnTriggerExit2D(Collider2D _col) {

		if ( _col.gameObject.layer == 8 ) {

			try {

				scrPlayer _scr = _col.gameObject.GetComponent<scrPlayer>();
				_scr.liTasks.Remove(tr);

			} catch {

				Debug.LogError("Task triggered on exit by non-player object");

			}

		}

	}

}
