import pp
import gdslib as gl
from simphony.netlist import Subcircuit

delta_length = 10
DL = delta_length/2
c = pp.c.mzi(DL=DL) 
pp.qp(c)

n = c.get_netlist()

"""
Explanation: Circuit simulations
get_netlist() returns a dict with all the instance settings and connections for a Component
There is another repo gdslib that stores all gdsfactory Sparameters (from Lumerical 3D FDTD simulations) and compact models (from SiPANN library) and allows you to do circuit simulations with Simphony
MZI
End of explanation
"""


m = gl.components.mzi(DL=DL)
gl.plot_circuit(m)

"""
Explanation: gdslib has also an MZI model. However we can make sure we get the exact cells from the component
End of explanation
"""


component = pp.c.mzi()
circuit = Subcircuit(component.name)
n = component.get_netlist()

model_name_tuple = []
for i in n.instances.keys():
    component_type = n.instances[i]['component']
    component_settings = n.instances[i]['settings']
    c = pp.c.component_type2factory[component_type]() # ignore settings
    #c = pp.c.component_type2factory[component_type](**component_settings) # needs fix
    model = gl.model_from_gdsfactory(c)
    model_name_tuple.append((model, i))
    
e = circuit.add(model_name_tuple)

for k, v in n.connections.items():
    c1, p1 = k.split(',')
    c2, p2 = v.split(',')
    circuit.connect(c1, p1, c2, p2)

circuit.elements["mmi1x2_12_0"].pins["W0"] = "input"
circuit.elements["mmi1x2_88_0"].pins["W0"] = "output"

gl.plot_circuit(circuit)

"""
Explanation: In this first case we are going to use Sparameters from Lumerical simulations stored in gdslib.
Some simulations (such as waveguides with different lengths and bends with 10um bend radius) may be missing so we ignore all the settings and will use the defaults (bends with radius = 5 and waveguides with length = 10
End of explanation
"""


component = pp.c.mzi()
circuit = Subcircuit(component.name)
n = component.get_netlist()

model_name_tuple = []
for i in n.instances.keys():
    component_type = n.instances[i]['component']
    component_settings = n.instances[i]['settings']
    model = gl.component_type2factory[component_type](**component_settings)
    model_name_tuple.append((model, i))
    
e = circuit.add(model_name_tuple)

for k, v in n.connections.items():
    c1, p1 = k.split(',')
    c2, p2 = v.split(',')
    circuit.connect(c1, p1, c2, p2)

circuit.elements["mmi1x2_12_0"].pins["W0"] = "input"
circuit.elements["mmi1x2_88_0"].pins["W0"] = "output"

gl.plot_circuit(circuit)

delta_length = 100
DL = delta_length/2
c = pp.c.mzi(DL=DL) 
pp.qp(c)

m = gl.circuit_from_gdsfactory(c)
m.elements["mmi1x2_12_0"].pins["W0"] = "input"
m.elements["mmi1x2_88_0"].pins["W0"] = "output"

gl.plot_circuit(m)

for e in m.elements:
    print(e)

"""
Explanation: It is hard to capture all possible variations by doing lots of lumerical simulations. That is why component Compact models from gdslib are powerful.
End of explanation
"""


import pp
import gdslib as gl
from simphony.netlist import Subcircuit

c = pp.c.ring_single(length_x=4, gap=0.2, bend_radius=5)
pp.qp(c)
m = gl.circuit_from_gdsfactory(c)

for e in m.elements:
    print(e)

m = gl.circuit_from_gdsfactory(c)
m.elements["coupler_ring_05cda304_-2_1"].pins["W0"] = "input"
m.elements["coupler_ring_05cda304_-2_1"].pins["E0"] = "output"
gl.plot_circuit(m)

component = c
circuit = Subcircuit(component.name)
n = component.get_netlist()

model_name_tuple = []
for i in n.instances.keys():
    component_type = n.instances[i]['component']
    component_settings = n.instances[i]['settings']
    print(component_type, component_settings)
    model = gl.component_type2factory[component_type](**component_settings)
    model_name_tuple.append((model, i))
    
e = circuit.add(model_name_tuple)

for k, v in n.connections.items():
    c1, p1 = k.split(',')
    c2, p2 = v.split(',')
    circuit.connect(c1, p1, c2, p2)

circuit.elements["coupler_ring_05cda304_-2_1"].pins["W0"] = "input"
circuit.elements["coupler_ring_05cda304_-2_1"].pins["E0"] = "output"
gl.plot_circuit(circuit)

import matplotlib.pyplot as plt

for e in m.elements:
    continue
    plt.figure()
    plt.title(e)
    gl.plot_model(e.model)

print(n.connections.pretty())

import pp
import gdslib as gl
from simphony.netlist import Subcircuit
#from pp.config import connections

#connections.clear()
c = pp.c.ring_double(length_y=10)
m = gl.circuit_from_gdsfactory(c)

for e in m.elements:
    print(e)

from pp.config import connections
connections

m.elements["coupler_ring_8a8f8eb7_-2_1"].pins["W0"] = "input"
m.elements["coupler_ring_8a8f8eb7_-2_1"].pins["E0"] = "output"
gl.plot_circuit(m)

m.netlist.nets

"""
Explanation: Ring resonator
This is work in progress and still has some issues. FIXME!
End of explanation
"""


