using System;
using System.Collections.Generic;
using BS.Shared;
using BS.Shared.DataContracts.Compact;
using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared.DataContracts;
using BeWo.Service.ServiceImplementations;

namespace BeWo.Service.DCEntityMapper
{
    public class GroupOfPeopleDC_GroupOfPeople : AbstractIDCEntityMapper<GroupOfPeople, GroupOfPeopleDC>
    {
        public override GroupOfPeopleDC MergeWithDC(GroupOfPeople pEntity, GroupOfPeopleDC pDataContract)
        {
            pDataContract.Name = pEntity.Name;
            pDataContract.GroupOfPeopleOid = pEntity.Oid;
            pDataContract.GroupOfPeopleVersion = pEntity.Version;
            pDataContract.GroupType = pEntity.GroupType;
            pDataContract.CreatedByEmployeeOid = pEntity.CreatedByEmployeeOid;
            switch (pEntity.GroupType)
            {
                case GroupTypeId.ServiceRecordSupportConcepts:
                    pDataContract.SupportConceptList = new List<CompactSupportConceptDC>();
                    foreach (var c2s in pEntity.CostBearer2SupportConceptList)
                    {
                        var dc = OperationsServiceImp.CreateFlatSupportConcept(c2s.SupportConcept, c2s);
                        pDataContract.SupportConceptList.Add(dc);
                    }

                    break;
                case GroupTypeId.ServiceRecordEmployees:
                    pDataContract.EmployeeList = MapperFactory.CompactEmployeeDC_Employee.MapToNewDCs(pEntity.EmployeeList);
                    break;
                case GroupTypeId.SupplementaryServiceCustomer:
                    pDataContract.CustomerList = MapperFactory.CompactCustomerDC_Customer.MapToNewDCs(pEntity.CustomerList);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return pDataContract;
        }

        public override GroupOfPeople MergeWithEntity(GroupOfPeopleDC pDataContract, GroupOfPeople pEntity)
        {
            this.ConcurrencyCheck(pDataContract.GroupOfPeopleVersion, pEntity);

            pEntity.Oid = pDataContract.GroupOfPeopleOid;
            pEntity.Name = pDataContract.Name;
            pEntity.GroupType = pDataContract.GroupType;
            pEntity.CreatedByEmployeeOid = pDataContract.CreatedByEmployeeOid;

            if (pDataContract.SupportConceptList != null)
            {
                pEntity.GroupType = GroupTypeId.ServiceRecordSupportConcepts;

                pEntity.CostBearer2SupportConceptList.Clear();

                if (pDataContract.SupportConceptList != null)
                {
                    foreach (var sc in pDataContract.SupportConceptList)
                    {
                        foreach (var cb2ScOid in sc.CostBearerRelOids)
                        {
                            CostBearer2SupportConcept c2s =
                                DAOFactory.GenericDAO.GetByID<CostBearer2SupportConcept>(cb2ScOid);
                            pEntity.CostBearer2SupportConceptList.Add(c2s);
                        }
                    }
                }
            }
            else if (pDataContract.EmployeeList != null)
            {
                pEntity.GroupType = GroupTypeId.ServiceRecordEmployees;
                MapperFactory.CompactEmployeeDC_Employee.MergeWithEntitys(pDataContract.EmployeeList, pEntity.EmployeeList);
            }
            else if (pDataContract.CustomerList != null)
            {
                pEntity.GroupType = GroupTypeId.SupplementaryServiceCustomer;
                MapperFactory.CompactCustomerDC_Customer.MergeWithEntitys(pDataContract.CustomerList, pEntity.CustomerList);
            }


            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(GroupOfPeopleDC pDC, GroupOfPeople pEntity)
        {
            if (pDC.GroupOfPeopleOid == null)
            {
                return false;
            }

            return pDC.GroupOfPeopleOid == pEntity.Oid;
        }
    }
}