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

public class Upgrade : MonoBehaviour
{
    public TextMeshProUGUI upgradeName;
    public TextMeshProUGUI upgradeDescription;
    public TextMeshProUGUI upgradeCost;
    public TextMeshProUGUI upgradeValue;
    public Image upgradeIcon;

    private UpgradeManager.UpgradeOption upgrade;

    public GameObject player;


    private void Awake()
    {
        player = ShapeSelect.selectedShape;
    }

    private void Start()
    {
        GetUpgrade();

        // Experimental bonus to get the player started
        ScoreManager.instance.score = 25;
    }

    // Invoke this function to fetch a new random upgrade
    public void GetUpgrade()
    {
        // Generate a new upgrade
        upgrade = UpgradeManager.Instance.GenerateRandomUpgrade();

        int upgradeValueRounded = (int)upgrade.Value;

        upgradeName.text = upgrade.name;
        upgradeDescription.text = upgrade.description;
        upgradeCost.text = "$" + upgrade.Cost.ToString();

        // If were dealing with floats, ensure it doesn't get rounded to 0
        if (upgrade.type == UpgradeManager.UpgradeType.FireRate)
        {
            upgradeValue.text = "+ " + upgrade.Value.ToString("F2") + " " + upgrade.name;
        }
        else
        {
            upgradeValue.text = "+ " + upgradeValueRounded.ToString() + " " + upgrade.name;
        }

        upgradeIcon.sprite = upgrade.icon;
    }

    // Invoke this function to buy an upgrade
    public void BuyUpgrade()
    {
        // Play the sound effect
        AudioManager.instance.PlayClickSound();

        // if the upgrade type is bullet damage
        if (upgrade.type == UpgradeManager.UpgradeType.BulletDamage)
        {
            // Deduct The money
            ScoreManager.instance.score -= upgrade.Cost;

            // Apply the upgrade
            player.GetComponent<Weapon>().bulletDamage += (int)upgrade.Value;
        }

        // if the upgrade type is increased fire rate
        if (upgrade.type == UpgradeManager.UpgradeType.FireRate)
        {
            // Deduct The money
            ScoreManager.instance.score -= upgrade.Cost;

            // Apply the upgrade
            player.GetComponent<Weapon>().shootCooldown -= upgrade.Value;

            // Ensure we put limits on the upgrade
            if (player.GetComponent<Weapon>().shootCooldown < 0.15f)
            {
                player.GetComponent<Weapon>().shootCooldown = 0.15f;
            }
        }

        // if the upgrade type is increased movement speed
        if (upgrade.type == UpgradeManager.UpgradeType.MovementSpeed)
        {
            // Deduct The money
            ScoreManager.instance.score -= upgrade.Cost;

            // Apply the upgrade
            player.GetComponent<PlayerController>().maxSpeed += (int)upgrade.Value;

            // Ensure we put limits on the upgrade
            if (player.GetComponent<PlayerController>().maxSpeed > 10)
            {
                player.GetComponent<PlayerController>().maxSpeed = 10;
            }
        }

        // if the upgrade type is increased fire rate
        if (upgrade.type == UpgradeManager.UpgradeType.ShieldCapacity)
        {
            // Deduct The money
            ScoreManager.instance.score -= upgrade.Cost;

            // Apply the upgrade
            player.GetComponentInChildren<Shield>().maxShieldHealth += (int)upgrade.Value;
            player.GetComponentInChildren<Shield>().currentShieldHealth = player.GetComponentInChildren<Shield>().maxShieldHealth;
        }

        // if the upgrade type is increased rotation speed
        if (upgrade.type == UpgradeManager.UpgradeType.RotationSpeed)
        {
            // Deduct The money
            ScoreManager.instance.score -= upgrade.Cost;

            // Apply the upgrade
            player.GetComponent<PlayerController>().rotateSpeed += (int)upgrade.Value;
        }

        // if the upgrade type is increased dash speed
        if (upgrade.type == UpgradeManager.UpgradeType.DashSpeed)
        {
            // Deduct The money
            ScoreManager.instance.score -= upgrade.Cost;

            // Apply the upgrade
            player.GetComponent<PlayerController>().dashSpeedBoost += (int)upgrade.Value;
        }

        // Move on to the next wave
        WaveManager.instance.StartNextWave();
    }


    private void Update()
    {
        if (ScoreManager.instance.score < upgrade.Cost)
        {
            // Disable the buy button if we don't have enough money
            this.gameObject.GetComponentInChildren<Button>().interactable = false;
        }
        else
        {
            // Enable the buy button if we have enough money for it
            this.gameObject.GetComponentInChildren<Button>().interactable = true;
        }
    }
}
