'''
Created on 16-Apr-2021

@author: rishi_priyansh

https://pythonforundergradengineers.com/circuit-diagram-problem-with-schemdraw.html
https://github.com/ProfessorKazarinoff/Engineering-Materials-Programming.git

'''
import random
import inspect
import traceback

import sympy

from ..helper import BaseHelper

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

# _electric_circuit_manager.py
# # ElectricCircuitManager
        
class ElectricCircuitManager(BaseManager):

    '''
    Electric Circuits 
    Example:
        ke = ElectricCircuitManager()
        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(ElectricCircuitManager, cls).__new__(cls, *args, **kwargs)
        for base_class in ElectricCircuitManager.__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_circuit_VR_simple',
                '_problem_circuit_R_resultant',
                '_problem_circuit_R_bridge',
                '_problem_circuit_VC_simple',
                '_problem_circuit_C_resultant',
                '_problem_LCR_circuit',
                
             ])
        pass

    def _problem_circuit_VR_simple(self, *args, **kwargs):
        ec_helper = self.base_helper.get_electric_circuit_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        result = ec_helper.get_problem_circuit_VR_simple(**kwargs)

        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_circuit_R_resultant(self, *args, **kwargs):
        ec_helper = self.base_helper.get_electric_circuit_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        result = ec_helper.get_problem_circuit_R_resultant(**kwargs)

        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_circuit_R_bridge(self, *args, **kwargs):
        ec_helper = self.base_helper.get_electric_circuit_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        result = ec_helper.get_problem_circuit_R_bridge(**kwargs)

        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_circuit_VC_simple(self, *args, **kwargs):
        ec_helper = self.base_helper.get_electric_circuit_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        result = ec_helper.get_problem_circuit_VC_simple(**kwargs)

        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_circuit_C_resultant(self, *args, **kwargs):
        ec_helper = self.base_helper.get_electric_circuit_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        result = ec_helper.get_problem_circuit_C_resultant(**kwargs)

        prob_s = f"""
        {result['prob_s']}
        """
        problemTemplate.add_problem(prob_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    

    def _problem_LCR_circuit(self, *args, **kwargs):
        ec_helper = self.base_helper.get_electric_circuit_helper()
        
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        circuit_type = 'LCR'
        result = ec_helper.get_problem_LCR_circuit()
        if result is None:
            problemTemplate.add_problem(f"""
            Failed to get circuit_type = {circuit_type}
            """.strip(), 'text/html')
            return problemTemplate

        problem_s = f"""
        {result['problem_s']}
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        {result['soln_s']}
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        {result['answer_s']}
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    

