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

public class SimpleTownShopUI : MonoBehaviour
{
    private TownShop currentShop;
    private TeamCharacter[] currentTeam;
    private Action onCloseCallback;

    public void OpenShop(TownShop shop, TeamCharacter[] team, Action closeCallback)
    {
        currentShop = shop;
        currentTeam = team;
        onCloseCallback = closeCallback;

        Debug.Log($"Opening {shop.buildingName} - This is a simplified shop UI for testing");
        CreateSimpleUI();
    }

    private void CreateSimpleUI()
    {
        // Create UI Document
        var uiDocument = gameObject.AddComponent<UIDocument>();

        // Create a simple overlay UI
        var root = new VisualElement();
        root.style.width = Length.Percent(100);
        root.style.height = Length.Percent(100);
        root.style.position = Position.Absolute;
        root.style.backgroundColor = new Color(0, 0, 0, 0.8f);
        root.style.justifyContent = Justify.Center;
        root.style.alignItems = Align.Center;

        // Create shop dialog
        var dialog = new VisualElement();
        dialog.style.width = 400;
        dialog.style.height = 300;
        dialog.style.backgroundColor = new Color(0.2f, 0.15f, 0.1f, 1f);
        dialog.style.borderTopWidth = 3;
        dialog.style.borderBottomWidth = 3;
        dialog.style.borderLeftWidth = 3;
        dialog.style.borderRightWidth = 3;
        dialog.style.borderTopColor = new Color(0.8f, 0.6f, 0.2f, 1f);
        dialog.style.borderBottomColor = new Color(0.8f, 0.6f, 0.2f, 1f);
        dialog.style.borderLeftColor = new Color(0.8f, 0.6f, 0.2f, 1f);
        dialog.style.borderRightColor = new Color(0.8f, 0.6f, 0.2f, 1f);
        dialog.style.paddingTop = 20;
        dialog.style.paddingBottom = 20;
        dialog.style.paddingLeft = 20;
        dialog.style.paddingRight = 20;
        dialog.style.justifyContent = Justify.SpaceBetween;

        // Title
        var title = new Label(currentShop.buildingName);
        title.style.fontSize = 24;
        title.style.color = new Color(1f, 0.84f, 0f, 1f);
        title.style.unityFontStyleAndWeight = FontStyle.Bold;
        title.style.unityTextAlign = TextAnchor.MiddleCenter;

        // Content
        var content = new VisualElement();
        content.style.flexGrow = 1;
        content.style.justifyContent = Justify.Center;

        var welcomeText = new Label($"Welcome to {currentShop.buildingName}!");
        welcomeText.style.color = Color.white;
        welcomeText.style.fontSize = 16;
        welcomeText.style.unityTextAlign = TextAnchor.MiddleCenter;
        welcomeText.style.marginBottom = 10;

        var inventoryText = new Label($"Items in stock: {currentShop.currentInventory.Count}");
        inventoryText.style.color = Color.white;
        inventoryText.style.fontSize = 14;
        inventoryText.style.unityTextAlign = TextAnchor.MiddleCenter;
        inventoryText.style.marginBottom = 10;

        var playerMoney = "";
        if (currentTeam != null && currentTeam.Length > 0)
        {
            var player = currentTeam[0];
            playerMoney = $"Your money: {player.gold}g {player.silver}s {player.copper}c";
        }
        else
        {
            playerMoney = "Your money: 100g 50s 25c";
        }

        var moneyText = new Label(playerMoney);
        moneyText.style.color = new Color(1f, 0.84f, 0f, 1f);
        moneyText.style.fontSize = 14;
        moneyText.style.unityTextAlign = TextAnchor.MiddleCenter;
        moneyText.style.marginBottom = 20;

        var noteText = new Label("(This is a simplified shop UI for testing.\nFull shop functionality will be added later.)");
        noteText.style.color = new Color(0.8f, 0.8f, 0.8f, 1f);
        noteText.style.fontSize = 12;
        noteText.style.unityTextAlign = TextAnchor.MiddleCenter;

        content.Add(welcomeText);
        content.Add(inventoryText);
        content.Add(moneyText);
        content.Add(noteText);

        // Close button
        var closeButton = new Button(() => CloseShop()) { text = "Close Shop" };
        closeButton.style.backgroundColor = new Color(0.7f, 0.2f, 0.2f, 1f);
        closeButton.style.color = Color.white;
        closeButton.style.paddingTop = 10;
        closeButton.style.paddingBottom = 10;
        closeButton.style.fontSize = 16;

        dialog.Add(title);
        dialog.Add(content);
        dialog.Add(closeButton);

        root.Add(dialog);
        uiDocument.rootVisualElement.Clear();
        uiDocument.rootVisualElement.Add(root);

        Debug.Log("Simple shop UI created and displayed!");
    }

    private void CloseShop()
    {
        Debug.Log($"Closing {currentShop.buildingName}");
        onCloseCallback?.Invoke();
    }
}
