using System;
using System.Collections.Generic;

using BeWo.ViewModel;

using BS.Shared.DataContracts;

namespace BeWo.Scheduler.ViewModel
{
    public class AbsenceTimeAppointmentVM : AbstractDCMapperVM<AbsenceTimeDC>, IBeWoAppointment
    {
        public static string PropertyName_End = "End";

        public static string PropertyName_Notice = "Notice";

        public static string PropertyName_Reason = "Reason";

        public static string PropertyName_Start = "Start";

        public static string PropertyName_EmployeeOid = "EmployeeOid";
        private long? _EmployeeOid;

        private DateTime _End;

        private string _Notice;

        private AbsenceReasonDC _Reason;

        private DateTime _Start;
        private Dictionary<string, object> mCustomFields;

        public AbsenceTimeAppointmentVM(AbsenceTimeDC pDC)
            : base(pDC, pDC.AbsenceTimeOid == null)
        {
        }

        public string Notice
        {
            get { return _Notice; }

            set
            {
                if (AreDifferent(_Notice, value))
                {
                    _Notice = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Notice, value), PropertyName_Notice);
                    FirePropertyChanged(PropertyName_Notice);
                }
            }
        }

        public AbsenceReasonDC Reason
        {
            get { return _Reason; }

            set
            {
                if (AreDifferent(_Reason, value))
                {
                    _Reason = value;
                    StoreDirtyInformation(!AreEqual(value, DataContract.Reason), PropertyName_Reason);
                    FirePropertyChanged(PropertyName_Reason);
                }
            }
        }

        public long? EmployeeOid
        {
            get { return _EmployeeOid; }

            set
            {
                if (AreDifferent(_EmployeeOid, value))
                {
                    _EmployeeOid = value;
                    StoreDirtyInformation(AreDifferent(DataContract.EmployeeOid, value), PropertyName_EmployeeOid);
                    FirePropertyChanged(PropertyName_EmployeeOid);
                }
            }
        }

        public DateTime End
        {
            get { return _End; }

            set
            {
                if (AreDifferent(_End, value))
                {
                    _End = value;
                    StoreDirtyInformation(AreDifferent(DataContract.End, value), PropertyName_End);
                    FirePropertyChanged(PropertyName_End);
                }
            }
        }

        public DateTime Start
        {
            get { return _Start; }

            set
            {
                if (AreDifferent(_Start, value))
                {
                    _Start = value;
                    StoreDirtyInformation(AreDifferent(DataContract.Start, value), PropertyName_Start);
                    FirePropertyChanged(PropertyName_Start);
                }
            }
        }


        public BeWoAppointmentCategory AppointmentCategory { get; set; }
        public object Id { get; set; }

        public int LabelId { get; set; }

        public string Subject
        {
            get
            {
                if (Reason != null)
                    return Reason.Description;
                return null;
            }
            set { }
        }

        public string Description
        {
            get { return Notice; }
            set { Notice = value; }
        }

        public int Status { get; set; }
        public string Location { get; set; }
        public bool AllDay { get; set; }
        public int EventType { get; set; }
        public String RecurrenceInfo { get; set; }
        public string ReminderInfo { get; set; }
        public object ResourceId { get; set; }

        public Dictionary<string, object> CustomFields
        {
            get
            {
                if (mCustomFields == null)
                    mCustomFields = new Dictionary<String, object>();
                return mCustomFields;
            }
        }

        protected override void InitByDataContract(AbsenceTimeDC pDataContract)
        {
            if (!IsNew)
            {
                _Start = pDataContract.Start ?? DateTime.MinValue;
                _End = pDataContract.End ?? DateTime.MaxValue;
                _Notice = pDataContract.Notice;

				if (_Notice == null || _Notice.Equals(""))
		            _Notice = pDataContract.Reason.Description;

                _Reason = pDataContract.Reason;
                _EmployeeOid = pDataContract.EmployeeOid;
            }
        }

        protected override AbsenceTimeDC MapToDataContract(AbsenceTimeDC pDataContract, bool doCommit)
        {
            if (_Reason != null)
            {
                pDataContract.Reason = _Reason;
            }

            pDataContract.Start = _Start;
            pDataContract.End = _End;
            pDataContract.Notice = _Notice;
			if (_Notice.Equals(""))
				pDataContract.Notice = Subject;
            pDataContract.EmployeeOid = _EmployeeOid;

            return pDataContract;
        }
    }
}