///bookedActivites gives us the list of completely booked activities
        ///activities could come across as available if:
        ///1. firstly based on availablity
        ///2. based on number of participants(total number of people that have already booked and are currently booking)
        ///3. based on time 
        ///4. check for other user booked activity to see if feasible
        ///5. check any other factors                
        public bool IsActivityAvailable(string activityPricingKey, DateTime bookingDate, string bookingTime,
            IEnumerable<ActivityBooking> bookedActivites, int adults, int children,  ActivitiesMaster activity)
        {
            bool available = true;
            if (available)
            {
                ///Getting total number of participants that have already
                ///booked the activity for the date and time selected
                ///by the user
                ///
                IActivityPriceMappingRepository activityPriceMappingRepository = _DataRepositoryFactory.GetDataRepository<IActivityPriceMappingRepository>();
                ActivityPriceMapping activityPricing = activityPriceMappingRepository.GetAllPriceOptionsForSelectedActivity(activity.ActivitesKey).FirstOrDefault();

                int numAdultParticipantsAlreadyPresent = bookedActivites.Where(item => item.ActivityPricingKey == activityPricingKey)
                    .Where(item => item.Time == bookingTime).Where(item => item.BookingDate == bookingDate).Sum(item => item.Participants);

                int numChildParticipantsAlreadyPresent = bookedActivites.Where(item => item.ActivityPricingKey == activityPricingKey)
                    .Where(item => item.Time == bookingTime).Where(item => item.BookingDate == bookingDate).Sum(item => item.ChildParticipants);

                adults = adults + numAdultParticipantsAlreadyPresent;
                children = children + numChildParticipantsAlreadyPresent;

                int totalNumberOfParticipants = adults + children;

                if ((activityPricing.NumAdults + activityPricing.NumChild) < totalNumberOfParticipants || activity.MaxUnits < totalNumberOfParticipants)
                {
                    available = false;
                }
            }
            return available;
        }
        public void SaveActivityDetails(ActivityDetailsDataContract activity, Dictionary<string, bool> activityDays,
            IEnumerable<string> activityTimes, string locationKey, string activityTypeKey, string user)
        {
            ExecuteFaultHandledOperation(() =>
            {
                IActivitiesMasterRepository activitiesRepository = _DataRepositoryFactory.GetDataRepository<IActivitiesMasterRepository>();
                IActivityDaySchedulerRepository activityDaySchedulerRepository = _DataRepositoryFactory.GetDataRepository<IActivityDaySchedulerRepository>();
                IActivityTimeSchedulerRepository activityTimeSchedulerRepository = _DataRepositoryFactory.GetDataRepository<IActivityTimeSchedulerRepository>();
                IActivityDatesRepository activityDatesRepository = _DataRepositoryFactory.GetDataRepository<IActivityDatesRepository>();
                IActivityPriceMappingRepository activityPriceMappingRepository = _DataRepositoryFactory.GetDataRepository<IActivityPriceMappingRepository>();
                IActivityTagMappingRepository activityTagRepository = _DataRepositoryFactory.GetDataRepository<IActivityTagMappingRepository>();
                ActivitiesMaster activitiesMaster = new ActivitiesMaster();
                activitiesMaster.ActivityEndTime = activity.ActivityEndTime;
                activitiesMaster.ActivityLocation = activity.LatLong;
                activitiesMaster.ActivityStartTime = activity.ActivityStartTime;
                activitiesMaster.CancellationPolicy = activity.CancellationPolicy;
                activitiesMaster.Address = activity.Location;
                activitiesMaster.AverageUserRating = activity.UserRating;
                activitiesMaster.DifficultyRating = activity.DifficultyRating;
                activitiesMaster.AllowInstantBooking = activity.AllowInstantBooking;
                activitiesMaster.Currency = activity.Currency;
                activitiesMaster.Description = activity.Description;
                activitiesMaster.MinAdults = activity.MinPeople;
                activitiesMaster.MinChildren = activity.MinChildren;
                activitiesMaster.Name = activity.Name;
                activitiesMaster.CostForChild = activity.CostForChild;
                activitiesMaster.Duration = activity.Duration;
                activitiesMaster.MaxAdults = activity.NumAdults;
                activitiesMaster.MaxChildren = activity.NumChildren;
                activitiesMaster.NumAdults = activity.NumAdults;
                activitiesMaster.NumChildren = activity.NumChildren;
                activitiesMaster.LocationKey = locationKey;
                activitiesMaster.ActivityTypeKey = activityTypeKey;
                activitiesMaster.CreatedDate = DateTime.Now;
                activitiesMaster.CreatedBy = user;
                activitiesMaster.IsPermitRequired = activity.IsPermitRequired;
                activitiesMaster.Included = activity.Included;
                activitiesMaster.Advice = activity.Advice;
                activitiesMaster.Comission = activity.Comission;
                activitiesMaster.ThingsToCarry = activity.ThingsToCarry;
                if (activity.ActivityKey == default(string))
                {
                    activitiesMaster.ActivitesKey = Guid.NewGuid().ToString();
                    //This has to be changed later
                    activitiesMaster.IsValidated = true;
                    activitiesRepository.Add(activitiesMaster);
                }
                else
                {
                    activitiesMaster.ActivitesKey = activity.ActivityKey;
                    //This has to be changed later
                    activitiesMaster.IsValidated = true;
                    activitiesRepository.Update(activitiesMaster);
                }
                //Getting existing tags and removing it
                List<ActivityTagMapping> activityTags = activityTagRepository.GetTagsForSelectedActivity(activity.ActivityKey);
                if (activityTags.Count() > 0)
                {
                    foreach (var item in activityTags)
                    {
                        activityTagRepository.Remove(item);
                    }
                }
                //Adding fresh tags
                if (activity.Tags.Count() > 0)
                {
                    foreach (var item in activity.Tags)
                    {
                        ActivityTagMapping tag = new ActivityTagMapping
                        {
                            ActivityTagKey = Guid.NewGuid().ToString(),
                            ActivityKey = activity.ActivityKey,
                            Tag = item
                        };
                        activityTagRepository.Add(tag);
                    }
                }
                ActivityDayScheduler dayScheduler = activityDaySchedulerRepository.Get().Where(e => e.ActivityKey == activitiesMaster.ActivitesKey).FirstOrDefault();
                if (dayScheduler == null)
                {
                    dayScheduler = new ActivityDayScheduler();
                    dayScheduler.ActivityDaySchedulerKey = Guid.NewGuid().ToString();
                    dayScheduler.ActivityKey = activitiesMaster.ActivitesKey;
                }

                IEnumerable<ActivityPriceMapping> allPriceMappings = activityPriceMappingRepository.GetAllPriceOptionsForSelectedActivity(activitiesMaster.ActivitesKey);

                if (allPriceMappings != null)
                {
                    foreach (var item in allPriceMappings)
                    {
                        if (activity.AllPriceOptions != null && activity.AllPriceOptions.Any(e => e.ActivityPricingKey == item.ActivityPricingKey))
                        {
                            item.CommissionAmount = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).CommissionAmount;
                            item.CommissionPercentage = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).CommissionPercentage;
                            item.CreatedDate = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).CreatedDate;
                            item.CreatedBy = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).CreatedBy;
                            item.NumAdults = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).NumAdults;
                            item.NumChild = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).NumChild;
                            item.OptionDescription = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).OptionDescription;
                            item.PriceForAdults = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).PriceForAdults;
                            item.PriceForChildren = activity.AllPriceOptions.FirstOrDefault(e => e.ActivityPricingKey == item.ActivityPricingKey).PriceForChildren;
                            activityPriceMappingRepository.Update(item);
                        }
                        else
                        {
                            activityPriceMappingRepository.Remove(item);
                        }
                    }
                }

                if (activity.AllPriceOptions != null && activity.AllPriceOptions.Count() > 0)
                {
                    foreach (var item in activity.AllPriceOptions)
                    {
                        if (string.IsNullOrEmpty(item.ActivityPricingKey))
                        {
                            item.ActivityPricingKey = Guid.NewGuid().ToString();
                            item.ActivityKey = activitiesMaster.ActivitesKey;
                            item.CreatedBy = user;
                            item.CreatedDate = DateTime.Now;
                            activityPriceMappingRepository.Add(item);
                        }
                    }
                }

                if (!activity.IsEvent)
                {
                    int count = 0;
                    foreach (var day in activityDays)
                    {
                        switch (count)
                        {
                            case 0: dayScheduler.IsSunday = day.Value; break;
                            case 1: dayScheduler.IsMonday = day.Value; break;
                            case 2: dayScheduler.IsTuesday = day.Value; break;
                            case 3: dayScheduler.IsWednesday = day.Value; break;
                            case 4: dayScheduler.IsThursday = day.Value; break;
                            case 5: dayScheduler.IsFriday = day.Value; break;
                            case 6: dayScheduler.IsSaturday = day.Value; break;
                        }
                        count++;
                    }

                    if (activityDaySchedulerRepository.Get(dayScheduler.ActivityDaySchedulerKey) != null)
                    {
                        activityDaySchedulerRepository.Update(dayScheduler);
                    }
                    else
                    {
                        activityDaySchedulerRepository.Add(dayScheduler);
                    }

                    List<ActivityTimeScheduler> timeScheduler = activityTimeSchedulerRepository.Get().Where(e => e.ActivityKey == activitiesMaster.ActivitesKey).ToList();
                    if (timeScheduler != null && timeScheduler.Count() > 0)
                    {
                        foreach (ActivityTimeScheduler item in timeScheduler)
                        {
                            activityTimeSchedulerRepository.Remove(item);
                        }
                    }
                    //setting activity times after clearing the old time values
                    //creating new activiy times
                    timeScheduler = new List<ActivityTimeScheduler>();
                    foreach (string time in activityTimes)
                    {
                        ActivityTimeScheduler newTime = new ActivityTimeScheduler() { ActivityTimeSchedulerKey = Guid.NewGuid().ToString() };
                        newTime.ActivityKey = activitiesMaster.ActivitesKey;
                        newTime.ActivityTime = time;
                        activityTimeSchedulerRepository.Add(newTime);
                    }
                }
                else
                {
                    List<ActivityDates> allActivityDates = activityDatesRepository.Get().Where(e => e.ActivityKey == activitiesMaster.ActivitesKey).ToList();
                    if (allActivityDates.Count() > 0)
                    {
                        foreach (var item in allActivityDates)
                        {
                            activityDatesRepository.Remove(item);
                        }
                    }
                    foreach (var item in activity.AllActivityUniqueDates)
                    {
                        item.ActivityDatesKey = Guid.NewGuid().ToString();
                        item.ActivityKey = activitiesMaster.ActivitesKey;
                        activityDatesRepository.Add(item);
                    }
                }
                if (activity.AllTopOffers.Count() > 0)
                {
                    ITopOffersRepository topOffersRepository = _DataRepositoryFactory.GetDataRepository<ITopOffersRepository>();
                    ITopOfferMappingRepository topOffersMappingRepository = _DataRepositoryFactory.GetDataRepository<ITopOfferMappingRepository>();
                    List<TopOfferMapping> updateOfferMapping = new List<TopOfferMapping>();
                    foreach (var item in activity.AllTopOffers)
                    {
                        if (topOffersRepository.Get(item.TopOffersKey) != null)
                        {
                            TopOffers offer = topOffersRepository.Get(item.TopOffersKey);
                            offer.ImageUrl = activity.ImageURL;
                            offer.OfferEndDate = item.OfferEndDate;
                            offer.OfferStartDate = item.OfferStartDate;
                            offer.LocationKey = locationKey;

                            IEnumerable<TopOfferMapping> offerMapping = topOffersMappingRepository.Get().Where(e => e.TopOfferKey == item.TopOffersKey && e.MappingKey == activity.ActivityKey);
                            foreach (var mapping in offerMapping)
                            {
                                topOffersMappingRepository.Remove(mapping);
                            }
                            TopOfferMapping mappingOffer = new TopOfferMapping
                            {
                                TopOfferKey = item.TopOffersKey,
                                MappingKey = activity.ActivityKey,
                                Discount = item.Discount,
                                IsDeleted = false,
                                MappingType = ACTIVITY,
                                TopOfferMappingKey = Guid.NewGuid().ToString()
                            };
                            updateOfferMapping.Add(mappingOffer);
                        }
                    }
                    //Adding all top offer mapping
                    topOffersMappingRepository.AddAll(updateOfferMapping);
                }
            });
        }