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

public class Bee : MonoBehaviour {
    PlayerManager pm;
    HoneyJar hj;
    public float Force;
    public float ForceMultiplier;
    public Vector2 VectorOffset;
    private IEnumerator MyCoRoutine;
    private IEnumerator MyCoRoutine2;
    private bool animating = false;
    public float speed = 1;
    public GameObject RippleSprite;
    public GameObject UpgradeSprite;
    private bool upgradeAnimating = false;

    // Use this for initialization
    private void Awake()
    {
        hj = GameObject.FindObjectOfType<HoneyJar>().GetComponent<HoneyJar>();
        pm = GameObject.FindObjectOfType<PlayerManager>().GetComponent<PlayerManager>();
    }

    void Start () {
        RippleSprite.GetComponent<SpriteRenderer>().color = Color.clear;
        UpgradeSprite.GetComponent<SpriteRenderer>().color = Color.clear;
    }

    public void DoUpgradeAnimation()
    {
        Vector2 endPosition = new Vector2(UpgradeSprite.transform.position.x, UpgradeSprite.transform.position.y + 0.2f);
        MyCoRoutine = UpgradeAnimation(UpgradeSprite.transform.position, endPosition, 0.5f);
        StartCoroutine(MyCoRoutine);
    }

    public void DoAttack()
    {
        float usableSpeed = 1f*(1-(1*speed)/8);
        Vector2 endPosition = new Vector2(hj.transform.position.x - ((hj.GetComponent<SpriteRenderer>().bounds.size.x) / 2), transform.position.y);
        MyCoRoutine = AttackMovement(transform.position, endPosition, usableSpeed);
        StartCoroutine(MyCoRoutine);
    }

    private void FixedUpdate()
    {
        if(!animating) UpdatePosition();
    }

    private void UpdatePosition()
    {
        Vector2 newPosition;
        newPosition.x = (float)((hj.transform.position.x - 3) + VectorOffset.x);
        newPosition.y = (float)(VectorOffset.y);
        transform.position = newPosition;
    }

    IEnumerator AttackMovement(Vector2 startPosition, Vector2 endPosition, float time)
    {
        //Debug.Log("Start Attack Movement");
        pm.usedBees.Add(this);
        pm.freeBees.Remove(this);

        animating = true;
        float elapsedTime = 0;

        while(elapsedTime < time)
        {
            transform.position = Vector2.Lerp(startPosition, endPosition, (elapsedTime / time));
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }

        elapsedTime = 0;
        RippleSprite.GetComponent<SpriteRenderer>().color = Color.red;
        hj.Hit(true, Force * ForceMultiplier, "Bee");

        while (elapsedTime < 0.075f)
        {
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }

        RippleSprite.GetComponent<SpriteRenderer>().color = Color.clear;
        elapsedTime = 0;

        while (elapsedTime < time)
        {
            transform.position = Vector2.Lerp(endPosition, startPosition, (elapsedTime / time));
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }

        animating = false;

        pm.usedBees.Remove(this);
        pm.freeBees.Add(this);
        //Debug.Log("End Attack Movement");
    }

    IEnumerator UpgradeAnimation(Vector2 startPosition, Vector2 endPosition, float time)
    {
        if (!upgradeAnimating)
        {
            upgradeAnimating = true;
            float elapsedTime = 0;

            UpgradeSprite.GetComponent<SpriteRenderer>().color = Color.white;

            while (elapsedTime < time)
            {
                UpgradeSprite.transform.position = Vector2.Lerp(startPosition, endPosition, (elapsedTime / time));
                elapsedTime += Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }

            UpgradeSprite.GetComponent<SpriteRenderer>().color = Color.clear;
            UpgradeSprite.transform.position = startPosition;
            upgradeAnimating = false;
        }
    }
}
