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

public class Store : MonoBehaviour
{
    public GameObject noMoneyText;

    public Button poge_grey;
    public Button poge_silver;
    public Button poge_gold;
    public Button poge_diamond;
    // Start is called before the first frame update
    void Start()
    {
        if(DataManager.selectedPogeLevel == 0){
            SelectGrey();
        }else if(DataManager.selectedPogeLevel == 1){
            SelectSilver();
        }else if(DataManager.selectedPogeLevel == 2){
            SelectGold();
        }else if(DataManager.selectedPogeLevel == 3){
            SelectDiamond();
        }
        poge_grey.onClick.AddListener(SelectGrey);
        poge_silver.onClick.AddListener(SelectSilver);
        poge_gold.onClick.AddListener(SelectGold);
        poge_diamond.onClick.AddListener(SelectDiamond);

        Refresh();
    }


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

    void SelectGrey(){
        poge_grey.transform.GetChild(0).gameObject.SetActive(true);
        poge_silver.transform.GetChild(0).gameObject.SetActive(false);
        poge_gold.transform.GetChild(0).gameObject.SetActive(false);
        poge_diamond.transform.GetChild(0).gameObject.SetActive(false);
        DataManager.SelectPoge(0); 

        Refresh();

    }

    void SelectSilver(){
        if(!DataManager.pogePurchased.Contains(0)){
            if(DataManager.total < silverPrice){
                noMoneyText.SetActive(true);

                return;
            }else{
                DataManager.total =(int)(DataManager.total- silverPrice);
                DataManager.PurchasePoge("silver");

            }
        }
        poge_grey.transform.GetChild(0).gameObject.SetActive(false);

        poge_silver.transform.GetChild(0).gameObject.SetActive(true);
        poge_gold.transform.GetChild(0).gameObject.SetActive(false);
        poge_diamond.transform.GetChild(0).gameObject.SetActive(false);
        DataManager.SelectPoge(1); 

        Refresh();

    }
    public float silverPrice = 50000;
    public float goldPrice =    500000;
    public float diamondPrice = 5000000;
    void SelectGold(){

        if(!DataManager.pogePurchased.Contains(1)){
            if(DataManager.total < goldPrice){
                noMoneyText.SetActive(true);

                return;
            }else{
                DataManager.total =(int)(DataManager.total- goldPrice);
                DataManager.PurchasePoge("gold");

            }
        }
        poge_grey.transform.GetChild(0).gameObject.SetActive(false);

        poge_silver.transform.GetChild(0).gameObject.SetActive(false);
        poge_gold.transform.GetChild(0).gameObject.SetActive(true);
        poge_diamond.transform.GetChild(0).gameObject.SetActive(false);
        DataManager.SelectPoge(2); 
        Refresh();

    }void SelectDiamond(){
        if(!DataManager.pogePurchased.Contains(2)){
            if(DataManager.total < diamondPrice){
                noMoneyText.SetActive(true);

                return;
            }else{
                DataManager.total =(int)(DataManager.total- diamondPrice);
                DataManager.PurchasePoge("diamond");
            }
        }
        poge_grey.transform.GetChild(0).gameObject.SetActive(false);

        poge_silver.transform.GetChild(0).gameObject.SetActive(false);
        poge_gold.transform.GetChild(0).gameObject.SetActive(false);
        poge_diamond.transform.GetChild(0).gameObject.SetActive(true);
        DataManager.SelectPoge(3); 

        Refresh();
    }

    // Update is called once per frame
    void Refresh()
    {
        MenuManager.Refresh();
        noMoneyText.SetActive(false);
        if(DataManager.pogePurchased.Contains(0)){
            poge_silver.transform.GetChild(1).GetChild(1).GetComponent<Text>().text = "Purchased";
        }else{
            poge_silver.transform.GetChild(1).GetChild(1).GetComponent<Text>().text = "$" + (int)silverPrice;
            
        }
        if(DataManager.pogePurchased.Contains(1)){
            poge_gold.transform.GetChild(1).GetChild(1).GetComponent<Text>().text = "Purchased";
        }else{
            poge_gold.transform.GetChild(1).GetChild(1).GetComponent<Text>().text = "$" + (int)goldPrice;
            
        }
        if(DataManager.pogePurchased.Contains(2)){
            poge_diamond.transform.GetChild(1).GetChild(1).GetComponent<Text>().text = "Purchased";
        }else{
            poge_diamond.transform.GetChild(1).GetChild(1).GetComponent<Text>().text = "$" + (int)diamondPrice;
            
        }
    }
}
