using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

using BS.Shared.DataContracts.Compact;

namespace BS.Shared.DataContracts
{
    [DataContract]
    public class BookingSequenceDC : IDataContract
    {
        [DataMember]
        public List<ResourceBookingDC> ChangedElements { get; set; }

        [DataMember]
        public CompactEmployeeDC Employee { get; set; }

        [DataMember]
        public ResourceBookingFrequencyType Frequency { get; set; }

        [DataMember]
        public string Notice { get; set; }

        [DataMember]
        public int RepeatInterval { get; set; }

        [DataMember]
        public ResourceDC Resource { get; set; }

        [DataMember]
        public ResourceBookingDC RootBooking { get; set; }

        [DataMember]
        public DateTime SequenceEnd { get; set; }

        [DataMember]
        public long? SequenceOid { get; set; }

        [DataMember]
        public long? SequenceVersion { get; set; }

        public override bool Equals(object obj)
        {
            var dc = obj as BookingSequenceDC;
            if (dc != null)
            {
                var y = dc;
                if(SequenceOid == null && y.SequenceOid == null)
                    return GetHashCode() == y.GetHashCode();

                if (SequenceOid != null && y.SequenceOid != null)
                    return SequenceOid == y.SequenceOid;

                if (SequenceEnd == y.SequenceEnd &&
                    RootBooking == y.RootBooking &&
                    Notice == y.Notice &&
                    RepeatInterval == y.RepeatInterval &&
                    Employee == y.Employee)
                {
                    if (Resource == null && y.Resource == null)
                        return true;

                    if (Resource != null && y.Resource != null && Resource == y.Resource)
                        return true;
                }
            }

            return false;
        }

        public override int GetHashCode()
        {
            return SequenceOid.HasValue
                ? GetType().Name.GetHashCode() ^ SequenceOid.GetHashCode()
                : base.GetHashCode();
        }
    }
}