using NIDAQmx.DAQmxEnum;
using NIDAQmxConst;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace NIDAQmx
{
    public sealed class AOChannel : OutChannel
    {
        internal AOChannel(DAQmxSession session) : base(session)
        {
            AttributeSession = new ChannelAttributeSession(session);
        }
        internal override ChannelAttributeSession AttributeSession { get; }
        internal void CreateVoltage(string physicalChannel, string nameToAssignToChannel, double minVal, double maxVal, AOVoltageUnits units,string customScaleName="")
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.AOCreate.DAQmxCreateAOVoltageChan>()(_Session.TaskHandle, physicalChannel, nameToAssignToChannel,  minVal, maxVal, units, customScaleName));
            if (_Session.Error)
            {
                throw new Exception($"Create Voltage Channel fail");
            }
            ChannelName = physicalChannel;
        }
        internal void CreateCurrent(string physicalChannel, string nameToAssignToChannel, double minVal, double maxVal, AnalogCurrentUnit units,string customScaleName="")
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.AOCreate.DAQmxCreateAOCurrentChan>()(_Session.TaskHandle, physicalChannel, nameToAssignToChannel, minVal, maxVal, units, customScaleName));
            if (_Session.Error)
            {
                throw new Exception($"Create Voltage Channel fail");
            }
            ChannelName = physicalChannel;
        }
        internal void CreateFuncGen(string physicalChannel,  string nameToAssignToChannel, AOFuncGenType type, double freq, double amplitude, double offset)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.AOCreate.DAQmxCreateAOFuncGenChan>()(_Session.TaskHandle, physicalChannel, nameToAssignToChannel, type,freq,amplitude,offset));
            if (_Session.Error)
            {
                throw new Exception($"Create FuncGen Channel fail");
            }
            ChannelName = physicalChannel;
        }
        public void WriteAnalog(int numSampsPerChan, bool autoStart, double timeout, bool dataLayout, ref double writeArray,ref int sampsPerChanWritten)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.DataWrite.DAQmxWriteAnalogF64>()(_Session.TaskHandle, numSampsPerChan, autoStart, timeout, dataLayout, ref writeArray, ref sampsPerChanWritten, IntPtr.Zero));
        }
        public void WriteAnalogScalar(bool autoStart, double timeout, double value)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.DataWrite.DAQmxWriteAnalogScalarF64>()(_Session.TaskHandle, autoStart, timeout, value, IntPtr.Zero));
        }
        public void WriteBinary(int numSampsPerChan, bool autoStart, double timeout, bool dataLayout, ref short writeArray,ref int sampsPerChanWritten)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.DataWrite.DAQmxWriteBinaryI16>()(_Session.TaskHandle, numSampsPerChan, autoStart, timeout, dataLayout, ref writeArray, ref sampsPerChanWritten, IntPtr.Zero));
        }
        public void WriteBinary(int numSampsPerChan, bool autoStart, double timeout, bool dataLayout, ref ushort writeArray, ref int sampsPerChanWritten)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.DataWrite.DAQmxWriteBinaryU16>()(_Session.TaskHandle, numSampsPerChan, autoStart, timeout, dataLayout, ref writeArray, ref sampsPerChanWritten, IntPtr.Zero));
        }
        public void WriteBinary(int numSampsPerChan, bool autoStart, double timeout, bool dataLayout, ref int writeArray, ref int sampsPerChanWritten)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.DataWrite.DAQmxWriteBinaryI32>()(_Session.TaskHandle, numSampsPerChan, autoStart, timeout, dataLayout, ref writeArray, ref sampsPerChanWritten, IntPtr.Zero));
        }
        public void WriteBinary(int numSampsPerChan, bool autoStart, double timeout, bool dataLayout, ref uint writeArray, ref int sampsPerChanWritten)
        {
            _Session.CheckResult(_Session.GetDelegate<Internal.DataWrite.DAQmxWriteBinaryU32>()(_Session.TaskHandle, numSampsPerChan, autoStart, timeout, dataLayout, ref writeArray, ref sampsPerChanWritten, IntPtr.Zero));
        }
    }
}
