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

public class StoreManager : MonoBehaviour {
    public GameManager gm;
    [SerializeField]
    int totalPlayerHits = 0;
    [SerializeField]
    int totalEnemyHits = 0;
    [SerializeField]
    int currentPlayerHits = 0;
    [SerializeField]
    int currentEnemyHits = 0;
    public int money;

    public void addPlayerHit(bool cheat = false)
    {
        if (gm.gamePlaying)
        {
            if (cheat)
                currentPlayerHits += 50;
            else
                currentPlayerHits++;

            gm.um.setCurrentPlayerHitText(currentPlayerHits);
        }
    }

    public void addEnemyHit()
    {
        if (gm.gamePlaying)
        {
            currentEnemyHits++;
            gm.um.setCurrentEnemyHitText(currentEnemyHits);
        }
    }

    public void FlushHits()
    {
        totalPlayerHits += currentPlayerHits;
        money += currentPlayerHits;
        totalEnemyHits += currentEnemyHits;

        gm.um.setCurrentMoneyText(money);

        resetCurrentHits();
    }

    void resetCurrentHits()
    {
        currentPlayerHits = 0;
        currentEnemyHits = 0;
        gm.um.setCurrentPlayerHitText(currentPlayerHits);
        gm.um.setCurrentEnemyHitText(currentEnemyHits);
    }

    public void closeStore()
    {
        gm.StartGameplay();
    }

    public void setActiveButtons()
    {
        for(int i = 0; i < gm.um.buttons.Count; i++)
        {
            if (i == 6)
                continue;

            gm.um.buttons[i].interactable = false;
        }

        // Add Bee
        if (money >= 50 && gm.Bees.Count <= 150)
            gm.um.buttons[0].interactable = true;

        // Base Power
        if (money >= 100)
            gm.um.buttons[1].interactable = true;

        // Power Multiplier
        if (money >= 200)
            gm.um.buttons[2].interactable = true;

        // Speed
        if (money >= 80)
            gm.um.buttons[3].interactable = true;

        // Remove Fly
        if (money >= 250 && gm.Flies.Count > 5)
            gm.um.buttons[4].interactable = true;

        if (money >= 300)
            gm.um.buttons[7].interactable = true;

        // Credits 5
        gm.um.buttons[5].interactable = true;
        // Close Store Button 6
        // once wave completes it will show up
    }

    public void AddBeeUpgrade()
    {
        if(money >= 50 && gm.Bees.Count <= 150)
        {
            money -= 50;
            gm.psa.spawnPrefab();
            gm.um.setCurrentMoneyText(money);
            setActiveButtons();
        }
    }

    public void BasePowerUpgrade()
    {
        if (money >= 100)
        {
            Debug.Log("Base Power Upgrade");
            money -= 100;
            foreach (Bee b in gm.Bees)
            {
                b.Force *= 1.05f;
                b.DoUpgradeAnimation();
            }
            gm.um.setCurrentMoneyText(money);
            setActiveButtons();
        }
    }

    public void PowerMultiplierUpgrade()
    {
        if (money >= 200)
        {
            money -= 200;
            int r = Random.Range(0, gm.Bees.Count - 1);
            gm.Bees[r].ForceMultiplier *= 1.2f;
            gm.Bees[r].DoUpgradeAnimation();
            gm.um.setCurrentMoneyText(money);
            setActiveButtons();
        }
    }

    public void BeeSpeedUpgrade()
    {
        if(money >= 80)
        {
            money -= 80;
            int r = Random.Range(0, gm.Bees.Count - 1);

            if (gm.Bees[r].speed <= 6f)
            {
                gm.Bees[r].speed *= 1.163f;
                gm.Bees[r].DoUpgradeAnimation();
            }

            gm.um.setCurrentMoneyText(money);
            setActiveButtons();
        }
    }

    public void RemoveFlyUpgrade()
    {
        if(money >= 250 && gm.Flies.Count > 5)
        {
            money -= 150;

            int r = Random.Range(0, gm.Flies.Count - 1);
            Fly f = gm.Flies[r];
            gm.Flies.Remove(f);

            if (gm.em.freeFlies.Contains(f))
                gm.em.freeFlies.Remove(f);

            if (gm.em.usedFlies.Contains(f))
                gm.em.usedFlies.Remove(f);

            Destroy(f.gameObject);

            gm.um.setCurrentMoneyText(money);
            setActiveButtons();
        }
    }

    public void CreateClicker()
    {
        if (money >= 300)
        {
            money -= 300;
            gm.psa.spawnPrefab(true);
            gm.um.setCurrentMoneyText(money);
            setActiveButtons();
        }
    }
}
