using System.Collections.ObjectModel;
using System.Linq;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Extensions;
using BeWo.Validation;

namespace BeWo.ViewModel
{
    public class AdditionalServiceGroupOfPeopleVM : AbstractDCMapperVM<AdditionalServiceGroupOfPeopleDC>
    {
        public static string PropertyName_CustomerList = "CustomerList";

        public static string PropertyName_Name = "Name";

        public static string PropertyName_AppointmentDayOfWeek = "AppointmentDayOfWeek";

        private ObservableCollection<CompactCustomerDC> _CustomerList;

        private string _Name;

        private AppointmentDayOfWeek _AppointmentDayOfWeek;


        public AdditionalServiceGroupOfPeopleVM(AdditionalServiceGroupOfPeopleDC pDataContract)
            : base(pDataContract, pDataContract.AdditionalServiceGroupOfPeopleOid == null)
        {
        }


        public ObservableCollection<CompactCustomerDC> CustomerList
        {
            get { return _CustomerList; }

            set
            {
                if (!AreDifferent(_CustomerList, value)) 
                    return;

                _CustomerList = value;
                FirePropertyChanged(PropertyName_CustomerList);
            }
        }

        [Validation(ValidationRule = ValidationRules.NotNullOrStringEmpty)]
        public string Name
        {
            get { return _Name; }

            set
            {
                if (!AreDifferent(_Name, value)) 
                    return;

                _Name = value;
                StoreDirtyInformation(DataContract.Name != value, PropertyName_Name);
                FirePropertyChanged(PropertyName_Name);
            }
        }

        public AppointmentDayOfWeek AppointmentDayOfWeek
        {
            get { return _AppointmentDayOfWeek; }

            set
            {
                if (!AreDifferent(_AppointmentDayOfWeek, value))
                    return;

                _AppointmentDayOfWeek = value;
                StoreDirtyInformation(DataContract.AppointmentDayOfWeek != value, PropertyName_AppointmentDayOfWeek);
                FirePropertyChanged(PropertyName_AppointmentDayOfWeek);
            }
        }

        protected override void InitByDataContract(AdditionalServiceGroupOfPeopleDC pDataContract)
        {
            _CustomerList = new ObservableCollection<CompactCustomerDC>();

            if (!IsNew)
            {
                _Name = pDataContract.Name;
                _AppointmentDayOfWeek = pDataContract.AppointmentDayOfWeek;

                if (pDataContract.CustomerList != null)
                    _CustomerList.AddRange(pDataContract.CustomerList);
            }


            _CustomerList.CollectionChanged += (s, e) => StoreDirtyInformation(!pDataContract.CustomerList.ContainsSameItemsAs(_CustomerList), PropertyName_CustomerList);
        }

        protected override AdditionalServiceGroupOfPeopleDC MapToDataContract(AdditionalServiceGroupOfPeopleDC pDataContract, bool doCommit)
        {
            pDataContract.Name = _Name;
            pDataContract.CustomerList = _CustomerList.ToList();
            pDataContract.AppointmentDayOfWeek = _AppointmentDayOfWeek;

            return pDataContract;
        }
    }
}
