using UnityEngine;

/// <summary>
/// Test helper to simulate entering different settlement types
/// Use this to test town vs village generation
/// </summary>
public class SettlementTestHelper : MonoBehaviour
{
    [Header("Test Controls")]
    public bool createContextOnStart = false; // DISABLED - Don't override real settlement context
    public SettlementType testSettlementType = SettlementType.Town;
    public string testSettlementName = "Test Town";
    public bool testHasHarbor = false;

    void Start()
    {
        // COMPLETELY DISABLED - This component should never interfere with real settlement system
        Debug.Log("SettlementTestHelper: DISABLED - No test context creation allowed");
        return;

        // if (createContextOnStart)
        // {
        //     Debug.Log("SettlementTestHelper: Creating test context (should only happen during testing)");
        //     CreateTestSettlementContext();
        // }
        // else
        // {
        //     Debug.Log("SettlementTestHelper: Disabled - not creating test context");
        // }
    }

    [ContextMenu("Create Test Town Context")]
    public void CreateTestTownContext()
    {
        CreateSettlementContext("Test Town", SettlementType.Town, new Vector2Int(100, 100), true);
    }

    [ContextMenu("Create Test Village Context")]
    public void CreateTestVillageContext()
    {
        CreateSettlementContext("Test Village", SettlementType.Village, new Vector2Int(50, 50), false);
    }

    [ContextMenu("Create Custom Test Context")]
    public void CreateTestSettlementContext()
    {
        CreateSettlementContext(testSettlementName, testSettlementType, new Vector2Int(75, 75), testHasHarbor);
    }

    private void CreateSettlementContext(string name, SettlementType type, Vector2Int position, bool harbor)
    {
        // Find or create SettlementContext
        var existingContext = FindFirstObjectByType<SettlementContext>();
        SettlementContext context;

        if (existingContext != null)
        {
            context = existingContext;
            Debug.Log("Using existing SettlementContext");
        }
        else
        {
            var contextObject = new GameObject("SettlementContext");
            context = contextObject.AddComponent<SettlementContext>();
            Debug.Log("Created new SettlementContext");
        }

        // Set settlement data
        context.SetSettlement(name, type, position, harbor);

        Debug.Log($"✅ Settlement context configured: {name} ({type}) - Harbor: {harbor}");

        // Force regenerate shops to see immediate effect
        var shops = context.GenerateShops();
        Debug.Log($"Generated shops: {string.Join(", ", System.Array.ConvertAll(shops, s => s.buildingName))}");

        // Update any existing TownShopManager
        var shopManager = FindFirstObjectByType<TownShopManager>();
        if (shopManager != null)
        {
            Debug.Log("Forcing TownShopManager to reinitialize with new settlement data");
            // Force clear the shops and reinitialize everything properly
            shopManager.allShops = null;
            shopManager.ReinitializeShopsAndUI(); // Use the new method that handles UI properly
        }

        // Update TownManager if it exists
        var townManager = FindFirstObjectByType<TownManager>();
        if (townManager != null)
        {
            townManager.townName = name;
            townManager.hasHarbor = harbor;
            Debug.Log("Updated TownManager with settlement data");
        }
    }

    [ContextMenu("Clear Settlement Context")]
    public void ClearSettlementContext()
    {
        var context = FindFirstObjectByType<SettlementContext>();
        if (context != null)
        {
            context.ClearSettlementData();
            Debug.Log("Settlement context cleared");
        }
        else
        {
            Debug.Log("No settlement context found to clear");
        }
    }

    [ContextMenu("Print Current Context")]
    public void PrintCurrentContext()
    {
        if (SettlementContext.Instance != null)
        {
            var ctx = SettlementContext.Instance;
            Debug.Log($"Current Settlement Context:");
            Debug.Log($"  Name: {ctx.settlementName}");
            Debug.Log($"  Type: {ctx.settlementType}");
            Debug.Log($"  Position: {ctx.mapPosition}");
            Debug.Log($"  Harbor: {ctx.hasHarbor}");
            Debug.Log($"  Generated: {ctx.isGenerated}");
            Debug.Log($"  Shop Count: {(ctx.availableShops?.Length ?? 0)}");
        }
        else
        {
            Debug.Log("No SettlementContext instance found");
        }
    }
}
