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

public class EnemyManager : MonoBehaviour {
    public GameManager gm;
    public List<Fly> freeFlies;
    public List<Fly> usedFlies;
    public float actionMultiplier = 1f;
    private float nextActionTime = 0;
    private float elapsedTime = 0;
    private int availableUpgrades = 0;

    private void Start()
    {
        nextActionTime = GenerateNextActionTime();
    }

    float GenerateNextActionTime()
    {
        nextActionTime = elapsedTime + (Random.Range(0f, 20f)) / actionMultiplier;
        //nextActionTime = elapsedTime + 20f / actionMultiplier;
        
        return nextActionTime;
    }

    private void FixedUpdate()
    {
        if (gm.gamePlaying)
        {
            elapsedTime += Time.deltaTime;

            if (elapsedTime >= nextActionTime)
            {
                GenerateNextActionTime();
                ProcessHit();
            }
        }
    }

    public void ProcessHit()
    {
        if (freeFlies.Count > 0)
        {
            int pickedFlyIndex = Random.Range(0, freeFlies.Count);
            freeFlies[pickedFlyIndex].DoAttack();
        }
    }

    public void AddUpgrade(int amt = 1)
    {
        availableUpgrades += amt;
    }

    public void spendUpgrades()
    {
        Debug.Log("Upgrades Available: " + availableUpgrades);
        while(availableUpgrades > 0)
        {
            availableUpgrades--;
            Debug.Log("Upgrading Enemy");
            int r = Random.Range(0, 4);
            int r2 = 0;
            Debug.Log(r);
            switch (r)
            {
                case 0:
                    gm.esa.spawnPrefab();
                    break;

                case 1:
                    foreach (Fly f in gm.Flies)
                    {
                        f.Force *= 1.05f;
                        f.DoUpgradeAnimation();
                    }
                    break;

                case 2:
                    r2 = Random.Range(0, gm.Flies.Count - 1);
                    gm.Flies[r2].ForceMultiplier *= 1.2f;
                    gm.Flies[r2].DoUpgradeAnimation();
                    break;

                case 3:
                    r2 = Random.Range(0, gm.Flies.Count - 1);

                    if (gm.Flies[r2].speed <= 6f)
                    {
                        gm.Flies[r2].speed *= 1.163f;
                        gm.Flies[r2].DoUpgradeAnimation();
                    }
                    break;

                case 4:
                    if (gm.Bees.Count > 5)
                    {
                        Debug.Log("Remove Bee");
                        removeBee();
                    }
                    else
                        continue;
                    break;
            }
        }
    }

    void removeBee()
    {
        Debug.Log("Removing Bee");
        int r = Random.Range(0, gm.Bees.Count - 1);
        Bee b = gm.Bees[r];
        gm.Bees.Remove(b);

        if (gm.pm.freeBees.Contains(b))
            gm.pm.freeBees.Remove(b);

        if (gm.pm.usedBees.Contains(b))
            gm.pm.usedBees.Remove(b);

        Destroy(b.gameObject);
    }
}
