'''
Created on 26-Jan-2022
@author: bthakur
'''
import random
import inspect
import traceback
from sympy import latex

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


class MidLevelMathManager(BaseManager):
    '''
    Example:
        ke = MidLevelMathManager()
        ke
        
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ke.getRandomProblem(problem_type = 1)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
    '''    
    def __new__(cls, *args, **kwargs):
        instance = super(MidLevelMathManager, cls).__new__(cls, *args, **kwargs)
        for base_class in MidLevelMathManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass


    def _TEMPLATE_SINGLE_PROBLEM(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        helper = self.base_helper.get_mid_level_math_helper()
        
        
        result = helper.get_random_problem(**kwargs)
        
        statement = f"""
        {result['statement']}
        """
        problemTemplate.add_problem(statement.strip(), "text/html")

        soln_s = result['soln_statement']
        problemTemplate.add_solution(soln_s.strip(), 'text/html')

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

    def _TEMPLATE_MULTIPLE_PROBLEM(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        helper = self.base_helper.get_mid_level_math_helper()
        
        result = helper.get_random_problems(**kwargs)
        
        statement = f"""
        {result['statements']}
        """
        problemTemplate.add_problem(statement.strip(), "text/html")

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

        answer_s = f"""
        {result['answers']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
        pass
        
    
    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
                '_problem_single_arithmetic',
                '_problem_multiple_arithmetic',
                '_problem_single_combination',
                '_problem_multiple_combination',
                
             ])
        pass
    
    def _problem_single_arithmetic(self, *args, **kwargs):
        kwargs['op'] = 'arithmetic'
        return self._TEMPLATE_SINGLE_PROBLEM(*args, **kwargs)
        pass

    def _problem_multiple_arithmetic(self, *args, **kwargs):
        kwargs['op'] = 'arithmetic'
        return self._TEMPLATE_MULTIPLE_PROBLEM(*args, **kwargs)
        pass
        
    def _problem_single_combination(self, *args, **kwargs):
        kwargs['op'] = 'combination'
        return self._TEMPLATE_SINGLE_PROBLEM(*args, **kwargs)
        pass

    def _problem_multiple_combination(self, *args, **kwargs):
        kwargs['op'] = 'combination'
        return self._TEMPLATE_MULTIPLE_PROBLEM(*args, **kwargs)
        pass
        
    