'''
Created on 02-Mar-2021

@author: bthakur
'''
'''
Created on 07-Feb-2021
https://web.ma.utexas.edu/users/olenab/s12-PutnamInequalitiesSols(3rd).pdf

@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 ..storeddata import InequalityStoredQuestions
        
from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate

# _inequality_problems_manager
# InequalityProblemsManager
        
class InequalityProblemsManager(BaseManager):
    '''
    Inequality Problems in math
    Example:
        ke = InequalityProblemsManager()
        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(InequalityProblemsManager, cls).__new__(cls, *args, **kwargs)
        for base_class in InequalityProblemsManager.__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_inequality_questions_index',
            '_problem_am_gm_inequality_type_1',
            '_problem_am_gm_inequality_type_2',
            '_problem_maximize_pos_nums',
            '_problem_numbers_with_roots',
         ])
        pass
    

    def _problem_inequality_questions_index(self, *args, **kwargs):
        lsqs = InequalityStoredQuestions(verbose = self.verbose)
        q_indexes_str = lsqs.q_indexes_to_html()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        List of problems related to inequality.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        List of problems related to inequality.

        {q_indexes_str}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = soln_s
        
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

            

    def _problem_am_gm_inequality_type_1(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'am_gm_inequality_type_1')
        return self._TEMPLATE_CUSTOM_QUESTIONS(InequalityStoredQuestions, keywords, **kwargs)
        pass


    def _problem_am_gm_inequality_type_2(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'am_gm_inequality_type_2')
        return self._TEMPLATE_CUSTOM_QUESTIONS(InequalityStoredQuestions, keywords, **kwargs)
        pass


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


    def _problem_numbers_with_roots(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'numbers_with_roots')
        return self._TEMPLATE_CUSTOM_QUESTIONS(InequalityStoredQuestions, keywords, **kwargs)
        pass


