'''
Created on 02-Mar-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

# _grade2_manager
# Grade2Manager
        
class Grade2Manager(BaseManager):
    '''
    Grade 2 (class 2) math
    Example:
        ke = Grade2Manager()
        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(Grade2Manager, cls).__new__(cls, *args, **kwargs)
        for base_class in Grade2Manager.__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 Grade2StoredQuestions
        
        stored_questions = Grade2StoredQuestions(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_tens_to_ones',
            '_problem_custom_questions_addition_word_problem', 
            '_problem_custom_questions_height_block_tower', 
            '_problem_custom_questions_complete_the_equations', 
            '_problem_custom_questions_complete_the_equations_1', 
            '_problem_custom_questions_quadrilateral',
            '_problem_custom_questions_word_to_number',
            '_problem_custom_questions_shaded_area',
            '_problem_custom_questions_addition_word_problem_1', 
            '_problem_custom_questions_addition', 
            '_problem_custom_questions_addition_word_problem_2',
            '_problem_custom_questions_coin_value',
            '_problem_custom_questions_even_number',
            '_problem_custom_questions_inequality',
            '_problem_custom_questions_length_rectangle',
            '_problem_custom_questions_tall_umbrella',
            '_problem_custom_questions_identify_shape',
            '_problem_custom_questions_subtraction',
            '_problem_custom_questions_word_to_expanded_form',
            '_problem_custom_questions_area',
            '_problem_custom_questions_expanded_form',
            '_problem_custom_questions_addition_word_problem_3',
            '_problem_custom_questions_subtraction_word_problem',
         ])
        pass

    def _problem_custom_questions_tens_to_ones(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'tens_to_ones')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
    
    def _problem_custom_questions_addition_word_problem(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'addition_word_problem')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
    
    def _problem_custom_questions_height_block_tower(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'height_block_tower')
        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_complete_the_equations_1(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'complete_the_equations_1')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

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

    def _problem_custom_questions_word_to_number(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'word_to_number')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
 
    def _problem_custom_questions_shaded_area(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'shaded_area')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

    def _problem_custom_questions_addition_word_problem_1(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'addition_word_problem_1')
        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_addition_word_problem_2(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'addition_word_problem_2')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)
    
    def _problem_custom_questions_coin_value(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'coin_value')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)

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

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

    def _problem_custom_questions_length_rectangle(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'length_rectangle')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     
    
    def _problem_custom_questions_tall_umbrella(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'tall_umbrella')
        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_subtraction(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'subtraction')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

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

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

    def _problem_custom_questions_expanded_form(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'expanded_form')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)  
       
    def _problem_custom_questions_addition_word_problem_3(self, *args, **kwargs):
        keywords = kwargs.get('keywords', 'addition_word_problem_3')
        return self._TEMPLATE_CUSTOM_QUESTIONS(keywords, **kwargs)     

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

