using ZB.MOM.WW.OtOpcUa.Core.Abstractions;

namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;

/// <summary>Extension methods for TwinCATDataType.</summary>
public static class TwinCATDataTypeExtensions
{
    /// <summary>Maps a TwinCAT data type to the equivalent driver data type.</summary>
    /// <param name="t">The TwinCAT data type to convert.</param>
    /// <returns>The corresponding driver data type.</returns>
    public static DriverDataType ToDriverDataType(this TwinCATDataType t) => t switch
    {
        TwinCATDataType.Bool => DriverDataType.Boolean,
        TwinCATDataType.SInt => DriverDataType.Int16,    // signed 8-bit — no narrower OPC UA atom
        TwinCATDataType.USInt => DriverDataType.UInt16,  // unsigned 8-bit — no narrower OPC UA atom
        TwinCATDataType.Int => DriverDataType.Int16,
        TwinCATDataType.UInt => DriverDataType.UInt16,
        TwinCATDataType.DInt => DriverDataType.Int32,
        TwinCATDataType.UDInt => DriverDataType.UInt32,
        TwinCATDataType.LInt => DriverDataType.Int64,
        TwinCATDataType.ULInt => DriverDataType.UInt64,
        TwinCATDataType.Real => DriverDataType.Float32,
        TwinCATDataType.LReal => DriverDataType.Float64,
        TwinCATDataType.String or TwinCATDataType.WString => DriverDataType.String,
        TwinCATDataType.Time or TwinCATDataType.Date
            or TwinCATDataType.DateTime or TwinCATDataType.TimeOfDay => DriverDataType.UInt32,
        TwinCATDataType.Structure => DriverDataType.String,
        _ => DriverDataType.Int32,
    };
}
