'''
Created on 03-June-2024

@author: B Thakur

Space time momentum energy

'''
import random
import inspect
import traceback

from ..helper import BaseHelper

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



# _fluid_mechanics_manager
# FluidMechanicsManager


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


    

    def get_fluid_system(self, *args, **kwargs):
        if self._fluid_system is None:
            from xv.km.mathsys import MathSystemFactory, FluidSystem
            math_sys = MathSystemFactory.GET_INSTANCE(FluidSystem, 
                                                      use_cache = True,
                                                      verbose = self.verbose)
            self._fluid_system = math_sys
            pass

        return self._fluid_system
        pass
    

    def _init_problem_types(self, *args, **kwargs):
        
        self._append_problem_types([
            '_problem_comprehension_fluid_physics',
            '_problem_incompressible_fluid',
            '_problem_math_system_random_problem',
         ])
        pass

 
    def _problem_comprehension_fluid_physics(self, *args, **kwargs):
        '''
        '''
        
        folder_path = "comprehension/physics"
        search = "middle_school_fluid_physics.txt"
        
        search_in_content = kwargs.get("search", None)
        file_paths = self.get_comp_factory().search_comprehension_files(folder_path,
                                                                        search_in_name = search,
                                                                        search_in_content = search_in_content)

        file_path = random.choice(file_paths)
        
        return self.get_comp_factory().PROBLEM_TEMPLATE_COMP_RANDOM_QUESTIONS(file_path, *args, **kwargs)



    def _problem_incompressible_fluid(self, *args, **kwargs):
        fluid_mechanics_helper = self.base_helper.get_fluid_mechanics_helper()
        
        result = fluid_mechanics_helper.get_problem_incompressible_fluid(**kwargs)
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        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_math_system_random_problem(self, *args, **kwargs):
        
        math_sys = self.get_fluid_system()
        
        result = math_sys.get_random_problem(**kwargs)

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        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


