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

public class ShopPanel : UICommon
{
    private int allCoin;

    public GameObject[] Objs;
    public override void Start()
    {
        base.Start();
       // Objs = new GameObject[9];
      //  Objs = this.transform.GetChild(2).transform.GetComponentsInChildren<GameObject>();
        GameDebug.Log($"当前有道具：{DiegGaoGaoGame.instance.gameUser.items.GetListStr()}");


        DiegGaoGaoGame.instance.gameUser.AddItem(0);
        DiegGaoGaoGame.instance.gameUser.AddItem(1);
        DiegGaoGaoGame.instance.gameUser.AddItem(2);
        Refresh();
    }

    void Refresh() 
    {
        int[] CurrentItems = DiegGaoGaoGame.instance.gameUser.GetItems();
        int[] CurrentItem = CurrentItems.Distinct().ToArray();
        int[] result = CurrentItem.OrderBy(x => x).ToArray();
   
        for (int i = 0; i < result.Length; i++)
        {
            Objs[i].transform.GetChild(1).gameObject.SetActive(true);
            Objs[i].transform.GetChild(2).gameObject.SetActive(false);
        }
        for (int i= result.Length;i <9 ; i++) 
        {

            if (i ==result.Length) 
            {
                Objs[i].transform.GetChild(1).gameObject.SetActive(false);
                Objs[i].transform.GetChild(2).gameObject.SetActive(true);
                Objs[i].transform.GetChild(2).gameObject.transform.GetChild(1).gameObject.SetActive(true);
            }
            Objs[i].transform.GetChild(1).gameObject.SetActive(false);
            Objs[i].transform.GetChild(2).gameObject.SetActive(true);
            Objs[i].transform.GetChild(2).gameObject.transform.GetChild(1).gameObject.SetActive(false);
        }
    }
    public void GoBack()
    {
        GetComponentInParent<ShopPanelDisplayController>().GoBackToBattleUI();
    }

    [ContextMenu("TestAddItem")]
    public void TestAddItem()
    {

        DiegGaoGaoGame.instance.gameUser.AddItem(0);
        DiegGaoGaoGame.instance.gameUser.AddItem(1);
    }

  
    public void PayButton() 
    {
     
        allCoin = DiegGaoGaoGame.instance.gameUser.coin;
        GameDebug.Log(allCoin);
        if (allCoin < 1000)
        {
            return;
        }
        else 
        {
            DiegGaoGaoGame.instance.gameUser.ReduceCoin(1000);
            UnLock();
              
        }
    }


    void UnLock() 
    {


        //GameDebug.LogError($"DiegGaoGaoGame.instance.gameUser:{DiegGaoGaoGame.instance.gameUser == null}");
      
        int[] CurrentItems = DiegGaoGaoGame.instance.gameUser.GetItems();

        int[] CurrentItem = CurrentItems.Distinct().ToArray();

        int[] result= CurrentItem.OrderBy(x => x).ToArray();
       
        
        for (int i =0; i<result.Length+1;i ++) 
        {
            Objs[i].transform.GetChild(1).gameObject.SetActive(true);
            Objs[i].transform.GetChild(2).gameObject.SetActive(false);
        }
        
        DiegGaoGaoGame.instance.gameUser.AddItem(result.Length);
        Objs[result.Length+1].transform.GetChild(1).gameObject.SetActive(false);
        Objs[result.Length + 1].transform.GetChild(2).gameObject.SetActive(true);
        Objs[result.Length + 1].transform.GetChild(2).transform.GetChild(1).gameObject.SetActive(true);
       
        return;
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) 
        {
            DiegGaoGaoGame.instance.gameUser.AddCoin(1000000);
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            DiegGaoGaoGame.instance.gameUser.RemoveItem(8);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(7);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(6);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(5);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(4);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(3);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(2);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(1);
            DiegGaoGaoGame.instance.gameUser.RemoveItem(0);
            Refresh();
        }
    }

}
