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

namespace NIDAQmx
{
    public class AO : BaseDevice
    {
        internal AO(DAQmxSession session, string deviceName) : base(session, deviceName)
        {
            PhysicalChans = GetPhysicalChans() ;
            SupportedOutputTypes = GetSupportedOutputTypes();
            Timing= new AOTiming(session, deviceName);
            Trigger = new AOTrigger(session, deviceName);
            Range = new AORange(session, deviceName);
        }
        private string[] GetPhysicalChans()
        {
            byte[] bytes = new byte[1024];
            _Session.CheckResult(_Session.GetDelegate<Internal.Internal.DAQmxGetDevAOPhysicalChans>()(DeviceName, ref bytes[0], (uint)bytes.Length));
            return System.Text.Encoding.UTF8.GetString(bytes).Trim('\0').Split(',').Select(x => x.Trim('\0')).Where(x => !string.IsNullOrEmpty(x)).ToArray();
        }
        private AOSupportedOutputTypes[] GetSupportedOutputTypes()
        {
            AOSupportedOutputTypes[] types = new AOSupportedOutputTypes[Enum.GetValues<AOSupportedOutputTypes>().Length];
            _Session.CheckResult(_Session.GetDelegate<Internal.Internal.DAQmxGetDevAOSupportedOutputTypes>()(DeviceName, ref types[0], (uint)types.Length));
            return types.Where(x=>x>0).ToArray();
        }
        public string[] PhysicalChans { get; }
        public AOSupportedOutputTypes[] SupportedOutputTypes { get; }
        public AOTiming Timing { get; }
        public AOTrigger Trigger { get; }
        public AORange Range { get; }
    }
}
