'''
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 xv.km.util import latex_fraction
from xv.km.kelements import KPrintElement
        
from ..helper import BaseHelper
from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate

# _grade4_manager
# Grade4Manager
        
class Grade4Manager(BaseManager):
    '''
     Grade 4 (class 4) math
    Example:
        ke = Grade4Manager()
        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(Grade4Manager, cls).__new__(cls, *args, **kwargs)
        for base_class in Grade4Manager.__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, keywords, **kwargs):
        
        from ..storeddata import Grade4StoredQuestions
        
        stored_questions = Grade4StoredQuestions(verbose = self.verbose)
        result = stored_questions.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_custom_questions_inequality',
            '_problem_custom_questions_measurement_unit_litre', 
            '_problem_custom_questions_division', 
            '_problem_custom_questions_angle_protractor', 
            '_problem_custom_questions_decimal_to_fraction',
            '_problem_custom_questions_height_giraffe',
            '_problem_custom_questions_time_addition',
            '_problem_custom_questions_yard_to_feet',
            '_problem_custom_questions_expansion_of_number',
            '_problem_custom_questions_angle',
            '_problem_custom_questions_addition',
            '_problem_custom_questions_complete_the_equations',
            '_problem_custom_questions_factor_multiple',
            '_problem_custom_questions_division_1',
            '_problem_custom_questions_fraction_word_problem',
            '_problem_custom_questions_identify_shape',
            '_problem_custom_questions_fraction_word_problem_1',
            '_problem_custom_questions_number_line',
            '_problem_custom_questions_add_fraction',
            '_problem_custom_questions_similar_fraction',
            '_problem_custom_questions_fraction_to_decimal',
            '_problem_custom_questions_fraction_word_problem_2',
            '_problem_custom_questions_fraction_area',
            '_problem_custom_questions_angle_1',
            
           
         ])
        pass

    def _problem_custom_questions_inequality(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'inequality')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
    
    def _problem_custom_questions_measurement_unit_litre(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'measurement_unit_litre')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
    
    def _problem_custom_questions_division(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'division')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_angle_protractor(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'angle_protractor')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_decimal_to_fraction(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'decimal_to_fraction')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_height_giraffe(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'height_giraffe')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_time_addition(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'time_addition')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_yard_to_feet(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'yard_to_feet')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
    
    def _problem_custom_questions_expansion_of_number(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'expansion_of_number')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_angle(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'angle')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_addition(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'addition')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_complete_the_equations(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'complete_the_equations')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     
    
    def _problem_custom_questions_factor_multiple(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'factor_multiple')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     
    
    def _problem_custom_questions_division_1(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'division_1')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_fraction_word_problem(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'fraction_word_problem')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_identify_shape(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'identify_shape')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_fraction_word_problem_1(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'fraction_word_problem_1')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)  
       
    def _problem_custom_questions_number_line(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'number_line')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_add_fraction(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'add_fraction')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_similar_fraction(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'similar_fraction')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_fraction_to_decimal(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'fraction_to_decimal')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_fraction_word_problem_2(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'fraction_word_problem_2')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_fraction_area(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'fraction_area')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

    def _problem_custom_questions_angle_1(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'angle_1')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     
