'''
Created on 16-Apr-2021

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

import sympy

from ..helper import BaseHelper

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

        
class CircuitPlottingManager(BaseManager):

    '''
    Circuit Plotting
    Example:
        ke = CircuitPlottingManager()
        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(CircuitPlottingManager, cls).__new__(cls, *args, **kwargs)
        for base_class in CircuitPlottingManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        self.base_helper = BaseHelper(**kwargs)
        pass


    def _SCHEMDRAW_TEMPLATE(self, 
                            method_name = None,
                            problem_statement = None,
                            *args, **kwargs):
        sd_helper = self.base_helper.get_schem_draw_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        result = sd_helper.get_code_and_graph(method_name)
        if result is None:
            problemTemplate.add_problem(f"""
            The SchemDrawHelper has no method named {method_name}
            """.strip(), 'text/html')
            return problemTemplate

        self.drw = result.get('method')()
        
        problem_s = f"""
        {problem_statement}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        <pre>{result['drw_code']}
        </pre>
        """
        problemTemplate.add_solution(soln_s.strip(), 'plane/text')
        
        answer_s = f"""
        Run the following:
        ke.drw.draw() 
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    


    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
                '_problem_Capacitor_Network',
                '_problem_Discharging_Capacitor',
                '_problem_Infinite_Transmission_Line',
                '_problem_ECE201_Style_Circuit',
                
             ])
        pass


    def _problem_Capacitor_Network(self, *args, **kwargs):
        method_name = '_problem_Capacitor_Network'
        problem_statement = 'Draw a Capacitor Network'
        return self._SCHEMDRAW_TEMPLATE(method_name, 
                                        problem_statement,
                                        *args, **kwargs
                                        )
    
    def _problem_Discharging_Capacitor(self, *args, **kwargs):
        method_name = '_problem_Discharging_Capacitor'
        problem_statement = 'Draw a Discharging Capacitor'
        return self._SCHEMDRAW_TEMPLATE(method_name, 
                                        problem_statement,
                                        *args, **kwargs
                                        )
    
    def _problem_Infinite_Transmission_Line(self, *args, **kwargs):
        method_name = '_problem_Infinite_Transmission_Line'
        problem_statement = 'Draw a Infinite Transmission Line'
        return self._SCHEMDRAW_TEMPLATE(method_name, 
                                        problem_statement,
                                        *args, **kwargs
                                        )

    def _problem_ECE201_Style_Circuit(self, *args, **kwargs):
        method_name = '_problem_ECE201_Style_Circuit'
        problem_statement = 'Draw a ECE201 Style Circuit'
        return self._SCHEMDRAW_TEMPLATE(method_name, 
                                        problem_statement,
                                        *args, **kwargs
                                        )
