using UnityEngine;
using System.Collections;

public class PlayerInteract : MonoBehaviour {

    enum InteractState { active, disabled, beingUsed };
    InteractState interactState = InteractState.disabled;

    private SpriteRenderer spriteRenderer;

    private Transform targetTransform;

    private Transform cameraTransform;

    public float iconDistance = 1.0f;

    private float countdownTime = 1.5f;

    public GameObject countdownCircle;
    private InteractingIndicator interIndic;

	// Use this for initialization
	void Start () {
        Interact.OnUseStart += UseStart;
        Interact.OnUseStop += UseStop;
        Interact.OnUseFinish += UseFinish;
        Interact.OnSpinnerActivate += Activate;
        Interact.OnSpinnerDeactivate += Deactivate;
        spriteRenderer = GetComponent<SpriteRenderer>();
        cameraTransform = Camera.main.transform;
        interIndic = countdownCircle.GetComponent<InteractingIndicator>();
	}
	
	// Update is called once per frame
	void Update () {
        if(interactState != InteractState.disabled)
        {
            Vector3 heading = targetTransform.position - cameraTransform.position;
            float distance = heading.magnitude;
            Vector3 direction = heading / distance;
            transform.position = direction * iconDistance + cameraTransform.position;
            transform.rotation = Quaternion.LookRotation(direction);
        }
	}

    /**
    USE START
    Start animations for holding down use button
    **/
    void UseStart()
    {
        if(interactState == InteractState.active)
        {
            interactState = InteractState.beingUsed;
            interIndic.StartUse(countdownTime);
            //Debug.Log("HAHAHAHAHA");
        }
    }

    /**
    USE STOP
    Stop animations for holding down use button
    **/
    void UseStop()
    {
        if(interactState == InteractState.beingUsed)
        {
            interactState = InteractState.active;
            interIndic.StopUse();
            //Debug.Log("STOPPED USING");
        }
    }

    /**
    ACTIVATE
    Start animations for activating use button and making it visible
    **/
    void Activate(Transform newTargetTransform, float countdownTime)
    {
        if (interactState == InteractState.disabled)
        {
            interactState = InteractState.active;
            //Debug.Log("IN RANGE");
            spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            targetTransform = newTargetTransform;
            this.countdownTime = countdownTime;
        }
    }

    /**
    DEACTIVATE
    Start animations for deactivating use button and making it invisible
    **/
    void Deactivate()
    {
        if(interactState == InteractState.beingUsed)
        {
            UseStop();
            interIndic.StopUse();
        }
        if (interactState == InteractState.active)
        {
            interactState = InteractState.disabled;
            //Debug.Log("OUT OF RANGE");
            spriteRenderer.color = new Color(1.0f, 1.0f, 1.0f, 0f);
        }
    }

    void UseFinish()
    {
        if(interactState == InteractState.beingUsed)
        {
            Deactivate();
            //Debug.Log("FINISHED");
        }
    }
}
