public HealthPlanCallQueueCriteriaEditModel GetCriteriaEditModel(long criteriaId = 0)
        {
            bool isTeamAssignment = false;
            IEnumerable <HealthPlanCriteriaAssignment>     criteriaAssignments = null;
            IEnumerable <HealthPlanCriteriaTeamAssignment> teamAssignments     = null;

            if (criteriaId <= 0)
            {
                var defaultLanguageForConfirmation = _languageRepository.GetByName(DefaultLanguageForConfirmation);
                var emptyModel = new HealthPlanCallQueueCriteriaEditModel();
                if (defaultLanguageForConfirmation != null)
                {
                    emptyModel.LanguageId = defaultLanguageForConfirmation.Id;
                }
                return(emptyModel);
            }

            var criteria  = _healthPlanCallQueueCriteriaRepository.GetById(criteriaId);
            var callQueue = _callQueueRepository.GetById(criteria.CallQueueId);

            var model = _healthPlanCallQueueCriteriaEditModelFactory.DomainToModel(criteria);

            criteriaAssignments = _healthPlanCriteriaAssignmentRepository.GetByCriteriaId(model.Id);
            if (!criteriaAssignments.Any())
            {
                teamAssignments = _healthPlanCriteriaTeamAssignmentRepository.GetTeamAssignments(model.Id);

                isTeamAssignment = teamAssignments.Any();
            }

            if (!isTeamAssignment)
            {
                var callQueueAssignmentEditModel = new List <CallQueueAssignmentEditModel>();

                if (criteriaAssignments != null && criteriaAssignments.Any())
                {
                    var assignedToIds = criteriaAssignments.Select(x => x.AssignedToOrgRoleUserId).ToArray();
                    var nameIdPairs   = _organizationRoleUserRepository.GetNameIdPairofUsers(assignedToIds);
                    foreach (var assignedTo in assignedToIds)
                    {
                        var criteriaAssignment = criteriaAssignments.First(x => x.AssignedToOrgRoleUserId == assignedTo);
                        callQueueAssignmentEditModel.Add(new CallQueueAssignmentEditModel
                        {
                            Name = nameIdPairs.First(x => x.FirstValue == assignedTo).SecondValue,
                            AssignedOrgRoleUserId = assignedTo,
                            StartDate             = criteriaAssignment.StartDate,
                            EndDate = criteriaAssignment.EndDate
                        });
                    }
                }
                model.Assignments = callQueueAssignmentEditModel;
                model.CallCenterTeamAssignments = null;
                model.IsTeamAssignment          = false;
            }
            else
            {
                var teamAssignmentEditModel = new List <HealthPlanCriteriaTeamAssignmentEditModel>();
                var idnamePairOfTeams       = _callCenterTeamRepository.GetIdNamePairOfTeams(teamAssignments.Select(x => x.TeamId).ToArray());

                foreach (var healthPlanCriteriaTeamAssignment in teamAssignments)
                {
                    var idnamePairOfTeam = idnamePairOfTeams.FirstOrDefault(x => x.FirstValue == healthPlanCriteriaTeamAssignment.TeamId);
                    if (idnamePairOfTeam == null)
                    {
                        continue;
                    }
                    var assignmentEditModel = new HealthPlanCriteriaTeamAssignmentEditModel
                    {
                        TeamId    = healthPlanCriteriaTeamAssignment.TeamId,
                        Name      = idnamePairOfTeam.SecondValue,
                        StartDate = healthPlanCriteriaTeamAssignment.StartDate,
                        EndDate   = healthPlanCriteriaTeamAssignment.EndDate
                    };
                    teamAssignmentEditModel.Add(assignmentEditModel);
                }

                model.CallCenterTeamAssignments = teamAssignmentEditModel;
                model.Assignments      = null;
                model.IsTeamAssignment = true;
            }

            model.CallQueue   = callQueue.Category;
            model.CallQueueId = callQueue.Id;

            if (callQueue.Category == HealthPlanCallQueueCategory.MailRound)
            {
                //var healthPlanCallQueueCriterias = _healthPlanCallQueueCriteriaRepository.GetCriteriaByHealthPlanCallQueue(model.HealthPlanId, HealthPlanCallQueueCategory.MailRound);
                var healthPlanCallQueueCriteria = _healthPlanCallQueueCriteriaRepository.GetById(criteriaId);
                //var campaigns = _campaignRepository.GetByIds(healthPlanCallQueueCriterias.Where(x => x.CampaignId.HasValue).Select(x => x.CampaignId.Value));
                //var campaignIds = new List<long>();

                //campaignIds.Add(healthPlanCallQueueCriteria.CampaignId.Value);
                //foreach (var campaign in campaigns)
                //{
                //    campaignIds.Add(campaign.Id);
                //}
                //model.Campaigns = campaignIds;
                var campaignDirectMailDates = new List <CampaignDirectMailDatesEditModel>();

                var directMailDates = _healthPlanCriteriaDirectMailService.GetDirectMailDateEditModel(criteriaId);
                campaignDirectMailDates.Add(new CampaignDirectMailDatesEditModel
                {
                    CampaignId = healthPlanCallQueueCriteria.CampaignId.Value,
                    DirectMailDateActivities = directMailDates
                });
                model.CampaignDirectMailDates = campaignDirectMailDates;
            }

            return(model);
        }
        public ActionResult Edit(HealthPlanCallQueueCriteriaEditModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }
                else
                {
                    //if ((model.Assignments == null && model.CallCenterTeamAssignments == null)
                    //    ||
                    //    ((model.Assignments != null && !model.Assignments.Any()) && (model.CallCenterTeamAssignments != null && !model.CallCenterTeamAssignments.Any())))
                    //{
                    //    model.FeedbackMessage = FeedbackMessageModel.CreateWarningMessage("Assignment must exist");
                    //    return View(model);
                    //}

                    var healthPlanCriteria = _healthPlanCallQueueCriteriaRepository.GetCriteriaByHealthPlanCallQueue(model.HealthPlanId, model.CallQueue);

                    if (!string.IsNullOrWhiteSpace(model.CriteriaName))
                    {
                        model.CriteriaName = model.CriteriaName.Trim();
                        var currentCriteria = healthPlanCriteria.FirstOrDefault(x => x.Id == model.Id);

                        //if we are Modifying Criteria Name then check for Uniqueness
                        if (currentCriteria.CriteriaName.ToLower() != model.CriteriaName.ToLower())
                        {
                            var criteriaNameList = _healthPlanCallQueueCriteriaRepository.GetAllHealthPlanCallQueueCriteriaNames();
                            if (criteriaNameList.Any(x => x.ToLower() == model.CriteriaName.ToLower()))
                            {
                                model.FeedbackMessage = FeedbackMessageModel.CreateWarningMessage("Name already exists");
                                return(View(model));
                            }
                        }
                        if (model.CallQueue == HealthPlanCallQueueCategory.AppointmentConfirmation && currentCriteria.LanguageId != model.LanguageId)
                        {
                            var healthPlanCallQueueCriteria = _healthPlanCallQueueCriteriaRepository.GetQueueCriteriaForQueueByLanguage(model.CallQueue, model.HealthPlanId, model.LanguageId);
                            if (healthPlanCallQueueCriteria != null)
                            {
                                model.FeedbackMessage = FeedbackMessageModel.CreateWarningMessage("Criteria already exists for selected language.");
                                return(View(model));
                            }
                        }
                    }
                    bool isCriteriaExist = CheckHealthPlanCallQueueCriteriaAlreadyExists(model, healthPlanCriteria);

                    if (isCriteriaExist)
                    {
                        model.FeedbackMessage = FeedbackMessageModel.CreateWarningMessage("Criteria for same healthplan and call queue exist");
                        return(View(model));
                    }
                    else
                    {
                        var criteria = healthPlanCriteria.First(x => x.Id == model.Id);
                        var isCriteriaSameAsPervious = IsCriteriaIsSameAsPerious(model, criteria);

                        if (isCriteriaSameAsPervious)
                        {
                            isCriteriaSameAsPervious = CheckCustomerDataHasBeenUploaded(criteria);
                        }

                        //check if user has changed mode of assignment (agent assignment or team assignment)
                        var isTeamAssignmentOld  = _healthPlanCriteriaTeamAssignmentRepository.GetTeamAssignments(criteria.Id).Any();
                        var isAgentAssignmentOld = _healthPlanCriteriaAssignmentRepository.GetByCriteriaId(criteria.Id).Any();

                        if (model.IsTeamAssignment && isAgentAssignmentOld) //delete old agent assignments
                        {
                            _healthPlanCriteriaAssignmentRepository.DeleteByCriteriaId(criteria.Id);
                        }
                        else if (!model.IsTeamAssignment && isTeamAssignmentOld)     //delete old team assignments
                        {
                            _healthPlanCriteriaTeamAssignmentRepository.DeleteAssignmentsForCriteria(criteria.Id);
                        }
                        else if (model.IsTeamAssignment && isTeamAssignmentOld && model.CallCenterTeamAssignments.IsNullOrEmpty())
                        {
                            _healthPlanCriteriaTeamAssignmentRepository.DeleteAssignmentsForCriteria(criteria.Id);
                        }
                        else if (!model.IsTeamAssignment && isAgentAssignmentOld && model.Assignments.IsNullOrEmpty())
                        {
                            _healthPlanCriteriaAssignmentRepository.DeleteByCriteriaId(criteria.Id);
                        }

                        _healthPlanCallQueueCriteriaService.SaveHealthPlanCallQueueCriteria(model, _sessionContext.UserSession.CurrentOrganizationRole.OrganizationRoleUserId, isCriteriaSameAsPervious);
                        if (model.IsCriteriaSameAsPervious)
                        {
                            model.FeedbackMessage = FeedbackMessageModel.CreateSuccessMessage("Criteria Updated");
                            return(View(model));
                        }
                        else
                        {
                            return(RedirectToAction("Edit", new { criteriaId = model.Id, isSuccessfull = true }));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                model.FeedbackMessage = FeedbackMessageModel.CreateFailureMessage("System Error:" + ex.Message);
                return(View(model));
            }
        }