'''
Created on 16-Apr-2021

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

from xv.km.ui import SvgHelper  

from sympy import ( symbols, diff, Symbol, Mul, Matrix, 
                    factorial, sympify, Rational,
                    Derivative, factor,
                    exp, sin, cos, cosh, sinh, pi, sqrt, atan)



from sympy.core.expr import UnevaluatedExpr

from xv.km.util import latex_fraction

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

# _popular_shapes_manager
# PopularShapesManager
        
class PopularShapesManager(BaseManager):
    '''
    Popular shapes like soccer field etc.


    Minimum Grade: 3
    Maximum Grade: 12


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


    doc_style: xv_doc
    ''' 


    def __new__(cls, *args, **kwargs):
        instance = super(PopularShapesManager, cls).__new__(cls, *args, **kwargs)
        for base_class in PopularShapesManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        self.geometry_helper = self.base_helper.get_geometry_helper()
        pass


    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_plot_a_soccer_field',
            '_problem_plot_a_soccer_field_png',
         ])
        pass



    def _problem_plot_a_soccer_field(self, *args, **kwargs):
        """
        soccer field

        Minimum Grade: 4
        Maximum Grade: 12


    
        Examples
        --------
        from xv.math.geometry import PopularShapesManager
        ke = PopularShapesManager()


        ke.getRandomProblem(problem_type = '_problem_plot_a_soccer_field')


        doc_style: xv_doc
        """

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

        psh = self.geometry_helper.get_popular_shapes_helper()
        plot_code, svg_str = psh.plot_a_soccer_field(use_cache = False,
                                                     refresh_cache = False)
        
        problem_s = f"""
        Plot a soccer field.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Run the following code to generate a sample notebook:
        ke.generateNotebook(templateName = 'soccer_field')
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        problemTemplate.add_solution(svg_str, 'text/svg')


        soln_s = f"""
        <h2>PLot Code:</h2>
        <pre>{plot_code}</pre>
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        Run the following code to generate a sample notebook:
        ke.generateNotebook(templateName = 'soccer_field')
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        problemTemplate.add_answer(svg_str, 'text/svg')
        
        return problemTemplate

    def _problem_plot_a_soccer_field_png(self, *args, **kwargs):
        """
        soccer field

        Minimum Grade: 4
        Maximum Grade: 12


    
        Examples
        --------
        from xv.math.geometry import PopularShapesManager
        ke = PopularShapesManager()


        ke.getRandomProblem(problem_type = '_problem_plot_a_soccer_field')


        doc_style: xv_doc
        """

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

        psh = self.geometry_helper.get_popular_shapes_helper()
        plot_code, png_str = psh.plot_a_soccer_field_to_png(use_cache = False,
                                                            refresh_cache = False)
        
        self.png_str = png_str
        
        problem_s = f"""
        Plot a soccer field in png.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Run the following code to generate a sample notebook:
        ke.generateNotebook(templateName = 'soccer_field')
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        problemTemplate.add_solution(png_str, 'img/png')


        soln_s = f"""
        <h2>PLot Code:</h2>
        <pre>{plot_code}</pre>
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        Run the following code to generate a sample notebook:
        ke.generateNotebook(templateName = 'soccer_field')
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        problemTemplate.add_answer(png_str, 'image/png')
        
        return problemTemplate

