'''
Created on 01-Apr-2021
@author: bthakur
'''
import random
from sympy import Symbol, symbols, latex

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

# _statement_to_expr_manager
# StatementToExprManager
        
class StatementToExprManager(BaseManager):
    '''
    Conversion of statements to expression


    Minimum Grade: 3
    Maximum Grade: 10


    Examples
    --------
    ke = StatementToExprManager()
    ke 
    
    ke.printProblemTypes()
    ke.helpProblemType(0)
    
    ke.getRandomProblem()
    ke.getRandomProblem(problem_type = 0)


    ke.printProblem()
    ke.printAnswer()
    ke.printSolution()
    ke.printProblemHelp()


    doc_style: xv_doc
    ''' 

    _OPR_TYPE_COUNT = 10
    
    def __new__(cls, *args, **kwargs):
        instance = super(StatementToExprManager, cls).__new__(cls, *args, **kwargs)
        for base_class in StatementToExprManager.__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_arithmetic_opreration',
            '_problem_assorted',
            '_problem_single_variable',
            '_problem_modelling_two_variable',
            '_problem_modelling_two_digits',
            '_problem_modelling_simple_percentage',
            '_problem_modelling_percentage',
            '_problem_modelling_multi_variable',
            '_problem_modelling_juice_business',
            '_problem_modelling_layered_business',
            '_problem_discounts_taxes',
            '_problem_multi_digit_numbers',
            
            '_problem_statement_to_expr_overview',
            
            ])
        pass



    def _problem_arithmetic_opreration(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_arithmetic_opreration()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_assorted(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_assorted()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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


    def _problem_single_variable(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_single_variable()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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


    def _problem_modelling_two_variable(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_two_variable()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_modelling_two_digits(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_two_digits()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_modelling_simple_percentage(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_simple_percentage()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_modelling_percentage(self, *args, **kwargs):
        """


        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_percentage()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_modelling_multi_variable(self, *args, **kwargs):
        """
        

        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_multi_variable()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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



    def _problem_modelling_juice_business(self, *args, **kwargs):
        """
        

        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_juice_business()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_modelling_layered_business(self, *args, **kwargs):
        """
        

        doc_style: xv_doc
        """
        soe_helper = self.base_helper.get_statement_to_expr_helper()
        result = soe_helper.get_problem_modelling_layered_business()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        
        if result is None:
            problem_s = f"""
            Failed to get a problem.
            """
            problemTemplate.add_problem(problem_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

    def _problem_discounts_taxes(self, *args, **kwargs):
        """
        discounts and taxes
        
        doc_style: xv_doc
        """
        ste_helper = self.base_helper.get_statement_to_expr_helper()


        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        random_model  = ste_helper.get_discounts_taxes_prob()
        
        if random_model is None:
            problem_s = f"""
            Failed to get statement with units
            """
            problemTemplate.add_problem(problem_s.strip(), 'text/html')
            return problemTemplate

        problem_s = f"""
        {random_model['problem_s']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

                
        soln_s = f"""
        {random_model['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {random_model['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate


    def _problem_multi_digit_numbers(self, *args, **kwargs):
        """
        Multi digit numbers
        
        doc_style: xv_doc
        """
        ste_helper = self.base_helper.get_statement_to_expr_helper()


        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        random_model  = ste_helper.get_mul_digit_num_prob()
        
        if random_model is None:
            problem_s = f"""
            Failed to get mul_digit_num_prob
            """
            problemTemplate.add_problem(problem_s.strip(), 'text/html')
            return problemTemplate

        problem_s = f"""
        {random_model['problem_s']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')

                
        soln_s = f"""
        {random_model['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {random_model['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate


    
    def _problem_statement_to_expr_overview(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/math/common"
        search_file_paths = [
                "statement_to_expr_overview.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)




    