using UnityEngine;
using System.Collections;

[AddComponentMenu("MGUB Event Editor/NPC Event")]
[RequireComponent(typeof(Entity))]
public class NPCEvent : InteractiveTrigger {
    private Entity NPCEntity;

    private void Awake() {
        NPCEntity = GetComponent<Entity>();
    }

    private void Reset() {
        interactionName = "Talk";
        automaticMovementDeactivation = true;
        activationDistance = 2f;
        activationOffset = new Vector3(0.0f, 0.5f, 0.0f);
        angularView = 180.0f;
        looping = true;
    }

    public override void OnEventStart() {
        base.OnEventStart();
        if (automaticMovementDeactivation && ThirdPersonController.main != null) {
            Action a1 = new Action(this);
            a1.type = Action.types.enable_movement;
            a1.boolValue1 = false;
            a1.Execute();
            Action a2 = new Action(this);
            a2.type = Action.types.look_at_character;
            a2.entity1 = NPCEntity;
            a2.Execute();
            Action a3 = new Action(this);
            a3.type = Action.types.look_at;
            a3.entity1 = ThirdPersonController.main.entity;
            a3.transform1 = NPCEntity.head;
            a3.Execute();
        }
    }

    public override void OnEventEnd() {
        base.OnEventEnd();
        if (automaticMovementDeactivation && ThirdPersonController.main != null) {
            Action a1 = new Action(this);
            a1.type = Action.types.enable_movement;
            a1.boolValue1 = true;
            a1.Execute();
            Action a2 = new Action(this);
            a2.type = Action.types.stop_looking;
            a2.entity1 = NPCEntity;
            a2.Execute();
            Action a3 = new Action(this);
            a3.type = Action.types.stop_looking;
            a3.entity1 = ThirdPersonController.main.entity;
            a3.Execute();
        }
    }

    public Entity GetEntity() {
        return NPCEntity;
    }
}
