'''
Created on 12-Jul-2021

@author: rishi_priyansh
'''
import inspect
import traceback
import random

from xv.km.util import PintUnitSystem

import matplotlib.pyplot as plt
from xv.km.ui import SvgHelper         

from xv.plotter import WavesPlotter2d
from xv.util import strip_source_code_tabs, get_function_source_code
# _diff_eq_helper
# DiffEqHelper
class DiffEqHelper:
    def __init__(self, **kwargs):
        self.verbose = kwargs.get('verbose', False)
        pass

    def plot_circle(self):
        method_name = "plot_circle"
        svg_str, plot_code = self._plot_specified_method(method_name)
        return svg_str, plot_code
        


    def _plot_specified_method(self, method_name):
        plt.ioff()
        plotter = WavesPlotter2d(verbose = self.verbose)
        
        if not hasattr(plotter, method_name):
            return None

        method = getattr(plotter, method_name)   
        plot_code = self._get_method_source_code(method)

        fig, ax = method()
        
        svgHelper = SvgHelper()
        svg_str = svgHelper.plt_to_svg()
        
        if not svg_str:
            if self.verbose:
                print(f"Error: svg_str could not be initialized")
            return None, plot_code
            
        plt.clf()
        plt.close()
        return svg_str, plot_code
        
        
        
    def _get_method_source_code(self, method):
        plot_code = get_function_source_code(method, 
                                             strip_tab = False)
        plot_code = plot_code[1:-2]
        plot_code = strip_source_code_tabs(plot_code)
        
        plot_code.append("plt.show()")
        
        plot_code = "\n".join(plot_code)
        return plot_code
        pass
    pass
#class DiffEqHelper:

    