using Common.Attributes;
using DeviceCommand.Base;
using Model.Models;
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceCommand.Devices
{
    #region 枚举定义

    /// <summary>
    /// Tektronix MSO 采集模式
    /// </summary>
    public enum TekAcquisitionMode
    {
        /// <summary> 采样模式 </summary>
        SAMple,
        /// <summary> 峰值检测模式 </summary>
        PEAKdetect,
        /// <summary> 高分辨率模式 </summary>
        HIRes,
        /// <summary> 平均模式 </summary>
        AVErage,
        /// <summary> 包络模式 </summary>
        ENVelope
    }

    /// <summary>
    /// Tektronix MSO 测量类型 (部分常用)
    /// </summary>
    public enum TekMeasurementType
    {
        AMPLitude,
        FREQuency,
        MEAN,
        PK2PK,
        MAXimum,
        MINimum
    }

    #endregion

    [ACPCommand]
    public class TektronixMSO : Tcp
    {
        // SCPI 指令结束符 (文档中提到消息终止符需使用 LF)[cite: 1]
        private const string ScpiDelimiter = "\n";

        public TektronixMSO(TcpConfig config) : base(config)
        {
        }

        #region 1. 公共命令与状态查询

        public virtual async Task<string> 查询设备标识(CancellationToken ct = default)
        {
            // 返回仪器的标识代码[cite: 1]
            return await WriteReadAsync($"*IDN?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        public virtual async Task<string> 查询忙碌状态(CancellationToken ct = default)
        {
            // 查询仪器是否处于忙碌状态，常用于同步操作[cite: 1]
            return await WriteReadAsync($"BUSY?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        public virtual async Task 执行自校准(CancellationToken ct = default)
        {
            // 指示仪器执行信号路径校准 (SPC)，需要几分钟时间[cite: 1]
            await SendAsync($"*CAL?{ScpiDelimiter}", ct);
        }

        #endregion

        #region 2. 采集系统控制 (Acquisition)

        /// <summary>
        /// 启动或停止采集
        /// </summary>
        public virtual async Task 设置采集状态(bool 运行, CancellationToken ct = default)
        {
            // 启动、停止或返回采集状态[cite: 1]
            string 参数 = 运行 ? "RUN" : "STOP";
            await SendAsync($"ACQuire:STATE {参数}{ScpiDelimiter}", ct);
        }

        public virtual async Task<string> 查询采集状态(CancellationToken ct = default)
        {
            return await WriteReadAsync($"ACQuire:STATE?{ScpiDelimiter}", ScpiDelimiter, ct); //[cite: 1]
        }

        public virtual async Task 设置采集模式(TekAcquisitionMode 模式, CancellationToken ct = default)
        {
            // 设置或查询采集模式[cite: 1]
            await SendAsync($"ACQuire:MODe {模式}{ScpiDelimiter}", ct);
        }

        #endregion

        #region 3. 水平系统控制 (Horizontal)

        public virtual async Task 设置水平刻度(double 秒每格, CancellationToken ct = default)
        {
            // 设置或查询水平刻度 (Scale)[cite: 1]
            string cmd = string.Format(CultureInfo.InvariantCulture, "HORizontal:SCAle {0:E}{1}", 秒每格, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        public virtual async Task 设置记录长度(long 长度, CancellationToken ct = default)
        {
            // 设置或查询记录长度[cite: 1]
            await SendAsync($"HORizontal:RECOrdlength {长度}{ScpiDelimiter}", ct);
        }

        public virtual async Task 设置采样率(double 采样率, CancellationToken ct = default)
        {
            // 设置或查询水平采样率[cite: 1]
            string cmd = string.Format(CultureInfo.InvariantCulture, "HORizontal:SAMPLERate {0:E}{1}", 采样率, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        #endregion

        #region 4. 通道与显示控制 (Display & Vertical)

        /// <summary>
        /// 全局启用或关闭某通道的显示
        /// </summary>
        public virtual async Task 设置通道显示开关(int 通道号, bool 开启, CancellationToken ct = default)
        {
            // 设置或查询指定通道的显示模式（开或关）[cite: 1]
            string 参数 = 开启 ? "ON" : "OFF";
            await SendAsync($"DISplay:GLObal:CH{通道号}:STATE {参数}{ScpiDelimiter}", ct);
        }

        /// <summary>
        /// 设置指定通道的垂直缩放比例 (Volts/Div)
        /// </summary>
        public virtual async Task 设置通道垂直刻度(int 通道号, double 伏特每格, CancellationToken ct = default)
        {
            // 设置或查询指定Waveform View内指定通道的垂直缩放比例（注：WaveView<x> 通常为1）[cite: 1]
            string cmd = string.Format(CultureInfo.InvariantCulture, "DISplay:WAVEView1:CH{0}:VERTical:SCAle {1:E}{2}", 通道号, 伏特每格, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        /// <summary>
        /// 设置指定通道的垂直位置 (Divisions)
        /// </summary>
        public virtual async Task 设置通道垂直位置(int 通道号, double 格数, CancellationToken ct = default)
        {
            // 设置或查询指定Waveform View内指定通道的垂直位置（以格为单位）[cite: 1]
            string cmd = string.Format(CultureInfo.InvariantCulture, "DISplay:WAVEView1:CH{0}:VERTical:POSition {1:F2}{2}", 通道号, 格数, ScpiDelimiter);
            await SendAsync(cmd, ct);
        }

        #endregion

        #region 5. 自动测量 (Measurement)

        /// <summary>
        /// 新增一个测量项槽位
        /// </summary>
        public virtual async Task 添加测量项(CancellationToken ct = default)
        {
            // 添加一个测量项[cite: 1]
            await SendAsync($"MEASUrement:ADDMEAS{ScpiDelimiter}", ct);
        }

        public virtual async Task 设置测量类型(int 测量项编号, TekMeasurementType 类型, CancellationToken ct = default)
        {
            // 设置或查询测量类型[cite: 1]
            await SendAsync($"MEASUrement:MEAS{测量项编号}:TYPe {类型}{ScpiDelimiter}", ct);
        }

        public virtual async Task 设置测量源(int 测量项编号, string 源名称, CancellationToken ct = default)
        {
            // 设置或查询局部输入源 (例如 CH1, CH2)[cite: 1]
            await SendAsync($"MEASUrement:MEAS{测量项编号}:SOURCE {源名称}{ScpiDelimiter}", ct);
        }

        [Monitorable("测量项当前采集的平均值")]
        public virtual async Task<string> 查询测量项平均值(int 测量项编号, CancellationToken ct = default)
        {
            // 返回当前采集的指定测量的平均值[cite: 1]
            return await WriteReadAsync($"MEASUrement:MEAS{测量项编号}:RESUlts:CURRentacq:MEAN?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        public virtual async Task<string> 查询测量项最大值(int 测量项编号, CancellationToken ct = default)
        {
            // 返回自上次统计重置以来指定测量的最大值（当前采集）[cite: 1]
            return await WriteReadAsync($"MEASUrement:MEAS{测量项编号}:RESUlts:CURRentacq:MAXimum?{ScpiDelimiter}", ScpiDelimiter, ct);
        }

        #endregion
    }
}