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

public class UpgradeMenu : MonoBehaviour
{
    public Transform speedBar;
    public Transform fuelBar;
    public Transform inAirBar;
    public Button btnUpgradeSpeed,btnUpgradeFuel,btnUpgradeInAir;



    void Start(){
        Refresh();


        btnUpgradeSpeed.onClick.AddListener(UpgradeSpeed);
        btnUpgradeFuel.onClick.AddListener(UpgradeFuel);
        btnUpgradeInAir.onClick.AddListener(UpgradeInAir);
    }

    public void Show(){
        gameObject.SetActive(true);
        Refresh();
    }

    void UpgradeSpeed(){
        DataManager.SpeedLevel++;
        DataManager.Money -= speedPrice;
        Refresh();
    }

    void UpgradeFuel(){
        DataManager.FuelLevel++;
        DataManager.Money-= fuelPrice;
        Refresh();
    } void UpgradeInAir(){
        DataManager.inAirLevel++;
        DataManager.Money-=inAirPrice;
        Refresh();
    }
    int speedPrice;
    int fuelPrice ;
    int inAirPrice ;

    int priceIncrementRate = 100;
    void Refresh(){
        int speedLevel = DataManager.SpeedLevel;
        int fuelLevel = DataManager.FuelLevel;
        int inAirLevel = DataManager.inAirLevel;

        speedPrice = 0;
        fuelPrice = 0;
        inAirPrice = 0;

        bool speedAvailable = true;
        bool fuelAvailable = true;
        bool inAirAvailable = true;
        for(int i=0; i<speedBar.childCount; i++){
            if(speedBar.GetChild(i).GetComponent<Image>()!=null){
                
                if( i <= speedLevel ){
                    speedBar.GetChild(i).GetComponent<Image>().color = Color.green;
                    speedPrice += priceIncrementRate;
                }else{
                    speedBar.GetChild(i).GetComponent<Image>().color = Color.gray;
                }
            }   

            if(i == speedBar.childCount-1){
                if(i == speedLevel){
                    speedAvailable =false;
                }
            }
        }

        for(int i=0; i<fuelBar.childCount; i++){
            if(fuelBar.GetChild(i).GetComponent<Image>()!=null){
                
                if( i <= fuelLevel ){
                    fuelBar.GetChild(i).GetComponent<Image>().color = Color.green;
                    fuelPrice += priceIncrementRate;
                }else{
                    fuelBar.GetChild(i).GetComponent<Image>().color = Color.gray;
                }
            }   

            if(i == fuelBar.childCount-1){
                if(i == fuelLevel){
                    fuelAvailable =false;
                }
            }
        }
        for(int i=0; i<inAirBar.childCount; i++){
            if(inAirBar.GetChild(i).GetComponent<Image>()!=null){
                
                if( i <= inAirLevel ){
                    inAirBar.GetChild(i).GetComponent<Image>().color = Color.green;
                    inAirPrice += priceIncrementRate;
                }else{
                    inAirBar.GetChild(i).GetComponent<Image>().color = Color.gray;
                }
            }   

            if(i == inAirBar.childCount-1){
                if(i == inAirLevel){
                    inAirAvailable =false;
                }
            }
        }

        btnUpgradeSpeed.image.color = speedAvailable ? Color.green : Color.gray;
        btnUpgradeFuel.image.color = fuelAvailable ? Color.green : Color.gray;
        btnUpgradeInAir.image.color = inAirAvailable ? Color.green : Color.gray;

        if(speedPrice > DataManager.Money){
            speedAvailable=false;
            btnUpgradeSpeed.image.color = Color.red;
        }

        if(fuelPrice > DataManager.Money){
            fuelAvailable=false;
            btnUpgradeFuel.image.color = Color.red;
        }

        if(inAirPrice > DataManager.Money){
            inAirAvailable=false;
            btnUpgradeInAir.image.color = Color.red;
        }


        btnUpgradeSpeed.interactable = speedAvailable;
        btnUpgradeFuel.interactable = fuelAvailable;
        btnUpgradeInAir.interactable = inAirAvailable;


        btnUpgradeSpeed.transform.GetChild(1).GetComponent<Text>().text = $"${speedPrice}";
        btnUpgradeFuel.transform.GetChild(1).GetComponent<Text>().text = $"${fuelPrice}";
        btnUpgradeInAir.transform.GetChild(1).GetComponent<Text>().text = $"${inAirPrice}";

        MenuManager.Refresh();
    }

}
