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


public class MainStore : MonoBehaviour{
    public Button MenuTogglerButton; //Reference to the Button used to Toggle the Menu's state

    public GameObject Menu; //Reference to the Menu to be shown

    public GameObject[] ParentChildPrefab; //First item is Parent, Second item is Child

    public GameObject ContainerParentChildPrefab; //Container for the Menu Items

    public Sprite[] MenuDropdownSprites; //Icons for menu items which are dropdowns in the Menu, 0 = Caret down, 1 = Caret up

    //Menu Items in the Menu, they will be added dynamically to the Menu in the Shop UI
    private Dictionary<string, List<string>> menuData = new Dictionary<string, List<string>>()
    {
        { "Outfits", new List<string> { "Maasai1", "Maasai2" } }, //Dropdown
        { "Coins", null }, //Not dropdown
        { "BattlePass", null },  //Not dropdown
        { "Characters", null },  //Not dropdown
    };


    void OnEnable(){
        Menu.SetActive(false); //Sets initial state of Menu when the UI is activated
    }


    void Start(){
        MenuTogglerButton.onClick.AddListener(() => ToggleMenu("toggle"));
        PopulateMenu();
    }


    void PopulateMenu(){
        foreach (var item in menuData){
            string key = item.Key;
            List<string> children = item.Value;

            // Console.WriteLine(key);
            if (children != null)
            {
                InstantiateMenuItem(key, "parent", "isDropdown");
                Debug.Log(key); //Parent

                foreach (string child in children)
                {
                    InstantiateMenuItem(child, "child", "isDropdown");
                    Debug.Log("  - " + child); //Child
                }
            }

            //If there are no children, not a dropdown
            if (children == null){
                InstantiateMenuItem(key, "parent", "isNotDropdown");
                // isNotDropdown();
            }
        }

        //Remove the Menu Item Prefabs
        foreach (GameObject obj in ParentChildPrefab){
            obj.SetActive(false);
        }
    }


    void InstantiateMenuItem(string MenuItemName, string MenuItemType, string MenuItemParentType){
        GameObject MenuItem_Instance;

        switch(MenuItemType){
            case "parent":
                MenuItem_Instance = Instantiate(ParentChildPrefab[0], ContainerParentChildPrefab.transform);

                if (MenuItemParentType == "isNotDropdown"){
                    ParentisNotDropdown(MenuItem_Instance);
                }

                (MenuItem_Instance.transform.Find("text")).GetComponent<TextMeshProUGUI>().text = MenuItemName;
                MenuItem_Instance.name = MenuItemName;
                break;
                

            case "child":        
                MenuItem_Instance = Instantiate(ParentChildPrefab[1], ContainerParentChildPrefab.transform);
                (MenuItem_Instance.transform.Find("text")).GetComponent<TextMeshProUGUI>().text = MenuItemName;
                MenuItem_Instance.name = MenuItemName;
                break;
        }
    }


    //Changes state of the Menu, if Open then Closes it and vice versa
    void ToggleMenu(string menuStatus){
        bool isActive = Menu.activeSelf; //Check if the Menu is active
        bool currentMenuStatus = !isActive;

        if (menuStatus == "toggle"){
            Menu.SetActive(currentMenuStatus); //Change the current state of the Menu, if false then true, and vice versa
        
        }else if(menuStatus == "close"){
            Menu.SetActive(false);
        }

        //Menu has been opened
        if (currentMenuStatus == true){
            // MenuIsOpen();
        }
    }


    //When Menu is Opened, set the Selected Item within the Menu
    void MenuIsOpen(){

        // foreach(GameObject obj in MenuParents){
        //     setCaretDown(obj);
        // }

        // SetMenuSelectedGameObject(MenuParents[0]);
        // ShowHideChildren(new GameObject[0]);   //Pass empty array, so all children are hidden
    }


    void ParentisNotDropdown(GameObject MenuItem_Instance){
        GameObject caret_GameObject = (MenuItem_Instance.transform.Find("caret")).gameObject;
        caret_GameObject.SetActive(false);
        Debug.Log("Parent is Not Dropdown");
    }
    
}
