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

using BeWo.Data.Entities;

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

namespace BeWo.Service.DCEntityMapper
{
    public class EmployeeDC_Employee : AbstractIDCEntityMapper<Employee, EmployeeDC>
    {
        public override EmployeeDC MergeWithDC(Employee pEntity, EmployeeDC pDataContract)
        {
            MapperFactory.PersonDC_Person.MergeWithDC(pEntity.Person, pDataContract);


            pDataContract.RelatedCustomers = new List<CustomerEmployeeRelationDC>();
            foreach (var e2c in pEntity.Employee2CustomerList)
            {
                if (e2c.Customer.IsActive == ActivationTypeId.Active)
                {
                    pDataContract.RelatedCustomers.Add(MapperFactory.EmployeeCustomerRelDC_Employee2CustomerMapper.MapToNewDC(e2c));
                }
            }
            
            pDataContract.Qualifications = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDCs(pEntity.ValueList.FindByType(ValueListEntryType.StaffQualificationsType).Select(e2o => e2o.Entry));

            pDataContract.Focuses = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDCs(pEntity.ValueList.FindByType(ValueListEntryType.StaffActivityFocusType).Select(e2o => e2o.Entry));

			pDataContract.Overtimes = MapperFactory.OvertimeDC_Overtime.MapToNewDCs(pEntity.Overtimes);
            
            pDataContract.EmployeeOid = pEntity.Oid;
            pDataContract.HealthInsurance = pEntity.HealthInsurance;
            pDataContract.InsuranceNumber = pEntity.InsuranceNumber;
            pDataContract.PersonnelNumber = pEntity.PersonnelNumber;
            pDataContract.TaxNumber = pEntity.TaxNumber;
            pDataContract.EmployeeVersion = pEntity.Version;
            pDataContract.IsQualified = pEntity.IsQualified;
            pDataContract.EmployeeColor = pEntity.EmployeeColor;
            pDataContract.Text1 = pEntity.Text1;
            pDataContract.Text2 = pEntity.Text2;
            pDataContract.Text3 = pEntity.Text3;
            pDataContract.Text4 = pEntity.Text4;
            pDataContract.Text5 = pEntity.Text5;

            pDataContract.Notice2 = pEntity.Notice2;
			pDataContract.ActivationType = pEntity.IsActive;

            //pDataContract.ProbationPeriod = pEntity.ProbationPeriod;
            //pDataContract.CancellationPeriod = pEntity.CancellationPeriod;
            //pDataContract.EntryDate = pEntity.EntryDate;
            //pDataContract.HourlyRate = pEntity.HourlyRate;
            //pDataContract.WeeklyFLS = pEntity.WeeklyFLS;
            //pDataContract.WeeklyTotalHours = pEntity.WeeklyTotalHours;
            //pDataContract.LeaveDays = pEntity.LeaveDays;

            //ValueListEntry2Object lEmploymentType = pEntity.ValueList.FindSingleByType(ValueListEntryType.EmploymentType);

            //if (lEmploymentType != null)
            //{
            //    pDataContract.EmploymentType = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(lEmploymentType.Entry);
            //}

            pDataContract.AbsenceTimes = MapperFactory.AbsenceTimeDC_AbsenceTime.MapToNewDCs(pEntity.AbsenceTimes);

            pDataContract.Contracts = MapperFactory.ContractDC_Contract.MapToNewDCs(pEntity.Contracts);

            if (pDataContract.Contracts != null)
            {
                pDataContract.Contracts.Sort((c1, c2)
                    =>
                    {
                        if (!c1.EntryDate.HasValue)
                            return 1;
                        else if (!c2.EntryDate.HasValue)
                            return -1;
                        else
                        {
                            return c1.EntryDate.Value.CompareTo(c2.EntryDate.Value);
                        }
                    });
            }
            //if (pDataContract.Contracts == null || pDataContract.Contracts.Count == 0)
            //{
            //    if (pEntity.CancellationPeriod != 0 || pEntity.ProbationPeriod != 0 || pEntity.EntryDate.HasValue || pEntity.HourlyRate.HasValue  || pEntity.WeeklyFLS.HasValue || pEntity.WeeklyTotalHours.HasValue || pEntity.LeaveDays.HasValue)
            //    {
            //        ContractDC contract = new ContractDC();
            //        contract.CancellationPeriod = pEntity.CancellationPeriod;
            //        contract.EntryDate = pEntity.EntryDate;
            //        contract.HourlyRate = pEntity.HourlyRate;
            //        contract.LeaveDays = pEntity.LeaveDays;
            //        contract.ProbationPeriod = pEntity.ProbationPeriod;
            //        contract.WeeklyFLS = pEntity.WeeklyFLS;
            //        contract.WeeklyTotalHours = pEntity.WeeklyTotalHours;

            //        pDataContract.Contracts.Add(contract);
            //    }
            //} 
            //else 
            //{
                
            //}

            return pDataContract;
        }

