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

public class PointAtTarget : MonoBehaviour {


    public float maxArrowDistanceFromPlayer;
    public float minArrowDistanceFromTarget;

    public enum Target { none, tile, life, star, god};
    public Target target;
    public Renderer mr;
    public Transform arrowTransform;
	
	void Update () {
        Transform targetTransform;
        targetTransform = null;
        switch (target)
        {
            case Target.none:
                break;
            case Target.tile:
                float minDistTile = 100;
                foreach (var col in Physics.OverlapSphere(transform.position, 10)) {
                    var tile = col.GetComponentInParent<Tile>();
                    if (tile == null) continue;
                    var dist = Vector3.Distance(tile.transform.position, transform.position);
                    if (tile != null && tile.world != Game.player && dist < minDistTile)
                    {
                        minDistTile = dist;
                        targetTransform = tile.transform;
                    }
                }
                break;
            case Target.life:
                float minDistLife = float.MaxValue;
                foreach (var tile in Game.lifeContainingTiles)
                {
                    var dist = Vector3.Distance(tile.transform.position, transform.position);
                    if (tile.world != Game.player && dist < minDistLife)
                    {
                        minDistLife = dist;
                        targetTransform = tile.transform;
                    }
                }
                break;
            case Target.star:
                float minDistStar = float.MaxValue;
                foreach (var star in Game.stars)
                {
                    var dist = Vector3.Distance(star.transform.position, transform.position);
                    if (dist < minDistStar)
                    {
                        minDistStar = dist;
                        targetTransform = star.transform;
                    }
                }
                break;
            case Target.god:
                float minDistGod = float.MaxValue;
                foreach (var god in Game.gods)
                {
                    var dist = Vector3.Distance(god.transform.position, transform.position);
                    if (dist < minDistGod)
                    {
                        minDistStar = dist;
                        targetTransform = god.transform;
                    }
                }
                break;
        }

        if (targetTransform == null)
            mr.enabled = false;
        else
        {
            mr.enabled = true;
            transform.LookAt(targetTransform);
            arrowTransform.localPosition = new Vector3(0, 0,
                Mathf.Min(Vector3.Distance(transform.position, targetTransform.position) - minArrowDistanceFromTarget, maxArrowDistanceFromPlayer));
        }
	}
}
