'''
Created on 02-Mar-2022
@author: b_thakur
'''
import os 

import random
import inspect
import traceback


from ..helper import BaseHelper

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




# _physical_geography_manager
# PhysicalGeographyManager


class PhysicalGeographyManager(BaseManager):
    '''
    Minimum Grade: 3
    Maximum Grade: 12


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


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


    doc_style: xv_doc
    ''' 
    
    def __new__(cls, *args, **kwargs):
        instance = super(PhysicalGeographyManager, cls).__new__(cls, *args, **kwargs)
        for base_class in PhysicalGeographyManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass

    def _TEMPLATE_WEB_LINKS(self, search_str, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        
        wl_helper = self.base_helper.get_web_link_helper()
        result = wl_helper.search(search_str)
        

        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        if result is None:
            problemTemplate.add_problem(f"""
            No result found matching: 
                search_str = '{search_str}'
            """.strip(), 'text/html')
            return problemTemplate

        result = wl_helper.convert_links_to_strs(result)

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

        
        soln_s = result['anchors_str']
        problemTemplate.add_solution(soln_s.strip(), 'text/html',
                                     add_html_line_break = False)
        
        answer_s = soln_s
        problemTemplate.add_answer(answer_s.strip(), 'text/html',
                                   add_html_line_break = False)
        
        return problemTemplate
    
    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_coriolis_effect',
            '_problem_weather',
            '_problem_lightning',
            
            ])
        pass

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

        doc_style: xv_doc
        """
        search_str = 'coriolis effect'
        return self._TEMPLATE_WEB_LINKS(search_str, *args, **kwargs)
                
    def _problem_weather(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        search_str = 'weather'
        return self._TEMPLATE_WEB_LINKS(search_str, *args, **kwargs)
                
    def _problem_lightning(self, *args, **kwargs):
        """

        doc_style: xv_doc
        """
        search_str = 'lightning'
        return self._TEMPLATE_WEB_LINKS(search_str, *args, **kwargs)
                
