using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class Player : LightSource {

    public float speed = 2.0f;
    public Transform[] flames = new Transform[1];
    public float[] flameTimers = new float[1];
    public Transform CurrentFlame
    {
        get { return currentFlame; }
    }
    public bool IsScared
    {
        get { return isScared; }
    }

    private CharacterController controller;
    private bool isScared = false;
    private Vector2 safePoint;
    private Transform currentFlame;
    private int currentForm = 0;
    private float timeLeftInForm = -1;

    private float hp = 100;

	void Start () 
    {
        isLighted = true;
        controller = gameObject.GetComponent<CharacterController>();
        currentFlame = flames[currentForm];
        safePoint = new Vector2(transform.position.x, transform.position.z);

        // GUI
        FlameGUI.SetBarMax(100);
        FlameGUI.SetBarValue(hp);
	}
	
	void Update () 
    {
        Vector3 moveDirection;

        if (!isScared && !FlameGUI.showMessage)
        {
            moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
            moveDirection.Normalize();
            controller.Move(moveDirection * Time.deltaTime * speed);
        }
        else if (isScared)
        {
            moveDirection = new Vector3(safePoint.x - transform.position.x, 0, safePoint.y - transform.position.z);
            moveDirection.Normalize();
            controller.Move(moveDirection * Time.deltaTime * speed * 0.5f);
            if (Vector2.Distance(safePoint, new Vector2(transform.position.x, transform.position.z)) < 0.5f)
            {
                isScared = false;
                currentFlame.GetComponent<Animator>().SetBool("IsScared", false);
            }
        }
        else
            moveDirection = Vector3.zero;

        if (moveDirection != Vector3.zero)
            currentFlame.forward = moveDirection;

        currentFlame.GetComponent<Animator>().SetFloat("Speed", moveDirection.magnitude);

        CheckWallObstructions();
        CheckFormTimer();
        UpdateLight();
	}

    void CheckWallObstructions()
    {
        RaycastHit hit;
        Vector3 rayDirection = Camera.main.transform.position - transform.position;

        if (Physics.Raycast(transform.position, rayDirection, out hit))
        {
            if (hit.transform.tag == "Wall")
            {
                hit.transform.SendMessage("MakeTransparent");
            }
        }
    }

    void CheckFormTimer()
    {
        if (timeLeftInForm == -1)
            return;

        timeLeftInForm -= Time.deltaTime;
        if (timeLeftInForm <= 0)
        {
            ActivateForm(0);
        }
    }

    void Scare(int level)
    {
        if (isScared)
            return;

        hp -= 35;
        FlameGUI.SetBarValue(hp);

        if (hp <= 0)
            GameObject.Find("/LevelManager").SendMessage("Lose");

        if (level > currentForm)
        {
            isScared = true;
            currentFlame.GetComponent<Animator>().SetBool("IsScared", true);
        }
    }

    void ActivateForm(int form)
    {
        if (form < flames.Length - 1)
        {
            currentFlame.gameObject.SetActive(false);
            currentForm = form;
            currentFlame = flames[currentForm];
            currentFlame.gameObject.SetActive(true);

            if (form > 0)
            {
                hp += 50; hp = hp > 100 ? 100 : hp;
                FlameGUI.SetBarValue(hp);
                timeLeftInForm = flameTimers[form];
            }
            else
                timeLeftInForm = -1;

            GameObject.Find("/Enemies").BroadcastMessage("UpdateForm", (int) form, SendMessageOptions.DontRequireReceiver);
        }
    }

    void UpdateLight()
    {
        Light light = transform.GetComponentInChildren<Light>();
        light.spotAngle = 70f + Random.Range(0f, 20f);
        light.intensity = 2f + Random.Range(0f, 1f);
    }

    void UpdateSafePoint(Vector3 newPoint)
    {
        ActivateForm(1);
        safePoint = new Vector2(newPoint.x, newPoint.z);
    }
}
