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

namespace IACommService4CSharp
{
    public class DataItem_t : IDisposable
    {
        private IDataItem4CSharp m_item;
        private bool m_disposed = false;
        private bool m_isReference = false;
        public DataItem_t()
        {
            m_item = DataItemFactory4CSharp.Create();
            if (null == m_item)
                throw new IACommServiceException(ErrorCode.InternalError);
        }
        public DataItem_t(DataValue_t value, uint status = ErrorCode.Good, long time = 0)
        {
            m_item = DataItemFactory4CSharp.Create();
            if (null == m_item)
                throw new IACommServiceException(ErrorCode.InternalError);

            Value = value;
            Status = status;
            Time = time;
        }
        public DataItem_t(object value, uint status = ErrorCode.Good, long time = 0)
        {
            m_item = DataItemFactory4CSharp.Create();
            if (null == m_item)
                throw new IACommServiceException(ErrorCode.InternalError);

            var temp = new DataValue_t(value);

            Value = temp;
            Status = status;
            Time = time;

            temp.Dispose();
        }
        //Internal use
        public DataItem_t(IDataItem4CSharp item)
        {
            m_item = item;
            if (null == m_item)
                throw new IACommServiceException(ErrorCode.InternalError);
            m_isReference = true;
        }
        ~DataItem_t()
        {
            Dispose(false);
        }
        public uint Status
        {
            get
            {
                return m_item.Status();
            }
            set
            {
                m_item.Status(value);
            }
        }
        public long Time
        {
            get
            {
                return m_item.Time();
            }
            set
            {
                m_item.Time(value);
            }
        }
        public DataValue_t Value
        {
            get
            {
                return new DataValue_t(m_item.Value());
            }
            set
            {
                if (value is null)
                    throw new IACommServiceException(ErrorCode.InvalidArgument);

                uint status = m_item.Value(value.Internal);
                if (status != ErrorCode.Good)
                    throw new IACommServiceException(status);
            }
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposeManaged)
        {
            if (!m_disposed)
            {
                if (disposeManaged)
                {
                    //do nothing
                }

                if (!m_isReference)
                    DataItemFactory4CSharp.Destroy(m_item);

                m_item = null;

                m_disposed = true;
            }
        }
        public IDataItem4CSharp Internal
        {
            get
            {
                return m_item;
            }
        }
    }
}
