using Ayay.SerilogLogs;
using Serilog;
using SHH.CameraSdk.HikFeatures;

namespace SHH.CameraSdk.DahuaFeatures;

/// <summary>
/// [大华功能组件] 云台与镜头控制
/// <para>适配说明：严格匹配 NETClient.PTZControl(IntPtr, int, uint, int, int, int, bool, IntPtr) 接口</para>
/// </summary>
public class DahuaPtzProvider : IPtzFeature
{
    private readonly IDahuaContext _context;

    #region --- 大华 PTZ 命令常量 (对应 dwPTZCommand) ---

    private const uint PTZ_UP = 0;
    private const uint PTZ_DOWN = 1;
    private const uint PTZ_LEFT = 2;
    private const uint PTZ_RIGHT = 3;
    private const uint PTZ_ZOOM_ADD = 4;    // 变倍+
    private const uint PTZ_ZOOM_DEC = 5;    // 变倍-
    private const uint PTZ_FOCUS_ADD = 6;   // 聚焦+
    private const uint PTZ_FOCUS_DEC = 7;   // 聚焦-
    private const uint PTZ_IRIS_ADD = 8;    // 光圈+
    private const uint PTZ_IRIS_DEC = 9;    // 光圈-

    #endregion

    public DahuaPtzProvider(IDahuaContext context)
    {
        _context = context;
    }

    public async Task PtzControlAsync(PtzAction action, bool stop, int speed)
    {
        IntPtr loginId = _context.GetUserId();
        if (loginId == IntPtr.Zero) return;

        await Task.Run(() =>
        {
            // 1. 映射指令
            uint dwCommand = action switch
            {
                PtzAction.Up => PTZ_UP,
                PtzAction.Down => PTZ_DOWN,
                PtzAction.Left => PTZ_LEFT,
                PtzAction.Right => PTZ_RIGHT,
                PtzAction.ZoomIn => PTZ_ZOOM_ADD,
                PtzAction.ZoomOut => PTZ_ZOOM_DEC,
                PtzAction.FocusFar => PTZ_FOCUS_ADD,
                PtzAction.FocusNear => PTZ_FOCUS_DEC,
                PtzAction.IrisOpen => PTZ_IRIS_ADD,
                PtzAction.IrisClose => PTZ_IRIS_DEC,
                _ => 999
            };

            if (dwCommand == 999) return;

            // 2. 准备速度参数 (大华一般 1-8)
            // Modified: [原因] 严格适配 8 参数接口。lParam1=水平速度, lParam2=垂直速度, lParam3=0
            int s = Math.Clamp(speed, 1, 8);
            int lParam1 = s;
            int lParam2 = s;
            int lParam3 = 0;

            // 3. 调用你提供的接口
            // Modified: [原因] 匹配签名: (IntPtr, int, uint, int, int, int, bool, IntPtr)
            bool result = NETClient.PTZControl(
                loginId,
                0,           // nChannelID
                dwCommand,   // dwPTZCommand
                lParam1,
                lParam2,
                lParam3,
                stop,        // dwStop
                IntPtr.Zero  // param4
            );

            if (!result)
            {
                string error = NETClient.GetLastError();
                Log.ForContext("SourceContext", LogModules.DaHuaSdk)
                   .Warning("[SDK] Dahua PTZ 失败. Action: {Action}, Stop: {Stop}, Error: {Error}, 可能操作太快.",
                            action, stop, error);
            }
            else
            {

            }
        });
    }

    public async Task PtzStepAsync(PtzAction action, int durationMs, int speed)
    {
        await PtzControlAsync(action, false, speed);
        await Task.Delay(durationMs);
        await PtzControlAsync(action, true, speed);
    }
}