using UnityEngine;
using System.Collections;

public class Shadow : MonoBehaviour {

    public Transform shadowPrefab;
    public float radius = 2.0f;
    public float spaceBetweenSmoke = 0.5f;
    public bool[] drawQuadrant = { true, true, true, true };

    public LightSource[] lightSources;

    private Player player;

    void Start()
    {
        player = GameObject.Find("/Player").transform.GetComponent<Player>();

        for (float currRad = spaceBetweenSmoke; currRad <= radius; currRad += spaceBetweenSmoke)
        {
            float size = (radius / currRad) * (15 * spaceBetweenSmoke);
            for (float i = 1; i < 90; i += size)
            {
                Transform currentShadow;
                float x = Mathf.Cos(Mathf.Deg2Rad * i) * currRad;
                float y = Mathf.Sin(Mathf.Deg2Rad * i) * currRad;
                float height = 0.5f;

                if (drawQuadrant[0])
                {
                    currentShadow = GameObject.Instantiate(shadowPrefab, new Vector3(transform.position.x + x, height, transform.position.z + y), Quaternion.identity) as Transform;
                    currentShadow.parent = transform;
                }
                if (drawQuadrant[1])
                {
                    currentShadow = GameObject.Instantiate(shadowPrefab, new Vector3(transform.position.x + x, height, transform.position.z - y), Quaternion.identity) as Transform;
                    currentShadow.parent = transform;
                }
                if (drawQuadrant[2])
                {
                    currentShadow = GameObject.Instantiate(shadowPrefab, new Vector3(transform.position.x - x, height, transform.position.z + y), Quaternion.identity) as Transform;
                    currentShadow.parent = transform;
                }
                if (drawQuadrant[3])
                {
                    currentShadow = GameObject.Instantiate(shadowPrefab, new Vector3(transform.position.x - x, height, transform.position.z - y), Quaternion.identity) as Transform;
                    currentShadow.parent = transform;
                }
            }
        }
    }

    void FixedUpdate()
    {
        // Check if the player is completely surrounded
        float dist = Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(player.transform.position.x, player.transform.position.z));
        if (dist + player.safeRadius < radius)
        {
            bool shouldScare = true;
            foreach (LightSource source in lightSources)
            {
                if (source.isLighted)
                {
                    dist = Vector2.Distance(new Vector2(source.transform.position.x, source.transform.position.z), new Vector2(player.transform.position.x, player.transform.position.z));
                    if ((dist - player.safeRadius - source.safeRadius) < 0)
                        shouldScare = false;
                }
            }

            if (shouldScare && gameObject.transform.childCount > 0)
                player.SendMessage("Scare", (int) Enemy.ThreatLevel.Max);
        }

        // Children auto destroy when all lights are on, if this happens, destroy this object
        //if (gameObject.transform.childCount == 0)
            //GameObject.Destroy(gameObject);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.DrawWireSphere(transform.position, radius);
    }
}
