'''
Created on 16-Apr-2021

@author: bthakur
'''
import os 
import numpy as np
import random
import inspect
import traceback

import sympy

from sympy import ( latex,symbols, diff, Symbol, Mul, Matrix, Function,
                    factorial, sympify, Rational, log,
                    Derivative, factor, Eq, simplify, expand,
                    exp, sin, cos, cosh, sinh, pi, sqrt, atan, Limit,
                    expand_trig, trigsimp, srepr, Integer, Pow, Add)

from sympy import UnevaluatedExpr as ueval

from sympy.core.numbers import One, Integer
from sympy.simplify.fu import TR1, TR2, TR2i, fu, TR16, TR22


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

# _applied_integration_manager
# AppliedIntegrationManager
        
class AppliedIntegrationManager(BaseManager):
    '''
    Integration 
    Example:
        ke = AppliedIntegrationManager()
        ke
        
        ke.printProblemTypes()
        
        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(AppliedIntegrationManager, cls).__new__(cls, *args, **kwargs)
        for base_class in AppliedIntegrationManager.__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_area_under_a_curve',
            '_problem_calculate_curve_length',
            '_problem_calculate_surface_area_of_rotation',
             ])
        pass
    

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

        doc_style: xv_doc
        """
        index = kwargs.get("index", None)
        
        auc_helper = self.base_helper.get_calculus_helper().get_area_under_curve_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        expr, x, x_limits = auc_helper.get_random_area_curve_data_set(index = index)
        
        result = auc_helper.get_area_under_a_curve(expr, x, x_limits)
        
        
        prob_s = f"""
        {result['problem_s']}
        
        """
        
        problemTemplate.add_problem(prob_s.strip(), 'text/html')


        soln_s = f"""
        {result['svg_str']}
        
        {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 _problem_calculate_curve_length(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        index = kwargs.get("index", None)
        
        cl_helper = self.base_helper.get_calculus_helper().get_curve_length_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        expr, x, x_limits = cl_helper.get_random_curve_length_data_set(index = index)
        
        result = cl_helper.get_curve_length(expr, x, x_limits)
        
        
        prob_s = f"""
        {result['problem_s']}
        
        """
        
        problemTemplate.add_problem(prob_s.strip(), 'text/html')


        soln_s = f"""
        {result['svg_str']}
        
        {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 _problem_calculate_surface_area_of_rotation(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        index = kwargs.get("index", None)
        
        sar_helper = self.base_helper.get_calculus_helper().get_surface_area_of_rotation_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        result = sar_helper.get_random_surface_area_of_rotation(index = index)
        
        
        prob_s = f"""
        {result['problem_s']}
        
        """
        
        problemTemplate.add_problem(prob_s.strip(), 'text/html')


        soln_s = f"""
        {result['svg_str']}
        
        {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

