using System.Collections.ObjectModel;
using System.Linq;

using BeWo.Validation;

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

namespace BeWo.ViewModel
{
    public class GroupOfPeopleVM : AbstractDCMapperVM<GroupOfPeopleDC>
    {

        public static string PropertyName_EmployeeList = "EmployeeList";

        public static string PropertyName_SupportConceptList = "SupportConceptList";

        public static string PropertyName_CustomerList = "CustomerList";

        public static string PropertyName_Name = "Name";

        private ObservableCollection<CompactEmployeeDC> _EmployeeList;

        private ObservableCollection<CompactSupportConceptDC> _SupportConceptList;

        private ObservableCollection<CompactCustomerDC> _CustomerList;

        private string _Name;

        private long _CreatedByEmployeeOid;

        public GroupOfPeopleVM(GroupOfPeopleDC pDataContract)
            : base(pDataContract, pDataContract.GroupOfPeopleOid == null)
        {
        }


        public ObservableCollection<CompactEmployeeDC> EmployeeList
        {
            get { return this._EmployeeList; }

            set
            {
                if (this.AreDifferent(this._EmployeeList, value))
                {
                    this._EmployeeList = value;
                    this.FirePropertyChanged(PropertyName_EmployeeList);
                }
            }
        }

        public ObservableCollection<CompactSupportConceptDC> SupportConceptList
        {
            get { return this._SupportConceptList; }

            set
            {
                if (this.AreDifferent(this._SupportConceptList, value))
                {
                    this._SupportConceptList = value;
                    this.FirePropertyChanged(PropertyName_SupportConceptList);
                }
            }
        }

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

            set
            {
                if (this.AreDifferent(this._CustomerList, value))
                {
                    this._CustomerList = value;
                    this.FirePropertyChanged(PropertyName_CustomerList);
                }
            }
        }

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

            set
            {
                if (this.AreDifferent(this._Name, value))
                {
                    this._Name = value;
                    this.StoreDirtyInformation(this.DataContract.Name != value, PropertyName_Name);
                    this.FirePropertyChanged(PropertyName_Name);
                }
            }
        }

        public long CreatedByEmployeeOid
        {
            get { return this._CreatedByEmployeeOid; }
            set { this._CreatedByEmployeeOid = value; }
        }


        protected override void InitByDataContract(GroupOfPeopleDC pDataContract)
        {
            this._EmployeeList = new ObservableCollection<CompactEmployeeDC>();
            this._CustomerList = new ObservableCollection<CompactCustomerDC>();
            this._SupportConceptList = new ObservableCollection<CompactSupportConceptDC>();

            if (!this.IsNew)
            {
                this._Name = pDataContract.Name;
                this._CreatedByEmployeeOid = pDataContract.CreatedByEmployeeOid;
                
                if (pDataContract.EmployeeList != null)
                {
                    this._EmployeeList.AddRange(pDataContract.EmployeeList);
                }
                if (pDataContract.SupportConceptList != null)
                {
                    this._SupportConceptList.AddRange(pDataContract.SupportConceptList);
                }
                if (pDataContract.CustomerList != null)
                {
                    this._CustomerList.AddRange(pDataContract.CustomerList);
                }
            }

            this._EmployeeList.CollectionChanged += (s, e) => this.StoreDirtyInformation(!pDataContract.EmployeeList.ContainsSameItemsAs(this._EmployeeList), PropertyName_EmployeeList);
            this._CustomerList.CollectionChanged += (s, e) => this.StoreDirtyInformation(!pDataContract.CustomerList.ContainsSameItemsAs(this._CustomerList), PropertyName_CustomerList);
            this._SupportConceptList.CollectionChanged += (s, e) => this.StoreDirtyInformation(!pDataContract.SupportConceptList.ContainsSameItemsAs(this._SupportConceptList), PropertyName_SupportConceptList);
        }
        
        protected override GroupOfPeopleDC MapToDataContract(GroupOfPeopleDC pDataContract, bool doCommit)
        {
            pDataContract.Name = this._Name;
            pDataContract.CreatedByEmployeeOid = this._CreatedByEmployeeOid;
            pDataContract.SupportConceptList = this._SupportConceptList.ToList();
            pDataContract.CustomerList = this._CustomerList.ToList();
            pDataContract.EmployeeList = this._EmployeeList.ToList();

            return pDataContract;
        }

        public override string ToString()
        {
            return this.Name;
        }
    }
}