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


//ONLY FOR TESTING
#region 
//Progress of missions
[System.Serializable]
public class ProductDetails{
    public string productID; //records the week the player is at
    public string productName;
    public string productDescription; //records the week the player is at
    public int productPrice; //records the week the player is at
    public string productImage;
}
#endregion



public class PopulateStore : MonoBehaviour
{
    public GameObject[] BBundle_Prefab; //Big Bundle, The 1st is Container, 2nd is Prefab
    public GameObject[] SBundle_Prefab; //Big Bundle, The 1st is Container, 2nd is Prefab
    public GameObject[] ProductPage; //0 is the gameobject for the individual product page, 1 is product page dismiss button

    [HideInInspector]
    public List<ProductDetails> retrievedProductDetails, retrievedBundleProductDetails;

    public Sprite[] productSprites;     // Array of product sprites

    // private Dictionary<string, string, string> SamplePurchaseItems = new Dictionary<string, string, string>()
    // {
    //     { "Outfit1", "1", "2" }, //Dropdown
    //     { "Outfit2", "3", "4" }, //Not dropdown
    //     { "Outfit3", "5", "6" },  //Not dropdown
    // };


    void onEnable(){
        
    }

    void Awake(){
        Button ProductPageDismissBtn = ProductPage[1].GetComponent<Button>();
        ProductPageDismissBtn.onClick.AddListener(BackButton_RemoveProductPage);

        retrievedBundleProductDetails = new List<ProductDetails>{
            new ProductDetails { productID = "com.ex.cat1", productName = "Cat", productDescription = "Cat Character", productPrice = 40, productImage = "Cat4X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.cheetah2", productName = "Cheetah", productDescription = "Cheetah Character", productPrice = 50, productImage = "Cheetah5X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.hyena3", productName = "Hyena", productDescription = "Hyena Character", productPrice = 60, productImage = "Heyna3X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.cat1", productName = "Cat", productDescription = "Cat Character", productPrice = 40, productImage = "Cat4X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.cheetah2", productName = "Cheetah", productDescription = "Cheetah Character", productPrice = 50, productImage = "Cheetah5X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.hyena3", productName = "Hyena", productDescription = "Hyena Character", productPrice = 60, productImage = "Heyna3X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.cat1", productName = "Cat", productDescription = "Cat Character", productPrice = 40, productImage = "Cat4X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.cheetah2", productName = "Cheetah", productDescription = "Cheetah Character", productPrice = 50, productImage = "Cheetah5X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.hyena3", productName = "Hyena", productDescription = "Hyena Character", productPrice = 60, productImage = "Heyna3X-removebg-preview 2" },
        };

        retrievedProductDetails = new List<ProductDetails>{
            new ProductDetails { productID = "com.ex.cat", productName = "Cat", productDescription = "Cat Character", productPrice = 40, productImage = "Cat4X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.cheetah", productName = "Cheetah", productDescription = "Cheetah Character", productPrice = 50, productImage = "Cheetah5X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.hyena", productName = "Hyena", productDescription = "Hyena Character", productPrice = 60, productImage = "Heyna3X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.hippo", productName = "Hippo", productDescription = "Hippo Character", productPrice = 80, productImage = "Hipos2X-removebg-preview 2" },
            new ProductDetails { productID = "com.ex.hammerhead", productName = "HammerHead", productDescription = "HammerHead Character", productPrice = 100, productImage = "hM3X-removebg-preview 2" },
        };
    }


    // Start is called before the first frame update
    void Start()
    {
        //Bundle
        foreach (ProductDetails prodDet in retrievedBundleProductDetails){
            GameObject MenuItem_Instance = Instantiate(BBundle_Prefab[1], BBundle_Prefab[0].transform);
            Button GameObject_Button = MenuItem_Instance.GetComponent<Button>();
            GameObject_Button.onClick.AddListener(() => ShowProductPage(prodDet));

            Debug.Log(prodDet.productID + "\n");
        }
        BBundle_Prefab[1].SetActive(false);


        //Small Product
        foreach (ProductDetails prodDet in retrievedProductDetails){
            GameObject MenuItem_Instance = Instantiate(SBundle_Prefab[1], SBundle_Prefab[0].transform);
            Button GameObject_Button = MenuItem_Instance.GetComponent<Button>();
            GameObject_Button.onClick.AddListener(() => ShowProductPage(prodDet));

            Debug.Log(prodDet.productID + "\n");
        }
        SBundle_Prefab[1].SetActive(false);
    }


    void BackButton_RemoveProductPage(){
        ProductPage[0].SetActive(false);
    }


    void ShowProductPage(ProductDetails productDetails)
    {
        ProductPage[0].SetActive(true);
        Debug.Log($"Product ID: {productDetails.productID}\nProduct Name: {productDetails.productName}\nProduct Description: {productDetails.productDescription}\nProduct Price: {productDetails.productPrice}");
        
        // Get the TextMeshProUGUI component from the "productName" GameObject
        TextMeshProUGUI productNameText = ProductPage[0].transform.Find("CharacterPurchase/productName").GetComponent<TextMeshProUGUI>();
        productNameText.text = productDetails.productName;

        TextMeshProUGUI productPriceText = ProductPage[0].transform.Find("CharacterPurchase/GameObject (2)/productPrice/price").GetComponent<TextMeshProUGUI>();
        productPriceText.text = (productDetails.productPrice).ToString();
        
        // GameObject fkw = ProductPage[0].transform.Find("CharacterPurchase/Panel/item_image").gameObject;
        // fkw.name = "fkw";

        // Get the Image component from the "item_image" GameObject
        Image itemImage = ProductPage[0].transform.Find("CharacterPurchase/Panel/item_image").GetComponent<Image>();
        Sprite newSprite = FindSpriteByName(productDetails.productImage);
        if (newSprite != null)
        {
            itemImage.sprite = newSprite;
        }
        else
        {
            Debug.LogError("Failed to load image with name: " + productDetails.productImage);
        }
    }

    Sprite FindSpriteByName(string spriteName)
    {
        foreach (Sprite sprite in productSprites)
        {
            if (sprite.name == spriteName)
            {
                return sprite;
            }
        }
        return null;
    }


    //API-ish
    void GetProductDataByID(string productID){
        // MissionInfo mission = missionInfo.Find(m => m.missionID == missionID);
        // return mission.missionProgressCount;
    }


    // Update is called once per frame
    void Update()
    {
        
    }
}
