'''
Created on 13-Oct-2024

@author: b thakur

'''
from sympy import symbols, Eq, solve, Symbol, latex, sqrt, UnevaluatedExpr
from xv.km.ui import SvgHelper  
import matplotlib.pyplot as plt

import random
import inspect
import traceback

from ..helper import BaseHelper

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

from xv.km.util import KmTemplateUtil



# _geo_overview_manager
# GeoOverviewManager

class GeoOverviewManager(BaseManager):
    '''
    GeoOverviewManager
    Example:
        ke = GeoOverviewManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
        ke.printProblemTypes()
        
    '''    
    _OPR_TYPE_COUNT = 10
    
    _DEFULT_TERMS_DESCRIPTION_FILE_PATH = "geo/common/geo_terms_description.csv"
    
    def __new__(cls, *args, **kwargs):
        instance = super(GeoOverviewManager, cls).__new__(cls, *args, **kwargs)
        for base_class in GeoOverviewManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        

        # not implemented
        self._term_description_helper = None
        pass
    
    
    def get_term_description_helper(self):
        if self._term_description_helper is None:
            self._init_term_description_helper()
        return self._term_description_helper
        pass

    def _init_term_description_helper(self, **kwargs):  
        from xv.km.helper import TermDescriptionHelper
        file_path = self.__class__._DEFULT_TERMS_DESCRIPTION_FILE_PATH
        self._term_description_helper = TermDescriptionHelper(file_path = file_path,
                                                              verbose = self.verbose)
        pass
    
    
    
    
    
    


    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_term_description',

            '_problem_atmosphere',
            '_problem_water',
            '_problem_land',
            '_problem_saving_the_planet',
            '_problem_geo_overview',
            
            ])
        pass



    def _problem_term_description(self, *args, **kwargs):
        """
        
        """
        td_helper = self.get_term_description_helper()
        
        result = td_helper.get_random_problem()
        
        
        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_atmosphere(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/geo/common"
        search_file_paths = [
                "atmosphere.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)



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



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



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



    def _problem_geo_overview(self, *args, **kwargs):
        """
        
        """
        
        folder_path = "question_bank/geo/common"
        search_file_paths = [
                "geo_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)


