using RFID_Sample.Annotations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace RFID_Sample
{
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class OrderAttribute : Attribute
    {
        private readonly int order_;
        public OrderAttribute([CallerLineNumber]int order = 0)
        {
            order_ = order;
        }

        public int Order { get { return order_; } }
    }
    [StructLayout(LayoutKind.Sequential,Pack = 1)]
    public class ReceivedTagData : INotifyPropertyChanged
    {
        private string _EPC;
        
        private long _Count;
        private sbyte _RSSI;
        private short _PC;
        private DateTime _LastSeen;
        private DateTime _FirstSeen;
        private long _Antenna;

        [Order]
        public string EPC
        {
            get { return _EPC; }
            set
            {
                if (_EPC != value)
                {
                    _EPC = value;
                    OnPropertyChanged();
                }
            }
        }

        
        [Order]
        public long Count
        {
            get { return _Count; }
            set
            {
                if (_Count != value)
                {
                    _Count = value;
                    OnPropertyChanged();
                }
                    
            }
        }

        [Order]
        public sbyte RSSI
        {
            get { return _RSSI; }
            set
            {
                if (_RSSI != value)
                {
                    _RSSI = value;
                    OnPropertyChanged();
                }
            }
        }

        [Order]
        public short PC
        {
            get { return _PC; }
            set
            {
                if (_PC != value)
                {
                    _PC = value;
                    OnPropertyChanged();
                }
            }
        }

        [Order]
        public DateTime LastSeen
        {
            get { return _LastSeen; }
            set
            {
                if (_LastSeen != value)
                {
                    _LastSeen = value;
                    OnPropertyChanged();
                }
            }
        }

        [Order]
        public DateTime FirstSeen
        {
            get { return _FirstSeen; }
            set
            {
                if (_FirstSeen != value)
                {
                    _FirstSeen = value;
                    OnPropertyChanged();
                }
            }
        }

        [Order]
        public long Antenna
        {
            get { return _Antenna; }
            set
            {
                if (_Antenna != value)
                {
                    _Antenna = value;
                    OnPropertyChanged();
                }
            }
        }


        public ReceivedTagData(string epc,
            long count,
            short rssi,
            DateTime lastSeen,
            DateTime firstSeen,
            long antenna,
            short pc)
        {
            try
            {
                EPC = epc;
                Count = count;
                RSSI = (sbyte) (rssi*0.1);
                LastSeen = lastSeen;
                FirstSeen = firstSeen;
                Antenna = antenna;
                PC = pc;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}