using UnityEditor;
using UnityEngine;
using AppleHills.Data.CardSystem;
using Data.CardSystem;
using Core;
using UI.CardSystem;
using UnityEngine.UI;
using System.Collections.Generic;
namespace Editor.CardSystem
{
///
/// Editor window for testing the Card System in play mode.
/// Provides buttons to test core functionalities like adding booster packs, opening packs, and generating cards.
///
public class CardSystemTesterWindow : EditorWindow
{
// Test Settings
private int boosterPacksToAdd = 3;
private int cardsToGenerate = 10;
// Auto-add toggles (always editable)
private bool autoAddBoosters = false;
private bool autoAddCards = false;
// Scheduling for delayed auto-add after entering play mode
private double scheduledAddTime = -1.0;
private bool scheduledBoosters = false;
private bool scheduledCards = false;
// Debug Info
private int currentBoosterCount;
private int totalCardsInCollection;
private string lastActionMessage = "";
// UI State
private Vector2 scrollPosition;
[MenuItem("AppleHills/Cards/Card System Tester")]
public static void ShowWindow()
{
var window = GetWindow(false, "Card System Tester", true);
window.minSize = new Vector2(400, 500);
}
private void OnEnable()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
EditorApplication.update += EditorUpdate;
}
private void OnDisable()
{
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.update -= EditorUpdate;
}
private void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredPlayMode)
{
RefreshDebugInfo();
// Schedule auto-adds after 1 second if toggles are enabled
if (autoAddBoosters || autoAddCards)
{
scheduledAddTime = EditorApplication.timeSinceStartup + 1.0;
scheduledBoosters = autoAddBoosters;
scheduledCards = autoAddCards;
}
}
else if (state == PlayModeStateChange.ExitingPlayMode)
{
lastActionMessage = "";
// clear any scheduled actions when leaving play mode
scheduledAddTime = -1.0;
scheduledBoosters = false;
scheduledCards = false;
}
Repaint();
}
// Polling update used to execute scheduled actions after a delay
private void EditorUpdate()
{
if (scheduledAddTime > 0 && EditorApplication.timeSinceStartup >= scheduledAddTime)
{
// perform scheduled actions
if (scheduledBoosters)
{
AddBoosterPacks();
}
if (scheduledCards)
{
GenerateRandomCards();
}
// clear schedule
scheduledAddTime = -1.0;
scheduledBoosters = false;
scheduledCards = false;
Repaint();
}
}
private void OnGUI()
{
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
// Header
EditorGUILayout.Space(10);
EditorGUILayout.LabelField("Card System Tester", EditorStyles.boldLabel);
EditorGUILayout.HelpBox("This tool allows you to test the card system in play mode. " +
"Enter play mode to enable the testing functions.", MessageType.Info);
EditorGUILayout.Space(10);
// Test Settings Section
DrawTestSettings();
EditorGUILayout.Space(10);
// Debug Info Section
DrawDebugInfo();
EditorGUILayout.Space(10);
// Test Actions Section
DrawTestActions();
EditorGUILayout.EndScrollView();
}
private void DrawTestSettings()
{
EditorGUILayout.LabelField("Test Settings", EditorStyles.boldLabel);
// Booster pack slider with always-editable auto-add toggle
EditorGUILayout.BeginHorizontal();
EditorGUI.BeginDisabledGroup(!Application.isPlaying);
boosterPacksToAdd = EditorGUILayout.IntSlider("Booster Packs to Add", boosterPacksToAdd, 1, 10);
EditorGUI.EndDisabledGroup();
GUILayout.FlexibleSpace();
autoAddBoosters = EditorGUILayout.ToggleLeft("Auto Add", autoAddBoosters, GUILayout.Width(110));
EditorGUILayout.EndHorizontal();
// Card generation slider with always-editable auto-add toggle
EditorGUILayout.BeginHorizontal();
EditorGUI.BeginDisabledGroup(!Application.isPlaying);
cardsToGenerate = EditorGUILayout.IntSlider("Cards to Generate", cardsToGenerate, 1, 100);
EditorGUI.EndDisabledGroup();
GUILayout.FlexibleSpace();
autoAddCards = EditorGUILayout.ToggleLeft("Auto Add", autoAddCards, GUILayout.Width(110));
EditorGUILayout.EndHorizontal();
}
private void DrawDebugInfo()
{
EditorGUILayout.LabelField("Debug Info", EditorStyles.boldLabel);
if (Application.isPlaying)
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.IntField("Current Booster Count", currentBoosterCount);
EditorGUILayout.IntField("Total Cards in Collection", totalCardsInCollection);
EditorGUI.EndDisabledGroup();
if (!string.IsNullOrEmpty(lastActionMessage))
{
EditorGUILayout.Space(5);
EditorGUILayout.HelpBox(lastActionMessage, MessageType.None);
}
EditorGUILayout.Space(5);
if (GUILayout.Button("Refresh Debug Info"))
{
RefreshDebugInfo();
}
}
else
{
EditorGUILayout.HelpBox("Debug info available in play mode.", MessageType.Warning);
}
}
private void DrawTestActions()
{
EditorGUILayout.LabelField("Test Actions", EditorStyles.boldLabel);
if (!Application.isPlaying)
{
EditorGUILayout.HelpBox("Enter Play Mode to use these testing functions.", MessageType.Warning);
return;
}
// Booster Pack Actions
EditorGUILayout.Space(5);
EditorGUILayout.LabelField("Booster Pack Actions", EditorStyles.miniBoldLabel);
if (GUILayout.Button("Add Booster Packs", GUILayout.Height(30)))
{
AddBoosterPacks();
}
// Card Generation Actions
EditorGUILayout.Space(10);
EditorGUILayout.LabelField("Card Generation Actions", EditorStyles.miniBoldLabel);
if (GUILayout.Button("Generate Random Cards", GUILayout.Height(30)))
{
GenerateRandomCards();
}
EditorGUILayout.Space(5);
// Danger Zone
EditorGUILayout.Space(10);
EditorGUILayout.LabelField("Danger Zone", EditorStyles.miniBoldLabel);
GUI.backgroundColor = new Color(1f, 0.6f, 0.6f);
if (GUILayout.Button("Clear All Cards", GUILayout.Height(30)))
{
if (EditorUtility.DisplayDialog("Clear All Cards",
"Are you sure you want to clear all cards from the inventory? This cannot be undone in this play session.",
"Clear All", "Cancel"))
{
ClearAllCards();
}
}
GUI.backgroundColor = Color.white;
}
// Refresh the debug information
private void RefreshDebugInfo()
{
if (!Application.isPlaying)
return;
if (CardSystemManager.Instance != null)
{
currentBoosterCount = CardSystemManager.Instance.GetBoosterPackCount();
totalCardsInCollection = CardSystemManager.Instance.GetCardInventory().GetAllCards().Count;
Repaint();
}
}
// Test Action Methods
private void AddBoosterPacks()
{
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.AddBoosterPack(boosterPacksToAdd);
lastActionMessage = $"Added {boosterPacksToAdd} booster pack(s)";
Logging.Debug($"[CardSystemTesterWindow] {lastActionMessage}");
RefreshDebugInfo();
}
else
{
lastActionMessage = "Error: CardSystemManager instance not found!";
Debug.LogError("[CardSystemTesterWindow] " + lastActionMessage);
Repaint();
}
}
private void GenerateRandomCards()
{
if (CardSystemManager.Instance != null)
{
int cardsAdded = 0;
List allDefinitions = CardSystemManager.Instance.GetAllCardDefinitions();
if (allDefinitions.Count == 0)
{
lastActionMessage = "Error: No card definitions available";
Logging.Warning($"[CardSystemTesterWindow] {lastActionMessage}");
Repaint();
return;
}
for (int i = 0; i < cardsToGenerate; i++)
{
// Get a random card definition
CardDefinition randomDef = allDefinitions[Random.Range(0, allDefinitions.Count)];
// Create a card data instance and add it to inventory
CardData newCard = randomDef.CreateCardData();
CardSystemManager.Instance.GetCardInventory().AddCard(newCard);
cardsAdded++;
}
lastActionMessage = $"Generated {cardsAdded} random cards";
Logging.Debug($"[CardSystemTesterWindow] {lastActionMessage}");
RefreshDebugInfo();
}
else
{
lastActionMessage = "Error: CardSystemManager instance not found!";
Debug.LogError("[CardSystemTesterWindow] " + lastActionMessage);
Repaint();
}
}
private void ClearAllCards()
{
if (CardSystemManager.Instance != null)
{
int count = CardSystemManager.Instance.GetCardInventory().GetAllCards().Count;
CardSystemManager.Instance.GetCardInventory().ClearAllCards();
lastActionMessage = $"Cleared {count} cards from inventory";
Logging.Debug($"[CardSystemTesterWindow] {lastActionMessage}");
RefreshDebugInfo();
}
else
{
lastActionMessage = "Error: CardSystemManager instance not found!";
Debug.LogError("[CardSystemTesterWindow] " + lastActionMessage);
Repaint();
}
}
}
}