        public override Employee MergeWithEntity(EmployeeDC pDataContract, Employee pEntity)
        {
            ConcurrencyCheck(pDataContract.EmployeeVersion, pEntity);

            if (pDataContract.ContainsPersonData())
            {
                if (pEntity.Person == null)
                {
                    pEntity.Person = new Person();
                }

                MapperFactory.PersonDC_Person.MergeWithEntity(pDataContract, pEntity.Person);
            }

            var e2cList = new List<Employee2Customer>();
            foreach (var e2c in pEntity.Employee2CustomerList)
            {
                if (e2c.Customer.IsActive != ActivationTypeId.Active)
                    e2cList.Add(e2c);
            }
            MapperFactory.EmployeeCustomerRelDC_Employee2CustomerMapper.MergeWithEntitys(pDataContract.RelatedCustomers, pEntity.Employee2CustomerList);
            foreach (var e2c in e2cList)
            {
                pEntity.Employee2CustomerList.Add(e2c);
            }
            
            MapperFactory.ValueListEntryDC_ValueListEntry.MapValueEntryListBack2Entity(pDataContract.Qualifications, pEntity.ValueList, ValueListEntryType.StaffQualificationsType, TableID.Employee, false);

            MapperFactory.ValueListEntryDC_ValueListEntry.MapValueEntryListBack2Entity(pDataContract.Focuses, pEntity.ValueList, ValueListEntryType.StaffActivityFocusType, TableID.Employee, false);

			MapperFactory.OvertimeDC_Overtime.MergeWithEntitys(pDataContract.Overtimes, pEntity.Overtimes);

            pEntity.Oid = pDataContract.EmployeeOid;
            pEntity.HealthInsurance = pDataContract.HealthInsurance;
            pEntity.InsuranceNumber = pDataContract.InsuranceNumber;
            pEntity.PersonnelNumber = pDataContract.PersonnelNumber;
            pEntity.TaxNumber = pDataContract.TaxNumber;
            pEntity.IsQualified = pDataContract.IsQualified;
            pEntity.EmployeeColor = pDataContract.EmployeeColor;
            pEntity.Text1 = pDataContract.Text1;
            pEntity.Text2 = pDataContract.Text2;
            pEntity.Text3 = pDataContract.Text3;
            pEntity.Text4 = pDataContract.Text4;
            pEntity.Text5 = pDataContract.Text5;

            pEntity.Notice2 = pDataContract.Notice2;

			pEntity.IsActive = pDataContract.ActivationType;

            //pEntity.CancellationPeriod = pDataContract.CancellationPeriod;
            //pEntity.EntryDate = pDataContract.EntryDate;
            //pEntity.ProbationPeriod = pDataContract.ProbationPeriod;
            //pEntity.HourlyRate = pDataContract.HourlyRate;
            //pEntity.LeaveDays = pDataContract.LeaveDays;
            //pEntity.WeeklyTotalHours = pDataContract.WeeklyTotalHours;
            //pEntity.WeeklyFLS = pDataContract.WeeklyFLS;
            //MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.EmploymentType, pEntity.ValueList, ValueListEntryType.EmploymentType, TableID.Employee);

            MapperFactory.AbsenceTimeDC_AbsenceTime.MergeWithEntitys(pDataContract.AbsenceTimes, pEntity.AbsenceTimes);

            MapperFactory.ContractDC_Contract.MergeWithEntitys(pDataContract.Contracts, pEntity.Contracts);

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(EmployeeDC pDC, Employee pEntity)
        {
            if (pDC.EmployeeOid == null)
            {
                return false;
            }

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