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

public class DistanceMonitor : MonoBehaviour {
    public Transform target;
    float lowDistanceThreshold = 3f;
    float mediumDistanceThreshold = 5f;
    private Distance last;

	void Start () {
        if (target == null) throw new System.Exception("you must specify the target!");
        this.last = calculateDistance();
	}
	
	void Update () {
        Distance current = calculateDistance();
        if (current != last)
        {
            PubSubService<PlayerAIDistanceChanged>.GetInstance().Publish(new PlayerAIDistanceChanged(current));
        }
        last = current;
	}

    private Distance calculateDistance()
    {
        float distance = (transform.position - target.position).magnitude;
        if(distance < lowDistanceThreshold)
        {
            return Distance.Low;
        } else if (distance > mediumDistanceThreshold)
        {
            return Distance.High;
        } else
        {
            return Distance.Medium;
        }
    }
}
