'''
Created on 16-Apr-2021

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


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

from sympy.core.numbers import One, Integer



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

# _ellipse_manager
# EllipseManager

        
class EllipseManager(BaseManager):
    '''
    Circle and its geometry
    Example:
        ke = EllipseManager()
        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(EllipseManager, cls).__new__(cls, *args, **kwargs)
        for base_class in EllipseManager.__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_plot_props_of_ellipse_first_set',
            '_problem_plot_props_of_ellipse_second_set',
         ])
        pass


    def _problem_plot_props_of_ellipse_first_set(self, *args, **kwargs):
        
        plotter_2d = self.base_helper.get_plotter_2d()
        svg_helper = self.base_helper.get_svg_helper()
        
        ellipse = Ellipse((0, 0), 2, 1)
        fig, ax = plotter_2d.plot(ellipse)
        plotter_2d.plot_hline(color = "lightgrey", ax = ax)
        plotter_2d.plot_vline(color = "lightgrey", ax = ax)

        plotter_2d.plot('hradius', ellipse, ax = ax,  color = "orange")
        legend_hradius = plotter_2d.create_legend_item('hradius', 'orange', shape = "line")

        plotter_2d.plot('vradius', ellipse, ax = ax,  color = "lightblue")
        legend_vradius = plotter_2d.create_legend_item('vradius', 'lightblue', shape = "line")

        plotter_2d.plot('radius', ellipse, ax = ax,  color = "yellow")
        legend_radius = plotter_2d.create_legend_item('radius', 'yellow', shape = "line")


        
        plotter_2d.plot(ellipse.center, ax = ax, color = "red")
        legend_center = plotter_2d.create_legend_item('center', 'red')

        plotter_2d.plot(*ellipse.foci, ax = ax, color = "green")
        legend_foci = plotter_2d.create_legend_item('foci', 'green')



        plotter_2d.legend(ax = ax, handles=[legend_center, 
                                            legend_foci, 
                                            legend_hradius,
                                            legend_vradius,
                                            legend_radius,
                                            ])
        
        svg_str = svg_helper.plt_to_svg(close_plot = True)
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        prob_s = f"""
        Draw an ellipse and show its center, foci, hradius, vradius and a random radius.
        """
        problemTemplate.add_problem(prob_s.strip(), "text/html")

        soln_s = f"""
        ellipse: $\\displaystyle {latex(ellipse)}$
        
        center: $\\displaystyle {latex(ellipse.center)}$
        
        foci: $\\displaystyle {latex(ellipse.foci)}$
        
        hradius: $\\displaystyle {latex(ellipse.hradius)}$
        
        vradius: $\\displaystyle {latex(ellipse.vradius)}$
        
        The length of the radius of an ellipse is not constant and depends on the selected point on the ellipse.
        """
        
        problemTemplate.add_solution(soln_s.strip(), "text/html")
                
        answer_s = f"""
        {svg_str}
        """
        
        problemTemplate.add_answer(answer_s.strip(), "text/html")
                
        return problemTemplate
        pass


    def _problem_plot_props_of_ellipse_second_set(self, *args, **kwargs):
        
        plotter_2d = self.base_helper.get_plotter_2d()
        svg_helper = self.base_helper.get_svg_helper()
        
        ellipse = Ellipse((0, 0), 2, 1)
        fig, ax = plotter_2d.plot(ellipse, color = "blue", fill = True)
        legend_ellipse = plotter_2d.create_legend_item('ellipse', 'blue', shape = "line")

        plotter_2d.plot_hline(color = "lightgrey", ax = ax)
        plotter_2d.plot_vline(color = "lightgrey", ax = ax)


        plotter_2d.legend(ax = ax, handles=[legend_ellipse, 
                                            ])
        
        svg_str = svg_helper.plt_to_svg(close_plot = True)
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        prob_s = f"""
        Draw an ellipse and show its outer lines.
        What is the area of the ellipse.
        """
        problemTemplate.add_problem(prob_s.strip(), "text/html")

        soln_s = f"""
        
        """
        
        problemTemplate.add_solution(soln_s.strip(), "text/html")
                
        answer_s = f"""
        ellipse: $\\displaystyle {latex(ellipse)}$
        
        area: $\\displaystyle {latex(ellipse.area)}$

        {svg_str}
        """
        
        problemTemplate.add_answer(answer_s.strip(), "text/html")
                
        return problemTemplate
        pass

