using UnityEngine;
using System.Collections.Generic;
using System;

/// <summary>
/// Context information passed to travel events for decision making
/// </summary>
[Serializable]
public class TravelEventContext
{
    public Vector2Int currentPosition;
    public MapTile currentTile;
    public TravelRoute currentRoute;
    public int dayOfJourney;
    public float timeOfDay; // 0-24 hours
    public Weather currentWeather = Weather.Clear;

    // Team state
    public List<TeamCharacter> teamMembers;
    public int teamGold;
    public int teamFood;
    public float teamMorale = 1f;

    // Event tracking
    public TravelEventHistory eventHistory;

    // Travel context
    public Vector2Int destination;
    public bool isOnMainRoad;
    public float distanceToDestination;

    public TravelEventContext(Vector2Int position, MapTile tile, TravelRoute route)
    {
        currentPosition = position;
        currentTile = tile;
        currentRoute = route;
        eventHistory = new TravelEventHistory();
        teamMembers = new List<TeamCharacter>();

        // Initialize with current team data
        var teamPlacement = UnityEngine.Object.FindFirstObjectByType<SimpleTeamPlacement>();
        if (teamPlacement != null)
        {
            // Get team data from the game
            // This would need to be implemented based on your team system
        }
    }
}

/// <summary>
/// Tracks which events have occurred and when
/// </summary>
[Serializable]
public class TravelEventHistory
{
    [Serializable]
    public class EventOccurrence
    {
        public string eventName;
        public float dayOccurred;
        public Vector2Int location;
    }

    public List<EventOccurrence> pastEvents = new List<EventOccurrence>();

    public bool HasOccurred(TravelEvent travelEvent)
    {
        return pastEvents.Exists(e => e.eventName == travelEvent.eventName);
    }

    public float GetTimeSinceLastOccurrence(TravelEvent travelEvent)
    {
        var lastOccurrence = pastEvents.FindLast(e => e.eventName == travelEvent.eventName);
        if (lastOccurrence == null) return float.MaxValue;

        // This would need to be implemented with actual game time
        return Time.time - lastOccurrence.dayOccurred;
    }

    public void RecordEvent(TravelEvent travelEvent, TravelEventContext context)
    {
        pastEvents.Add(new EventOccurrence
        {
            eventName = travelEvent.eventName,
            dayOccurred = context.dayOfJourney,
            location = context.currentPosition
        });
    }
}

[Serializable]
public enum Weather
{
    Clear,
    Cloudy,
    Rain,
    Storm,
    Fog,
    Snow
}

/// <summary>
/// Interface for objects that can handle travel event results
/// </summary>
public interface ITravelEventHandler
{
    void HandleEventResult(EventResult result, TravelEventContext context);
}
