public override UserMedal CheckIfFullfiledRequirement(SpontaneousUserModel statistic, UserActivity activity)
        {
            UserMedal currentMedal = new FlowCompleteMedal();
            currentMedal.CreatedAt = new DateTime(0);
            if (statistic.UserMedalsList != null && statistic.UserMedalsList.Count > 0)
            {
                //if (IfAlreadyHave(statistic)) return null;
                currentMedal = getLastMedal(statistic);
            }

            //Check if current activity is meets requirement
            string activityName = activity.GetType().Name;
            int activityCount = 0;
            if (activity is MedalViewActivity)
            {
                activityCount = statistic.GetActivityCount(activityName, ((MedalViewActivity)activity).MedalType, currentMedal.CreatedAt);
            }
            else
            {
                activityCount = statistic.GetActivityCount(activityName, currentMedal.CreatedAt);
            }

            //Check if current activity is in requirement list
            if (RequirementsList.FirstOrDefault(r => r.Activity.GetType().Name == activityName) != null)
            {
                if (activityCount >= RequirementsList.FirstOrDefault(r => r.Activity.GetType().Name == activityName).Count)
                {
                    //Current activity is meets requirement
                    //Check if meets other requirements necessary for the medal
                    foreach (var requirement in RequirementsList)
                    {
                        activityName = requirement.Activity.GetType().Name;
                        //avtivityCount = statistic.GetActivityCount(activityName);
                        if (requirement.Activity is MedalViewActivity)
                        {
                            activityCount = statistic.GetActivityCount(activityName, "FlowCompleteMedal", currentMedal.CreatedAt);
                        }
                        else
                        {
                            activityCount = statistic.GetActivityCount(activityName, currentMedal.CreatedAt);
                        }
                        if (!(activityCount >= RequirementsList.FirstOrDefault(r => r.Activity.GetType().Name == activityName).Count))
                        {
                            log.DebugFormat("[FlowCompleteMedal: CheckIfFullfiledRequirement] Still not win FlowComplete Medal. avtivityCount={0}", activityCount);
                            return null;
                        }
                    }
                    UserMedal medal = new FlowCompleteMedal();
                    log.InfoFormat("[FlowCompleteMedal: CheckIfFullfiledRequirement] Win FlowComplete Medal. avtivityCount={0}", activityCount);
                    return medal;
                }
            }
            else
            {
                log.InfoFormat("[FlowCompleteMedal: CheckIfFullfiledRequirement] Still not win FlowComplete Medal. avtivityCount={0}", activityCount);
            }
            return null;
        }
 public UserMedal getLastMedal(SpontaneousUserModel statistic)
 {
     UserMedal returnValue = new FlowCompleteMedal();
     returnValue.CreatedAt = new DateTime(0);
     if (statistic != null && statistic.UserMedalsList != null && IfAlreadyHave(statistic))
     {
         var flowMedalList = statistic.UserMedalsList.Where(c => c is FlowCompleteMedal);
         if (flowMedalList != null) {
             returnValue = flowMedalList.OrderBy(o => o.CreatedAt).ToList().LastOrDefault();
         }
     }
     log.DebugFormat("[FlowCompleteChecker: getLastMedal, CreatedAt={0}", returnValue.CreatedAt);
     return returnValue;
 }
        public override MedalProgress GetProgress(SpontaneousUserModel statistic)
        {
            //if (!IfAlreadyHave(statistic))
            //{
                UserMedal currentMedal = new FlowCompleteMedal();
                currentMedal.CreatedAt = new DateTime(0);
                if (statistic.UserMedalsList != null && statistic.UserMedalsList.Count > 0)
                {
                    currentMedal = getLastMedal(statistic);
                }

                MedalProgress returnValue = new MedalProgress()
                {
                    MedalType = MedalType,
                };
                //Calculate total points required for the medal
                foreach (var requirement in RequirementsList)
                {
                    returnValue.totalPointsRequired += requirement.Activity.Points * requirement.Count;
                }

                returnValue.pointsRequired = returnValue.totalPointsRequired;

                if (statistic != null && statistic.UserActivityList != null)
                {
                    //Calculate points still required for the medal
                    bool RestaurantMenuEnterActivityFlag = false;
                    bool MenuLinkPushActivityFlag = false;
                    bool ApproveMealPushActivityFlag = false;
                    bool SendMealSummaryActivityFlag = false;
                    bool PreMealSugarApproveActivityFlag = false;
                    bool PostMealSugarApproveActivityFlag = false;
                    bool PostMealCarbBlameActivityFlag = false;
                    foreach (UserActivity userActivity in statistic.UserActivityList)
                    {
                        if (userActivity.Date > currentMedal.CreatedAt)
                        {
                            SpontaneousMedalRequirementChecker.ActivityType type =
                                (SpontaneousMedalRequirementChecker.ActivityType)System.Enum.Parse(typeof(SpontaneousMedalRequirementChecker.ActivityType), userActivity.GetType().Name);
                            //int activityType = (int)type;
                            var dateDiff = (DateTime.UtcNow - userActivity.Date).TotalDays;

                            switch (type)
                            {
                                case SpontaneousMedalRequirementChecker.ActivityType.PageViewActivity:
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.RestaurantMenuEnterActivity:
                                    if (!RestaurantMenuEnterActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired =
                                            returnValue.pointsRequired -= userActivity.Points;
                                        RestaurantMenuEnterActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.MenuLinkPushActivity:
                                    if (!MenuLinkPushActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired -= userActivity.Points;
                                        MenuLinkPushActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.ApproveMealPushActivity:
                                    if (!ApproveMealPushActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired -= userActivity.Points;
                                        ApproveMealPushActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.SendMealSummaryActivity:
                                    if (!SendMealSummaryActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired -= userActivity.Points;
                                        SendMealSummaryActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.PreMealSugarApproveActivity:
                                    if (!PreMealSugarApproveActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired -= userActivity.Points;
                                        PreMealSugarApproveActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.PostMealSugarApproveActivity:
                                    if (!PostMealSugarApproveActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired -= userActivity.Points;
                                        PostMealSugarApproveActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.PostMealCarbBlameActivity:
                                    if (!PostMealCarbBlameActivityFlag && dateDiff < 1)
                                    {
                                        returnValue.pointsRequired -= userActivity.Points;
                                        PostMealCarbBlameActivityFlag = true;
                                    }
                                    break;
                                case SpontaneousMedalRequirementChecker.ActivityType.MedalViewActivity:
                                    break;
                                default:
                                    break;
                            }

                        }//if (userActivity.Date > currentMedal.CreatedAt)
                    }
                    if (returnValue.pointsRequired < 0) returnValue.pointsRequired = 0;
                }
                return returnValue;
            //}
            //return null;
        }
        public void GetMedalsTest_Should_ReturnListOfUserMedal_ifMedalWithNullCouponMustCreatNewCoupon()
        {
            //Arrange
            SpontaneousUserModel userModel = new SpontaneousUserModel()
            {
                UserActivityList = new List<UserActivity>(),
                UserMedalsList = new List<UserMedal>()
            };

            DateTime time = DateTime.UtcNow;
            CouponType coupon = new CouponType()
            {
                Id = new ObjectId("51aae3bf3720e811c09fb107"),
                Description = "test coupon nomber 1",
                RestaurantsIDs = new List<string>() { "51b02f863720e81d6cf62a3b", "51cbee273720e80ea00922ae" },
                ValidFrom = new DateTime(0),
                ValidUntil = time
            };
            UserMedal medal1 = new WelcomeUserMedal();
            UserMedal medal2 = new RestaurantAdviserMedal();
            UserMedal medal3 = new RestaurantAdviserMedal();
            medal3.Coupon = coupon;
            UserMedal medal4 = new FlowCompleteMedal();
            UserMedal medal5 = new AddRestaurantMedal();

            userModel.UserMedalsList.Add(medal1);
            userModel.UserMedalsList.Add(medal2);
            userModel.UserMedalsList.Add(medal3);
            userModel.UserMedalsList.Add(medal4);
            userModel.UserMedalsList.Add(medal5);

            //Act
            List<UserMedal> list = serviceLayer.GetMedals();

            //Assert
            Assert.IsFalse(list.Exists(m => m.Coupon == null));
        }