'''
Created on 07-Jul-2021

@author: bthakur
'''

from sympy import latex, Symbol

# _physics_schem_draw_helper
# PhysicsPhysicsSchemDrawHelper

from xv.plotter.helper import BaseSchemDrawHelper

class PhysicsSchemDrawHelper(BaseSchemDrawHelper):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
    def draw_parallel_inductances(self, *inductances, **kwargs):
        import schemdraw.elements as elm
        comp_cls = elm.Inductor
        return self._draw_parallel_components(comp_cls, *inductances, **kwargs)
        pass
    
    def draw_series_inductances(self, *inductances, **kwargs):
        import schemdraw.elements as elm
        comp_cls = elm.Inductor
        return self._draw_series_components(comp_cls, *inductances, **kwargs)
        pass

    def draw_parallel_capacitors(self, *capacitors, **kwargs):
        import schemdraw.elements as elm
        comp_cls = elm.Capacitor
        return self._draw_parallel_components(comp_cls, *capacitors, **kwargs)
        pass
    
    def draw_series_capacitors(self, *capacitors, **kwargs):
        import schemdraw.elements as elm
        comp_cls = elm.Capacitor
        return self._draw_series_components(comp_cls, *capacitors, **kwargs)
        pass

    def draw_parallel_resistances(self, *resistances, **kwargs):
        import schemdraw.elements as elm
        comp_cls = elm.Resistor
        return self._draw_parallel_components(comp_cls, *resistances, **kwargs)
        pass
    
    def draw_series_resistances(self, *resistances, **kwargs):
        import schemdraw.elements as elm
        comp_cls = elm.Resistor
        return self._draw_series_components(comp_cls, *resistances, **kwargs)
        pass

    def _draw_parallel_components(self, comp_cls, *components, **kwargs):
        V = kwargs.get('V', Symbol('V'))
    
        import schemdraw
        import schemdraw.elements as elm
    
    
        with schemdraw.Drawing(show=False) as d2:
            d2 += elm.Line().left()
            d2 += elm.SourceSin().down().label(f'${latex(V)}$')
            d2 += elm.Line().right()
    
            for component in components:
                with schemdraw.Drawing(show=False) as d1:
                    #d1 += elm.Resistor()
                    d1 += elm.Line()
                    d1.push()
                    d1 += comp_cls().up().label(f'${latex(component)}$')
                    d1 += elm.Line().left()
                    d1.pop()
                d2 += elm.ElementDrawing(d1)
    
            return d2
        pass
    
    def _draw_series_components(self, comp_cls, *components, **kwargs):
        V = kwargs.get('V', Symbol('V'))
        
        import schemdraw
        import schemdraw.elements as elm
        
        d = schemdraw.Drawing()
        for component in components:
            d += comp_cls().right().label(f'${latex(component)}$')

        d += elm.Line().down()
        for component in components:
            d += elm.Line().left()
            
        d += elm.SourceSin().up().label(f'${latex(V)}$')
        return d
        pass
    
    
    def _problem_Capacitor_Network(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm
        
        d = schemdraw.Drawing(fontsize=12)
        A  = d.add(elm.Dot(label='a'))
        C1 = d.add(elm.Capacitor(label='8nF'))
        C2 = d.add(elm.Capacitor(label='18nF'))
        C3 = d.add(elm.Capacitor(botlabel='8nF', d='down'))
        C4 = d.add(elm.Capacitor(botlabel='32nF', d='left'))
        C5 = d.add(elm.Capacitor(botlabel='40nF'))
        B  = d.add(elm.Dot(label='b'))
        C6 = d.add(elm.Capacitor(label='2.8nF', endpts=[C1.end,C5.start]))
        C7 = d.add(elm.Capacitor(endpts=[C2.end,C5.start]))
        C7.add_label('5.6nF', loc='center', ofst=[-.3,-.1], align=('right','bottom'))
        return d        
    
    def _problem_Discharging_Capacitor(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm
        d = schemdraw.Drawing()
        V1 = d.add(elm.SourceV(label='5V'))
        d.add(elm.Line(d='right', l=d.unit*.75))
        S1 = d.add(elm.SwitchSpdt2(d='up', action='close', anchor='b', rgtlabel='$t=0$'))
        d.add(elm.Line(d='right', xy=S1.c,  l=d.unit*.75))
        d.add(elm.Resistor(d='down', label=r'$100\Omega$', botlabel=['+','$v_o$','-']))
        d.add(elm.Line(to=V1.start))
        d.add(elm.Capacitor(xy=S1.a, d='down', toy=V1.start, label=r'1$\mu$F'))
        d.add(elm.Dot())
        return d        
    
    def _problem_Infinite_Transmission_Line(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm
        d1 = schemdraw.Drawing()
        d1.add(elm.Resistor())
        d1.push()
        d1.add(elm.Capacitor('down'))
        d1.add(elm.Line('left'))
        d1.pop()
        
        d = schemdraw.Drawing()
        for i in range(3):
            d.add(elm.ElementDrawing(d1))
        
        d.push()
        d.add(elm.Line(l=d.unit/6))
        d.add(elm.DotDotDot())
        d.add(elm.ElementDrawing(d1))
        d.pop()
        d.here = [d.here[0], d.here[1]-d.unit]
        d.add(elm.Line('right', l=d.unit/6))
        d.add(elm.DotDotDot())
        return d
    
    def _problem_ECE201_Style_Circuit(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm
        
        d = schemdraw.Drawing(unit=2)  # unit=2 makes elements have shorter than normal leads
        d.push()
        R1 = d.add(elm.Resistor(d='down', label=r'20$\Omega$'))
        V1 = d.add(elm.SourceV(d='down', reverse=True, label='120V'))
        d.add(elm.Line(d='right', l=3))
        d.add(elm.Dot())
        d.pop()
        d.add(elm.Line(d='right', l=3))
        d.add(elm.Dot())
        d.add(elm.SourceV(d='down', label='60V', reverse=True))
        d.add(elm.Resistor(label=r'5$\Omega$'))
        d.add(elm.Dot())
        d.add(elm.Line(d='right', l=3))
        d.add(elm.SourceI(d='up', label='36A'))
        d.add(elm.Resistor(label=r'10$\Omega$'))
        d.add(elm.Dot())
        d.add(elm.Line(d='left', l=3, move_cur=False))
        d.add(elm.Line(d='right', l=3))
        d.add(elm.Dot())
        R6 = d.add(elm.Resistor(d='down', toy=V1.end, label=r'6$\Omega$'))
        d.add(elm.Dot())
        d.add(elm.Line(d='left', l=3, move_cur=False))
        d.add(elm.Resistor(d='right', xy=R6.start, label=r'1.6$\Omega$'))
        d.add(elm.Dot(label='a'))
        d.add(elm.Line(d='right', xy=R6.end))
        d.add(elm.Dot(label='b'))
        return d
    
    def _problem_physics_iit_current_electricity_10001_a(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm
        
        d = schemdraw.Drawing()  # unit=2 makes elements have shorter than normal leads
        d.push()
        line1 = elm.Line(d = 'up')
        d.add(line1)
        d.add(elm.Resistor(label = r'2$\Omega$').theta(45))
        d.add(elm.Dot(label = 'A'))
        d.add(elm.Resistor(label = r'4$\Omega$').theta(315))
        line2 = elm.Line(d = 'down')
        d.add(line2)
        V = elm.SourceV().endpoints(line2.end, line1.start).label('9$V$', loc = 'bottom')
        d.add(V)
        d.pop()
        d.add(elm.Resistor(label = r'4$\Omega$', at=line1.end).theta(315))
        d.add(elm.Dot(label = 'B'))
        d.add(elm.Resistor(label = r'2$\Omega$', to=line2.start))
        return d
    
    def _problem_physics_iit_current_electricity_10001_b(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm

        d = schemdraw.Drawing()  # unit=2 makes elements have shorter than normal leads
        d.push()

        # Create line up
        line_up = elm.Line(d = 'up')
        d.add(line_up)

        # Add end_point of line_up
        line_up_end_point = elm.Dot(label = 'C', at=line_up.end)
        d.add(line_up_end_point)

        # Add resistance to right of line_up_end_point
        res1 = elm.Resistor(label = r'6$\Omega$', d = 'right', at=line_up_end_point.end)
        d.add(res1)

        # Add end_point of res1
        res1_end_point = elm.Dot(label = 'D', at=res1.end)
        d.add(res1_end_point)

        # Create line down
        line_down = elm.Line(d = 'down', at = res1_end_point.end)
        d.add(line_down)

        # Add Voltage source
        V = elm.SourceV().endpoints(line_down.end, line_up.start).label('9$V$', loc = 'bottom')
        d.add(V)

        # Add one more resistance between C and D
        res2 = elm.Resistor(label = r'6$\Omega$', at=line_up_end_point.end).theta(45)
        d.add(res2)

        # connect res2 to D
        line_down = elm.Line().endpoints(res2.end, res1_end_point.start)
        d.add(line_down)

        return d
    
    def _problem_physics_iit_current_electricity_10001_c(self, *args, **kwargs):
        import schemdraw
        import schemdraw.elements as elm

        d = schemdraw.Drawing()  # unit=2 makes elements have shorter than normal leads
        d.push()

        # Create line up
        line_up = elm.Line(d = 'up')
        d.add(line_up)

        # Add end_point of line_up
        line_up_end_point = elm.Dot(label = 'C', at=line_up.end)
        d.add(line_up_end_point)

        # Add resistance to right of line_up_end_point
        res1 = elm.Resistor(label = r'3$\Omega$', d = 'right', at=line_up_end_point.end)
        d.add(res1)

        # Add end_point of res1
        res1_end_point = elm.Dot(label = 'D', at=res1.end)
        d.add(res1_end_point)

        # Create line down
        line_down = elm.Line(d = 'down', at = res1_end_point.end)
        d.add(line_down)

        # Add Voltage source
        V = elm.SourceV().endpoints(line_down.end, line_up.start).label('9$V$', loc = 'bottom')
        d.add(V)

        
        return d
    
    pass
#class PhysicsSchemDrawHelper:
