using System.Collections.Generic;
using System.Linq;

using BeWo.Data.Access;
using BeWo.Data.Entities;

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

namespace BeWo.Service.DCEntityMapper
{
    public class SupportConceptDC_SupportConcept : AbstractIDCEntityMapper<SupportConcept, SupportConceptDC>
    {
        public override SupportConceptDC MergeWithDC(SupportConcept pEntity, SupportConceptDC pDataContract)
        {
            pDataContract.SupportConceptOid = pEntity.Oid;
            pDataContract.SupportConceptVersion = pEntity.Version;
            pDataContract.Conference = pEntity.Conference;

            pDataContract.ConferenceDate = pEntity.ConferenceDate;
            pDataContract.ApprovalDate = pEntity.ApprovalDate;
            pDataContract.RenewalSendDate = pEntity.RenewalSendDate;
            pDataContract.ConsultingNeeded = pEntity.ConsultingNeeded;
            pDataContract.RequisitionSendDate = pEntity.RequisitionSendDate;
            pDataContract.SupportConceptSendDate = pEntity.SupportConceptSendDate;
            pDataContract.ConferenceVenue = pEntity.ConferenceVenue;

            pDataContract.Customer = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(pEntity.Customer);
            pDataContract.Notice = pEntity.Notice;
            pDataContract.ActivationType = pEntity.IsActive;
            if (pEntity.Originator != null)
            {
                pDataContract.Originator = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Originator);
            }

            pDataContract.CostBearerRelations = MapperFactory.SupportConceptCostBearerRelDC_CostBearer2SupportConcept.MapToNewDCs(pEntity.CostBearer2SupportConceptList);
            var list = new List<ValueListEntryType>
            {
				ValueListEntryType.SupportConceptGoalCategoryType,
                ValueListEntryType.SupportConceptGoalType,
                ValueListEntryType.SupportConceptIndividualGoalType,
                ValueListEntryType.SupportConceptIndividualGoalCategoryType
            };
            pDataContract.Goals = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDCs(pEntity.ValueList.FindByTypes(list).Select(e2o => e2o.Entry));

            pDataContract.Tasks = MapperFactory.TaskDC_Task.MapToNewDCs(pEntity.TaskList);

            pDataContract.ServiceAccountings = MapperFactory.ServiceAccountingDC_ServiceAccounting.MapToNewDCs(pEntity.ServiceAccountings);

            return pDataContract;
        }

        public override SupportConcept MergeWithEntity(SupportConceptDC pDataContract, SupportConcept pEntity)
        {
            this.ConcurrencyCheck(pDataContract.SupportConceptVersion, pEntity);

            pEntity.Oid = pDataContract.SupportConceptOid;
            pEntity.Conference = pDataContract.Conference;
            pEntity.ConferenceDate = pDataContract.ConferenceDate;
            pEntity.ApprovalDate = pDataContract.ApprovalDate;
            pEntity.RenewalSendDate = pDataContract.RenewalSendDate;
            pEntity.ConsultingNeeded = pDataContract.ConsultingNeeded;
            pEntity.RequisitionSendDate = pDataContract.RequisitionSendDate;
            pEntity.SupportConceptSendDate = pDataContract.SupportConceptSendDate;
            pEntity.ConferenceVenue = pDataContract.ConferenceVenue;

            if (pEntity.Customer == null || pEntity.Customer.Oid.Value != pDataContract.Customer.CustomerOid)
            {
                pEntity.Customer = DAOFactory.GenericDAO.LoadByID<Customer>(pDataContract.Customer.CustomerOid);
            }

            // checkout & checkin & Fileupload über extra service...
            pEntity.IsActive = pDataContract.ActivationType;
            if (pDataContract.Originator != null)
            {
                pEntity.Originator = MapperFactory.CompactEmployeeDC_Employee.MergeWithEntity(pDataContract.Originator, pEntity.Originator);
            }

            pEntity.Notice = pDataContract.Notice;

            MapperFactory.SupportConceptCostBearerRelDC_CostBearer2SupportConcept.MergeWithEntitys(pDataContract.CostBearerRelations, pEntity.CostBearer2SupportConceptList);

			var globalGoalCategories = new List<ValueListEntryDC>();
            var globalGoals = new List<ValueListEntryDC>();
            var individualGoals = new List<ValueListEntryDC>();
            var individualGoalCategories = new List<ValueListEntryDC>();

            if (pDataContract.Goals != null)
            {
				globalGoalCategories =
					pDataContract.Goals.Where(g => g.Type == ValueListEntryType.SupportConceptGoalCategoryType).ToList();
                globalGoals =
                    pDataContract.Goals.Where(g => g.Type == ValueListEntryType.SupportConceptGoalType).ToList();
                individualGoals =
                    pDataContract.Goals.Where(g => g.Type == ValueListEntryType.SupportConceptIndividualGoalType).ToList();
                individualGoalCategories =
                    pDataContract.Goals.Where(g => g.Type == ValueListEntryType.SupportConceptIndividualGoalCategoryType).ToList();
            }

			MapperFactory.ValueListEntryDC_ValueListEntry.MapValueEntryListBack2Entity(globalGoalCategories, pEntity.ValueList, ValueListEntryType.SupportConceptGoalCategoryType, TableID.SupportConcept, false);
            MapperFactory.ValueListEntryDC_ValueListEntry.MapValueEntryListBack2Entity(globalGoals, pEntity.ValueList, ValueListEntryType.SupportConceptGoalType, TableID.SupportConcept, false);
            MapperFactory.ValueListEntryDC_ValueListEntry.MapValueEntryListBack2Entity(individualGoals, pEntity.ValueList, ValueListEntryType.SupportConceptIndividualGoalType, TableID.SupportConcept, true);
            MapperFactory.ValueListEntryDC_ValueListEntry.MapValueEntryListBack2Entity(individualGoalCategories, pEntity.ValueList, ValueListEntryType.SupportConceptIndividualGoalCategoryType, TableID.SupportConcept, true);

            MapperFactory.TaskDC_Task.MergeWithEntitys(pDataContract.Tasks, pEntity.TaskList);

            MapperFactory.ServiceAccountingDC_ServiceAccounting.MergeWithEntitys(pDataContract.ServiceAccountings, pEntity.ServiceAccountings);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(SupportConceptDC pDC, SupportConcept pEntity)
        {
            if (pDC.SupportConceptOid == null)
            {
                return false;
            }

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