using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clicker : MonoBehaviour {
    public GameManager gm;
    private int life = 350;
    private float elapsedTime = 0f;
    private float clickAtTime = 3f;
    private bool enrolled = false;
    public Vector2 vectorOffset;
    bool animating = false;
    public TextMesh lifeCountText;

    private void Start()
    {
        gm = GameObject.FindObjectOfType<GameManager>();
        UpdateLifeCounterText();
    }

    private void Update()
    {
        if(!enrolled)
        {
            gm.BeeClickers.Add(this);
            enrolled = true;
        }

        if (gm.gamePlaying)
        {
            if (elapsedTime > clickAtTime)
                DoClick();

            elapsedTime += Time.deltaTime;
        }
    }

    void UpdateLifeCounterText()
    {
        lifeCountText.text = life.ToString();
    }

    void DoClick()
    {
        Debug.Log(life);
        if (!animating)
        {
            animating = true;
            elapsedTime = 0;

            if (life <= 0)
            {
                Debug.Log("Destroying");
                Destroy(this.gameObject);
                gm.BeeClickers.Remove(this);
            }

            life--;
            UpdateLifeCounterText();
            gm.am.PlayClip("Swoop");
            gm.pm.ProcessHit();

            animating = false;
        }
    }
}
