'''
Created on 15-Aug-2024

@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

# _aops_manager
# AopsManager
        
class AopsManager(BaseManager):
    '''
    Assorted Problems in advanced math
    Example:
        ke = AopsManager()
        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(AopsManager, cls).__new__(cls, *args, **kwargs)
        for base_class in AopsManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass

    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_combinatorics',
            '_problem_aops_08_q_01_05_level',
            '_problem_aops_08_q_06_10_level',
            '_problem_aops_08_q_11_15_level',
            '_problem_aops_08_test_papers',
            
         ])
        pass




    def _problem_combinatorics(self, **kwargs):
        
        comb_helper = self.base_helper.get_aops_helper().get_combinatorics_helper()
        result = comb_helper.get_problem_most_difficult(**kwargs)
        
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            prob_s = f"""
            Failed to get questions
            kwargs = {kwargs}
            """
            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 _problem_aops_08_q_01_05_level(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/math/aops"
        search_file_paths = [
                "aops_08_q_01_05_level.txt"
            ]
        
        
        file_paths = self.get_q_bank_factory().search_question_bank_files(folder_path, search_file_paths)
        return self.get_q_bank_factory().PROBLEM_TEMPLATE_Q_BANK_RANDOM_QUESTION(file_paths, *args, **kwargs)

        

    def _problem_aops_08_q_06_10_level(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/math/aops"
        search_file_paths = [
                "aops_08_q_06_10_level.txt"
            ]
        
        
        file_paths = self.get_q_bank_factory().search_question_bank_files(folder_path, search_file_paths)
        return self.get_q_bank_factory().PROBLEM_TEMPLATE_Q_BANK_RANDOM_QUESTION(file_paths, *args, **kwargs)

        

    def _problem_aops_08_q_11_15_level(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/math/aops"
        search_file_paths = [
                "aops_08_q_11_15_level.txt"
            ]
        
        
        file_paths = self.get_q_bank_factory().search_question_bank_files(folder_path, search_file_paths)
        return self.get_q_bank_factory().PROBLEM_TEMPLATE_Q_BANK_RANDOM_QUESTION(file_paths, *args, **kwargs)

        
    def _problem_aops_08_test_papers(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/math/aops"
        search_file_paths = [
                "aops_08_test_papers.txt"
            ]
        
        
        file_paths = self.get_q_bank_factory().search_question_bank_files(folder_path, search_file_paths)
        return self.get_q_bank_factory().PROBLEM_TEMPLATE_Q_BANK_RANDOM_QUESTION(file_paths, *args, **kwargs)

        
    pass
    



