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

public class ForceCurveDataLogger : MonoBehaviour
{
    private List<float> _strokeBuffer = new List<float>();

    void Start()
    {
        if (PerformanceMonitorManager.Instance != null)
            PerformanceMonitorManager.Instance.OnForceCurveUpdated += HandleData;
    }

    void HandleData(List<float> points)
    {
        // 1. SIGNAL: Manager sends empty list when a NEW stroke starts
        if (points.Count == 0)
        {
            if (_strokeBuffer.Count > 5) 
            {
                LogFinishedStroke();
            }
            _strokeBuffer.Clear();
        }
        else
        {
            // 2. Accumulate the cumulative data
            _strokeBuffer = new List<float>(points);
        }
    }

    void LogFinishedStroke()
    {
        float maxForce = _strokeBuffer.Max();
        float totalImpulse = _strokeBuffer.Sum(); // Area under the curve
        string csv = string.Join(",", _strokeBuffer.Select(p => p.ToString("0")));

        Debug.Log($"<color=#FF7F00><b>[STROKE RECORDED]</b></color> Impulse: {totalImpulse:N0} | Pts: {_strokeBuffer.Count} | Max: {maxForce}");
        Debug.Log("CSV:" + csv);
    }
}