using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

    public enum ThreatLevel
    {
        None = 0,   // Can't harm the player
        Low = 1,        // Only scares the first player form
        Medium = 2,     // Only scares the first 2 player forms
        Max = 3         // Always scares the player
    }

    public Transform shadowForm;
    public Transform realForm;
    public LightSource[] lightSources;  // When all these are on, the enemy stops "acting", if empty, the player is alway on
    public ThreatLevel threatLevel;
    public float visibleScareDistance = 1.0f;
    public float touchScareDistance = 0.5f;

    public bool isRealForm = false;
    protected Transform player;
    protected Player playerComponent;

    public void Init()
    {
        player = GameObject.Find("/Player").transform;
        playerComponent = player.GetComponent<Player>();
    }

    protected void CheckPlayerLevel()
    {

    }

    protected void CheckLightSources()
    {
        if (lightSources.Length == 0)
            return;

        bool turnOff = true;
        foreach (LightSource source in lightSources)
        {
            if (!source.isLighted)
                turnOff = false;
        }

        if (turnOff)
            ShowRealForm();
    }

    // Scare the player if he is too close
    // Scare the player if he is looking at the enemy from a given distance
    protected void CheckPlayerPosition()
    {
        bool shouldScarePlayer = false;
        float distanceToPlayer = Vector2.Distance(new Vector2(transform.position.x, transform.position.z), new Vector2(player.position.x, player.position.z));

        if (distanceToPlayer < touchScareDistance)
            shouldScarePlayer = true;
        else if (distanceToPlayer < visibleScareDistance)
        {
            Vector3 playerForward = playerComponent.CurrentFlame.forward;
            Vector3 vectorToPlayer = player.position - transform.position;
            if (Vector3.Dot(playerForward, vectorToPlayer) < -0.8f)
                shouldScarePlayer = true;
        }

        if (shouldScarePlayer)
            playerComponent.SendMessage("Scare", (int)threatLevel);
    }

    void ShowScaryForm()
    {
        isRealForm = false;
        shadowForm.gameObject.SetActive(true);
        realForm.gameObject.SetActive(false);
    }

    void ShowRealForm()
    {
        isRealForm = true;
        shadowForm.gameObject.SetActive(false);
        realForm.gameObject.SetActive(true);
    }

    // Called by the player on all enemies when changing forms
    void UpdateForm(int level)
    {
        // First check the light sources status
        if (lightSources.Length == 0)
        {
            if (level >= (int)threatLevel)
                ShowRealForm();
            else
                ShowScaryForm();
        }
        else
        {
            bool shouldBeShadow = false;
            foreach (LightSource source in lightSources)
            {
                if (!source.isLighted)
                    shouldBeShadow = true;
            }

            if (shouldBeShadow)
            {
                if (level >= (int)threatLevel)
                    ShowRealForm();
                else
                    ShowScaryForm();
            }
            else
            {
                ShowRealForm();
            }
        }
    }
}
