using System;
using System.Linq;

using BeWo.Validation;

using BS.Shared.DataContracts;
using BS.Shared.Extensions;

using System.Collections.Generic;

namespace BeWo.ViewModel
{
    public class ContractVM : AbstractDCMapperVM<ContractDC>
    {
        public static string PropertyName_ProbationPeriod = "ProbationPeriod";
        public static string PropertyName_WeeklyDays = "WeeklyDays";
        public static string PropertyName_CancellationPeriod = "CancellationPeriod";
        public static string PropertyName_EntryDate = "EntryDate";
        public static string PropertyName_ContractEndDate = "ContractEndDate";
        public static string PropertyName_HourlyRate = "HourlyRate";
        public static string PropertyName_WeeklyFLS = "WeeklyFLS";
        public static string PropertyName_WeeklyTotalHours = "WeeklyTotalHours";
        public static string PropertyName_LeaveDays = "LeaveDays";
        public static string PropertyName_Notice = "Notice";
        public static string PropertyName_EmploymentType = "EmploymentType";
        public static string PropertyName_PossibleEmploymentTypes = "PossibleEmploymentTypes";
        public static string PropertyName_ContractString = "ContractString";

        private decimal? _LeaveDays;
        private decimal? _WeeklyTotalHours;
        private decimal? _WeeklyFLS;
        private decimal? _HourlyRate;
        private DateTime? _EntryDate;
        private DateTime? _ContractEndDate;
        private int _CancellationPeriod;
        private int _ProbationPeriod;
        private int _WeeklyDays;
        private String _Notice;
        private EmploymentTypeDC _EmploymentType;
        

        private IList<EmploymentTypeDC> _PossibleEmploymentTypes;
        
        public ContractVM(ContractDC pDC)
            : base(pDC, pDC.ContractOid == null)
        {
        }

        public string ContractString
        {
            get
            {
                String text = "";
                if (this.EntryDate.HasValue)
                {
                    text = String.Format("Vertrag vom {0:dd.MM.yyyy}", this.EntryDate);

                    if (this.ContractEndDate.HasValue)
                    {
                        text += String.Format(" - {0:dd.MM.yyyy}", this.ContractEndDate);
                    }
                }
                else
                {
                    if (this.ContractEndDate.HasValue)
                    {
                        text = String.Format("Vertrag bis {0:dd.MM.yyyy}", this.ContractEndDate);
                    }
                }
                if (String.IsNullOrEmpty(text))
                {
                    if (this.IsNew)
                        text = "Neuer Vertrag";
                    else
                        text = "Vertrag";
                }
                return text;
            }
           
            set { }
        }

        public IList<EmploymentTypeDC> PossibleEmploymentTypes
        {
            get { return this._PossibleEmploymentTypes; }
            set { this._PossibleEmploymentTypes= value; }
        }

        

