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

namespace NIDAQmx
{
    public class Device : BaseDevice
    {
        internal Device(DAQmxSession session, string deviceName) : base(session, deviceName)
        {
            IsSimulated = GetIsSimulated();
            byte[] bytes = new byte[10240];
            _Session.CheckResult(_Session.GetDelegate<Internal.Internal.DAQmxGetDevTerminals>()(deviceName, ref bytes[0], (uint)bytes.Length));
            Terminals= Encoding.UTF8.GetString(bytes).Split(",").Select(x => x.Trim('\0')).Where(x => !String.IsNullOrEmpty(x) && x.IndexOf("None") < 0).ToArray();
            Product = new Product(session, deviceName);
            Class = new Class(session, deviceName);
            Trigger = new Trigger(session, deviceName);
            IO = new IO(session, deviceName);
            HWTEDSSupported = GetHWTEDSSupported();
            NumDMAChans = GetNumDMAChans();
            Location= new Location(session, deviceName);
        }
        private bool GetIsSimulated()
        {
            uint data = 0;
            _Session.CheckResult(_Session.GetDelegate<Internal.Internal.DAQmxGetDevIsSimulated>()(DeviceName, ref data));
            return Convert.ToBoolean(data);
        }
        private bool GetHWTEDSSupported()
        {
            uint data = 0;
            _Session.CheckResult(_Session.GetDelegate<Internal.Internal.DAQmxGetDevTEDSHWTEDSSupported>()(DeviceName, ref data));
            return Convert.ToBoolean(data);
        }
        private uint GetNumDMAChans()
        {
            uint data = 0;
            _Session.CheckResult(_Session.GetDelegate<Internal.Internal.DAQmxGetDevNumDMAChans>()(DeviceName, ref data));
            return data;
        }
        public override string ToString() => DeviceName;
        public Boolean IsSimulated { get; }
        public string[] Terminals { get; }
        public Product Product { get; }
        public Class Class { get; }
        public Trigger Trigger { get; }
        public IO IO { get; }
        public bool HWTEDSSupported { get;  }
        public uint NumDMAChans { get; }
        public Location Location { get; }
    }
}
