public async Task <QuestionGeneration> Do_Regenerate_Template(string text)
        {
            //var templateCursor = context.QuestionGenerationCollection.Find(_ => true).Project(u => u.Text);
            List <QuestionGeneration> Selected_object = new List <QuestionGeneration>();
            QuestionGeneration        Dublicate_copy  = new QuestionGeneration();

            FilterDefinition <QuestionGeneration> filter = Builders <QuestionGeneration>
                                                           .Filter.Eq(m => m.Text, text);

            var questionCursor = await context.QuestionGenerationCollection.FindAsync(filter);

            Selected_object = await questionCursor.ToListAsync();

            Dublicate_copy.CategoryName = Selected_object[0].CategoryName;
            Dublicate_copy.Text         = Selected_object[0].Text;
            Dublicate_copy.TopicName    = Selected_object[0].TopicName;
            Dublicate_copy.CategoryId   = Selected_object[0].CategoryId;
            Dublicate_copy.TopicId      = Selected_object[0].TopicId;
            Console.WriteLine("assigned to another object-----------");
            DeleteResult deleteResult = await context.QuestionGenerationCollection.DeleteOneAsync(filter);

            if (deleteResult.IsAcknowledged && deleteResult.DeletedCount > 0)
            {
                //Console.WriteLine("deleted the template from the database======");
                Console.WriteLine(deleteResult.DeletedCount + " Items Deleted.");
                return(Dublicate_copy);
            }
            return(null);
        }
        public async Task <bool> GenerateQuestionsAndOptions(QuestionGeneration qT)
        {
            if (qT.CategoryName == "" && qT.TopicName == "")
            {
                Console.WriteLine("Entered for the renewal---------");
                QuestionGeneration delete_result = await Do_Regenerate_Template(qT.Text);

                if (delete_result != null)
                {
                    qT = delete_result;
                }
                else
                {
                    return(false);
                }
            }

            Console.WriteLine("Finished  Renewal  Check");
            FilterDefinition <QuestionGeneration> filter = Builders <QuestionGeneration>
                                                           .Filter.Eq(m => m.Text, qT.Text);

            var checkForTemplate = await context.QuestionGenerationCollection.Find(filter).FirstOrDefaultAsync();

            if (checkForTemplate == null)
            {
                List <string> getPresentTopics = await GetAllTopics();

                await context.QuestionGenerationCollection.InsertOneAsync(qT);

                var insertedTemplate = await context.QuestionGenerationCollection.Find(filter).FirstOrDefaultAsync();

                ObjectId currentTemplateId = insertedTemplate.Id;
                bool     check             = await InsertQuestionsAndOptions(qT, currentTemplateId);

                if (check)
                {
                    // getPresentTopics.ForEach(i => Console.Write("-- {0}\t", i));
                    if (!getPresentTopics.Contains(qT.TopicName, StringComparer.OrdinalIgnoreCase))
                    {
                        PublishTopic(qT.TopicName);
                    }
                    return(true);
                }
            }
            return(false);
        }
        public async Task <bool> InsertQuestionsAndOptions(QuestionGeneration q, ObjectId currentTemplateId)
        {
            var otherOptionsList = GenerateOtherOptions(q.CategoryName);

            string sparQL = "SELECT ?cidLabel ?authortitleLabel WHERE {?cid wdt:P31 wd:" + q.TopicId + ".?cid wdt:" + q.CategoryId + " ?authortitle .SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' . }}LIMIT " + NumberOfQuestions + "";
            // string sparQL2 = $@"SELECT ?personLabel WHERE {{ ?person wdt:{q.Topic} wd:{q.Categ} . SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' . } }LIMIT "+NumberOfQuestions+""; // Nishant
            Task <List <universal_object> > subjectForQuestion = System.Threading.Tasks.Task <string> .Run(() => GetSubjectsForQuestion(sparQL).Result);

            List <universal_object> listOfSubjectForQuestion = subjectForQuestion.Result;

            List <Questions> questionsList         = new List <Questions>();
            string           replacementStrSubject = '[' + GetBetween(q.Text, "[", "]") + ']';
            string           replacementStrObject  = '(' + GetBetween(q.Text, "(", ")") + ')';

            for (int i = 0; i < listOfSubjectForQuestion.Count; i++)
            {
                if (!(listOfSubjectForQuestion[i].mainobject[0] == 'Q' && IsDigitsOnly(listOfSubjectForQuestion[i].mainobject.Substring(1))))
                {
                    string questionText = q.Text;
                    questionText = questionText.Replace(replacementStrObject, q.CategoryName);
                    questionText = questionText.Replace(replacementStrSubject, listOfSubjectForQuestion[i].mainobject);

                    List <OtherOptions> listOfOtherOptions = new List <OtherOptions>();
                    // int iteratorForListOfOptions = 0;
                    int increaser = 0;
                    // otherOptionsList = otherOptionsList.GetRange(1, otherOptionsList.Count).Append(otherOptionsList.First()).ToList();

                    for (int j = 0; j < 3 + increaser; j++)
                    {
                        if (otherOptionsList[j] != "" && !(otherOptionsList[j][0] == 'Q' && IsDigitsOnly(otherOptionsList[j].Substring(1))))
                        {
                            OtherOptions otherOptionObject = new OtherOptions();
                            otherOptionObject.Option = otherOptionsList[j];
                            listOfOtherOptions.Add(otherOptionObject);
                            // if(iteratorForListOfOptions+3 < otherOptionsList.Count)
                            //     iteratorForListOfOptions++;
                            // else
                            //     iteratorForListOfOptions = 0;
                        }
                        else
                        {
                            increaser++;
                        }
                    }
                    otherOptionsList = Shift(otherOptionsList);

                    Questions questionObject = new Questions();
                    questionObject.Question         = questionText;
                    questionObject.CorrectOption    = listOfSubjectForQuestion[i].predicate;
                    questionObject.OtherOptionsList = listOfOtherOptions;

                    questionsList.Add(questionObject);
                }
            }
            QuestionGeneration questionGeneratedObject = new QuestionGeneration();

            questionGeneratedObject.Id            = currentTemplateId;
            questionGeneratedObject.Text          = q.Text;
            questionGeneratedObject.CategoryId    = q.CategoryId;
            questionGeneratedObject.CategoryName  = q.CategoryName;
            questionGeneratedObject.TopicId       = q.TopicId;
            questionGeneratedObject.TopicName     = q.TopicName;
            questionGeneratedObject.QuestionsList = questionsList;
            ReplaceOneResult updateResult = await context.QuestionGenerationCollection.
                                            ReplaceOneAsync(filter : g => g.Id == questionGeneratedObject.Id, replacement : questionGeneratedObject);

            return(updateResult.IsAcknowledged && updateResult.ModifiedCount > 0);
        }