using SHH.Contracts;
using System.Diagnostics;

namespace SHH.MjpegPlayer
{
    /// <summary>
    /// AI 视频流监控控制器
    /// 职责：接收 gRpc 转换后的 Payload -> 业务转换 -> 分发 UI/AI
    /// </summary>
    public class ImageMonitorController
    {
        public static ImageMonitorController Instance { get; } = new ImageMonitorController();

        private ImageMonitorController() { }

        /// <summary>
        /// 统一接收入口：由 GatewayProviderImpl.UploadVideoStream 调用
        /// </summary>
        public void ReceivePayload(VideoPayload payload)
        {
            if (payload == null) return;

            // 1. 过滤 2 秒外的过期数据
            if ((DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - payload.CaptureTimestamp) > 2000)
                return;

            try
            {
                ImagePayloadConverter.ToXWcfMsg(payload);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"[Controller Error] {ex.Message}");
            }
        }
    }
}