        public EmploymentTypeDC EmploymentType
        {
            get
            {
                return this._EmploymentType;
            }

            set
            {
                if (this.AreDifferent(this._EmploymentType, value))
                {
                    this._EmploymentType = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.EmploymentType, value), PropertyName_EmploymentType);
                    this.FirePropertyChanged(PropertyName_EmploymentType);
                }
            }
        }

        public decimal? LeaveDays
        {
            get { return this._LeaveDays; }

            set
            {
                if (this.AreDifferent(this._LeaveDays, value))
                {
                    this._LeaveDays = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.LeaveDays, value), PropertyName_LeaveDays);
                    this.FirePropertyChanged(PropertyName_LeaveDays);
                }
            }
        }

        public decimal? WeeklyTotalHours
        {
            get { return this._WeeklyTotalHours; }

            set
            {
                if (this.AreDifferent(this._WeeklyTotalHours, value))
                {
                    this._WeeklyTotalHours = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.WeeklyTotalHours, value), PropertyName_WeeklyTotalHours);
                    this.FirePropertyChanged(PropertyName_WeeklyTotalHours);
                }
            }
        }

        public decimal? WeeklyFLS
        {
            get { return this._WeeklyFLS; }

            set
            {
                if (this.AreDifferent(this._WeeklyFLS, value))
                {
                    this._WeeklyFLS = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.WeeklyFLS, value), PropertyName_WeeklyFLS);
                    this.FirePropertyChanged(PropertyName_WeeklyFLS);
                }
            }
        }

        public decimal? HourlyRate
        {
            get { return this._HourlyRate; }

            set
            {
                if (this.AreDifferent(this._HourlyRate, value))
                {
                    this._HourlyRate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.HourlyRate, value), PropertyName_HourlyRate);
                    this.FirePropertyChanged(PropertyName_HourlyRate);
                }
            }
        }

        public DateTime? EntryDate
        {
            get { return this._EntryDate; }

            set
            {
                if (this.AreDifferent(this._EntryDate, value))
                {
                    this._EntryDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.EntryDate, value), PropertyName_EntryDate);
                    this.FirePropertyChanged(PropertyName_EntryDate);
                    this.FirePropertyChanged(PropertyName_ContractString);
                }
            }
        }

        public DateTime? ContractEndDate
        {
            get { return this._ContractEndDate; }

            set
            {
                if (this.AreDifferent(this._ContractEndDate, value))
                {
                    this._ContractEndDate = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ContractEndDate, value), PropertyName_ContractEndDate);
                    this.FirePropertyChanged(PropertyName_ContractEndDate);
                    this.FirePropertyChanged(PropertyName_ContractString);
                }
            }
        }

        public int CancellationPeriod
        {
            get { return this._CancellationPeriod; }

            set
            {
                if (this.AreDifferent(this._CancellationPeriod, value))
                {
                    this._CancellationPeriod = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.CancellationPeriod, value), PropertyName_CancellationPeriod);
                    this.FirePropertyChanged(PropertyName_CancellationPeriod);
                }
            }
        }

        public int ProbationPeriod
        {
            get { return this._ProbationPeriod; }

            set
            {
                if (this.AreDifferent(this._ProbationPeriod, value))
                {
                    this._ProbationPeriod = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.ProbationPeriod, value), PropertyName_ProbationPeriod);
                    this.FirePropertyChanged(PropertyName_ProbationPeriod);
                }
            }
        }

        public int WeeklyDays
        {
            get { return this._WeeklyDays; }

            set
            {
                if (this.AreDifferent(this._WeeklyDays, value))
                {
                    this._WeeklyDays = value;
                    this.StoreDirtyInformation(this.AreDifferent(this.DataContract.WeeklyDays, value), PropertyName_WeeklyDays);
                    this.FirePropertyChanged(PropertyName_WeeklyDays);
                }
            }
        }

        public string Notice
        {
            get { return this._Notice; }

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

       

        protected override void InitByDataContract(ContractDC pDataContract)
        {
            if (!this.IsNew)
            {
                this._CancellationPeriod = pDataContract.CancellationPeriod;
                this._ContractEndDate = pDataContract.ContractEndDate;
                this._EntryDate = pDataContract.EntryDate;
                this._HourlyRate = pDataContract.HourlyRate;
                this._LeaveDays = pDataContract.LeaveDays;
                this._ProbationPeriod = pDataContract.ProbationPeriod;
                this._WeeklyDays = pDataContract.WeeklyDays;
                this._WeeklyFLS = pDataContract.WeeklyFLS;
                this._WeeklyTotalHours = pDataContract.WeeklyTotalHours;
                this._Notice = pDataContract.Notice;
                this._EmploymentType = pDataContract.EmploymentType;
            }
        }

        protected override ContractDC MapToDataContract(ContractDC pDataContract, bool doCommit)
        {
            pDataContract.CancellationPeriod = this._CancellationPeriod;
            pDataContract.ContractEndDate = this._ContractEndDate;
            pDataContract.EntryDate = this._EntryDate;
            pDataContract.HourlyRate = this._HourlyRate;
            pDataContract.LeaveDays = this._LeaveDays;
            pDataContract.ProbationPeriod = this._ProbationPeriod;
            pDataContract.WeeklyDays = this._WeeklyDays;
            pDataContract.WeeklyFLS = this._WeeklyFLS;
            pDataContract.WeeklyTotalHours = this._WeeklyTotalHours;
            pDataContract.Notice = this._Notice;

            pDataContract.EmploymentType = this._EmploymentType;
            
            return pDataContract;
        }
    }
}