'''
Created on 02-Mar-2021

@author: bthakur
'''
'''
Created on 07-Feb-2021

@author: bthakur
'''
import numpy as np
import random
import inspect
import traceback

from sympy import (simplify, sympify, log, latex, Symbol, symbols, 
                   UnevaluatedExpr as ueval)

        
from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate

# _assorted_problems_manager
# AssortedProblemsManager
        
class AssortedProblemsManager(BaseManager):
    '''
    Assorted Problems in advanced math
    Example:
        ke = AssortedProblemsManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ke.getRandomProblem(problem_type = 1)
        ...
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()

        ke.printProblemTypes()
    '''    
    def __new__(cls, *args, **kwargs):
        instance = super(AssortedProblemsManager, cls).__new__(cls, *args, **kwargs)
        for base_class in AssortedProblemsManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass

    def _TEMPLATE_CUSTOM_QUESTIONS(self, stored_questions_cls, keywords, **kwargs):
        
        lsqs = stored_questions_cls(verbose = self.verbose)
        result = lsqs.get_random_question(keywords)

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            prob_s = f"""
            Failed to get questions
            keywords = {keywords}
            """
            problemTemplate.add_problem(prob_s.strip(), 'text/html')
            return problemTemplate
            
        
        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')

        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')

        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
        pass


    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_matrix_subsets',
            '_problem_solve_for_x_type_1',
            '_problem_maximize_pos_nums',
         ])
        pass

    def _problem_matrix_subsets(self, *args, **kwargs):
        from ..storeddata import AssortedStoredQuestions
        keywords = kwargs.get('keywords', 'subsets')
        return self._TEMPLATE_CUSTOM_QUESTIONS(AssortedStoredQuestions, keywords, **kwargs)
        pass


    def _problem_solve_for_x_type_1(self, *args, **kwargs):
        from ..storeddata import AssortedStoredQuestions
        keywords = kwargs.get('keywords', 'solve for x')
        return self._TEMPLATE_CUSTOM_QUESTIONS(AssortedStoredQuestions, keywords, **kwargs)
        pass


    def _problem_maximize_pos_nums(self, *args, **kwargs):
        from ..storeddata import AssortedStoredQuestions
        keywords = kwargs.get('keywords', 'positive numbers')
        return self._TEMPLATE_CUSTOM_QUESTIONS(AssortedStoredQuestions, keywords, **kwargs)
        pass


