using UnityEngine;
using System.Collections;

public class AnimationChest : MonoBehaviour {

    private string action = "action";
    private Animator animator;
    public bool status = false;
    public bool done = false;
    public bool hover = false;
    private Vector3 point;
    private Rect t;
    public int idItem;

    public bool key = false;
    public int keyId;

    public string msg = "";

    void Start() {
        this.animator = this.gameObject.GetComponent<Animator>();
    }

    public void setAction() {
        if (!done) {
            Player p = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
            if (key)
            {
                if (p.containsItem(keyId))
                {
                    p.receiveItem(this.idItem);
                    done = !done;
                    msg = "(Empty)";
                }
                else
                {
                    msg = "(Closed)";

                }
            }
            else
            {
                p.receiveItem(this.idItem);
                done = !done;
                msg = "(Empty)";
            }
            
       }

        if (!done) return;

        this.animator.SetTrigger(action);
        this.status = !this.status;
    }

    void OnCollisionEnter(Collision other) {
        if (other.gameObject.tag == "Player")
        {
            hover = true;
        }


    }

    void OnCollisionExit(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            hover = false;
        }
    }

    void OnGUI()
    {
        if (hover)
        {
            this.point = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
            this.t = new Rect(point.x, point.y, 300, 300);
            
            GUI.Label(this.t, "<size=" + ((50)) + ">" + "Press action to open." + msg + "</size>");
        }
    }
}
