using System;
using System.Runtime.Serialization;
using BS.Shared.DataContracts.Compact;


namespace BS.Shared.DataContracts
{
    [DataContract]
    public class AppointmentDC : IDataContract
    {
        [DataMember]
        public long? AppointmentOid { get; set; }

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

        [DataMember]
        public Object ContextObject { get; set; }

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

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

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

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

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

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

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

        [DataMember]
        public bool AllDay { get; set; }

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

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

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

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

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

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

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

        [DataMember]
        public AppointmentCategoryDC AppointmentCategory { get; set; }

        public override bool Equals(object obj)
        {
            if (obj is AppointmentDC)
            {
                var y = (AppointmentDC) obj;
                if (AppointmentOid == null && y.AppointmentOid == null)
                {
                    return GetHashCode() == y.GetHashCode();
                }

                if (AppointmentOid != null && y.AppointmentOid != null)
                {
                    return AppointmentOid == y.AppointmentOid;
                }

                if (StartDate == y.StartDate && 
                    EndDate == y.EndDate && 
                    Type == y.Type && 
                    Subject == y.Subject && 
                    Description == y.Description && 
                    CustomerOid == y.CustomerOid && 
                    EmployeeOid == y.EmployeeOid)
                {
                    if (AppointmentCategory == null && y.AppointmentCategory == null)
                        return true;
                    if (AppointmentCategory != null && y.AppointmentCategory != null &&
                        AppointmentCategory.AppointmentCategoryOid == y.AppointmentCategory.AppointmentCategoryOid)
                        return true;
                }
            }

            return false;
        }

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