private HafModel GetHafModel(IEnumerable <KeyValuePair <long, List <long> > > keyValuePairs, IEnumerable <EventTest> eventTests, long eventId, long customerId, bool setChildQuestion, int versionNumber)
        {
            var questionsGroupByGroupName = _healthAssessmentRepository.GetAllQuestionGroupWithQuestion();
            var dependencyRules           = _haqDependencyRuleRepository.Get();
            var genericQuestionIds        = keyValuePairs.First(x => x.Key == 0).Value;
            IEnumerable <HealthAssessmentAnswer> answers = null;

            if (versionNumber > 0)
            {
                var archiveanswers = _healthAssessmentRepository.GetArchive(customerId, eventId, versionNumber);
                answers = archiveanswers.Select(aa => aa.HealthAssessmentAnswer).ToArray();
            }
            else
            {
                answers = _healthAssessmentRepository.Get(customerId, eventId);
            }


            keyValuePairs = keyValuePairs.Where(x => x.Key > 0);

            var questions = questionsGroupByGroupName.Where(x => x.Questions != null).SelectMany(x => x.Questions);

            var questionHafModel = questions.Where(
                x =>
                genericQuestionIds.Contains(x.Id) &&
                (x.IsForFemale == null || x.IsForFemale == IsFemale))
                                   .OrderBy(x => x.DisplaySequence)
                                   .ThenBy(x => x.Id)
                                   .Select(
                x => GetQuestion(x, dependencyRules.FirstOrDefault(d => d.QuestionId == x.Id), answers))
                                   .ToList();

            var genericModel = questionHafModel.Where(x => x.ParentQuestionId <= 0).ToArray();

            var model = new HafModel
            {
                Name        = string.Empty,
                Description = string.Empty,
                HafGroup    = new HafQuestionGroup
                {
                    Questions = setChildQuestion ? genericModel.Select(x => SetChildQuestions(x, questionHafModel.Where(c => c.ParentQuestionId == x.QuestoinId).ToArray(), questionHafModel.ToArray())) : questionHafModel
                },
                HafTests = CreateTestLevelHafQuestion(keyValuePairs, eventTests, questionsGroupByGroupName.Where(x => x.Questions != null).ToArray(), dependencyRules, answers, setChildQuestion)
            };

            long parentQuestionId = 0;
            var  index            = 0;

            foreach (var item in model.HafGroup.Questions.Where(x => x.ParentQuestionId > 0))
            {
                if (parentQuestionId != item.ParentQuestionId)
                {
                    index            = 1;
                    parentQuestionId = item.ParentQuestionId;
                }
                item.RelativeOrder = index++;
            }

            return(model);
        }
        public bool?IsKynHafPrefilled(long eventId, long customerId, DateTime appointmentTime, bool checkKynOnly = true)
        {
            var model = _healthAssessmentService.GetHealthAssessmentEditModel(customerId, eventId);

            if (model == null || !model.QuestionEditModels.Any())
            {
                return(false);
            }

            if (!model.IsKynPurchased && checkKynOnly)
            {
                return(null);
            }

            var customer = _customerRepository.GetCustomer(customerId);

            var isFilled = checkKynOnly ? CheckKynHaf(customer, model) : CheckHafPrefilled(customer, eventId);

            if (isFilled)
            {
                var answers = _healthAssessmentRepository.Get(customerId, eventId);

                if (answers.Any())
                {
                    var answer = answers.First();
                    if (answer.DataRecorderMetaData == null || answer.DataRecorderMetaData.DataRecorderCreator == null)
                    {
                        return(false);
                    }

                    var technicianIds = _organizationRoleUserRepository.GetOrganizationRoleUserIdsForRole((long)Roles.Technician);

                    technicianIds.AddRange(_organizationRoleUserRepository.GetOrganizationRoleUserIdsByParentRole((long)Roles.Technician));

                    var dataRecorderMetaData = answer.DataRecorderMetaData;
                    var updatedByCustomerOrOtherThanTechnician = dataRecorderMetaData.DataRecorderCreator.Id == customerId || !technicianIds.Contains(dataRecorderMetaData.DataRecorderCreator.Id);

                    if (updatedByCustomerOrOtherThanTechnician)
                    {
                        return(dataRecorderMetaData.DateCreated.AddHours(2) <= appointmentTime);
                    }

                    var latestVersion = _healthAssessmentRepository.GetLastVersionNumberUpdatedByCustomerOrOtherThanTechnician(answer.EventCustomerId, customerId);

                    if (latestVersion > 0)
                    {
                        var archiveAnswers = _healthAssessmentRepository.GetArchive(customerId, eventId, latestVersion);

                        model = _healthAssessmentService.GetHealthAssessmentEditModel(customerId, eventId, latestVersion);

                        isFilled = checkKynOnly ? CheckKynHaf(customer, model) : CheckHafPrefilled(customer, eventId, latestVersion);

                        if (isFilled)
                        {
                            if (archiveAnswers != null && archiveAnswers.Any())
                            {
                                answer = archiveAnswers.First().HealthAssessmentAnswer;
                                dataRecorderMetaData = answer.DataRecorderMetaData;
                                return(dataRecorderMetaData.DateCreated.AddHours(2) <= appointmentTime);
                            }
                        }
                    }
                }
            }

            return(false);
        }