using Ayay.SerilogLogs;
using Newtonsoft.Json.Linq;
using Serilog;
using SHH.CameraSdk;
using SHH.Contracts;

namespace SHH.CameraService
{
    /// <summary>
    /// 移除设备指令处理器
    /// </summary>
    public class RemoveCameraHandler : ICommandHandler
    {
        private ILogger _sysLog = Log.ForContext("SourceContext", LogModules.Core);

        private readonly CameraManager _cameraManager;

        /// <summary>
        /// 指令名称
        /// </summary>
        public string ActionName => ProtocolCodes.Remove_Camera;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="cameraManager"></param>
        public RemoveCameraHandler(CameraManager cameraManager)
        {
            _cameraManager = cameraManager;
        }

        /// <summary>
        /// 处理指令
        /// </summary>
        /// <param name="payload"></param>
        public async Task ExecuteAsync(JToken payload)
        {
            long deviceId = 0;

            try
            {
                // 1. 增强型 ID 解析
                if (payload.Type == JTokenType.Object)
                {
                    // 兼容大小写不敏感的解析
                    var idToken = payload["Id"] ?? payload["id"];
                    if (idToken != null) deviceId = idToken.Value<long>();
                }
                else if (payload.Type == JTokenType.Integer || payload.Type == JTokenType.String)
                {
                    // 兼容字符串形式的 ID
                    long.TryParse(payload.ToString(), out deviceId);
                }

                if (deviceId <= 0)
                {
                    _sysLog.Warning($"[Sync] 收到无效指令, ID解析失败 ({payload})");
                    return;
                }

                // 2. 预检查
                var device = _cameraManager.GetDevice(deviceId);
                if (device == null)
                {
                    _sysLog.Warning($"[Sync] 设备 {deviceId} 已经不在管理池中，无需操作.");
                    return;
                }

                // 3. 安全移除
                // 这里建议增加审计日志，记录谁触发了删除（如果协议里有用户信息的话）
                _sysLog.Debug($"[Sync] 收到远程指令, 正在安全移除设备, ID:{deviceId} Name:{device.Config.Name} .");

                // CameraManager 内部会：StopAsync -> DisposeAsync -> TryRemove -> SaveChanges
                await _cameraManager.RemoveDeviceAsync(deviceId);

                _sysLog.Information($"[Sync] 收到远程指令, 设备, ID:{deviceId} Name:{device.Config.Name}已彻底清理并从持久化库中移除 .");

                // 4. (可选) 此处可以调用 CommandDispatcher 发送 Success ACK
            }
            catch (Exception ex)
            {
                // 捕获异常，防止影响全局 Socket 轮询
                _sysLog.Error($"[Sync] 移除设备, ID:{deviceId} 过程中发生致命错误, {ex.Message}.");
            }
        }
    }
}