#import "@preview/zap:0.5.0"
#import "@preview/cetz:0.4.2"

#import "tumCustomSymbols.typ" as joham
#import "../common_rewrite.typ" : *


#let opampTable = table(
  columns: (auto, auto, auto, auto, auto, auto),
  align: center + horizon,
  inset: (x, y) => if(y > 0) {3mm} else {2mm},
  fill: (x, y) => if calc.rem(x, 2) == 1 { tableFillHigh } else { tableFillLow },

  table.header(
    [*Spannungsfolger*], 
    [*Invertierender Verstärker*], 
    [*Nicht invertierender Verstärker*], 
    [*Ideale Diode*], 
    [*NIK*], 
    [*Gyrator*]),
  zap.circuit({
    import zap: *

    set-style(vsource: (radius: 0.3))

    joham.ideal-opamp("O", (2, 0), scale: 0.4, invert: true)

    node("MU", (rel: (0.2, 0), to: "O.out"))
    cetz.draw.anchor("MM", (rel: (0, -0.65), to: "MU"))
    node("MB", (2, -1))


    node("A", (rel: (-0.7, 0), to: "O.plus"), fill: false)
    node("C", (rel: (0.7, 0), to: "O.out"), fill: false)
    node("D", (rel: (0.7, -1), to: "O.out"), fill: false)
    node("B", ("A", "|-", "MB"), fill: false)


    wire("O.ground", "MB")
    wire("B", "MB")
    wire("MB", "D")

    wire("A", "O.plus")
    wire("O.out", "C")

    zwire("O.minus", "MM", ratio: -30%)
    wire("MM", "MU")

    joham.ground("G1", "MB", scale: 0.7)

    joham.voltage("A", "B", $u_"in"$)
    joham.voltage("C", "D", anchor: "west", $u_"out"$)
  }),
  zap.circuit({
    import zap: *

    set-style(vsource: (radius: 0.3))

    let height = 2 / 3;

    node("A", (0, height), fill: false)
    node("B", (0, -height), fill: false)
    node("C", (3, height), fill: false)
    node("D", (3, -height), fill: false)

    node("MU", (1.25, height))
    node("MB", (1.25, -height))

    node("OU", (2.5, height))
    node("OB", (2, -height))

    joham.ideal-opamp("O", (2, 0), scale: 0.4)

    resistor("R1", "A", "MU", scale: 0.4, label: (content: $R_1$, distance: 3pt), fill: none)
    wire("B", "MB")

    zwire("MU", "O.minus", ratio: 0%)
    zwire("MB", "O.plus", ratio: 0%)

    resistor("R0", "MU", "OU", scale: 0.4, label: (content: $R_0$, distance: 3pt))
    wire("OU", "C")

    wire("O.ground", "OB")
    wire("MB", "OB")
    wire("OB", "D")

    zwire("O.out", "OU", ratio: 100%)

    joham.ground("G1", "MB", scale: 0.7)

    joham.voltage("A", "B", $u_"in"$)
    joham.voltage("C", "D", anchor: "west", $u_"out"$)
  }),
  zap.circuit({
    import zap: *

    set-style(vsource: (radius: 0.3))

    joham.ideal-opamp("O", (2, 0), scale: 0.4, invert: true)

    node("MU", (rel: (0.2, 0), to: "O.out"))
    node("MM", (rel: (0, -1), to: "MU"))
    node("MB", (rel: (0, -1), to: "MM"))

    node("A", (rel: (-0.7, 0), to: "O.plus"), fill: false)
    node("C", (rel: (1, 0), to: "O.out"), fill: false)
    node("D", (rel: (1, -2), to: "O.out"), fill: false)
    node("B", ("A", "|-", "MB"), fill: false)

    node("G", (2, -2))

    wire("O.ground", "G")

    resistor("R0", "MU", "MM", scale: 0.4, label: (content: $R_0$, distance: 3pt), fill: none)
    resistor("R1", "MM", "MB", scale: 0.4, label: (content: $R_1$, distance: 3pt), fill: none)

    wire("MB", "D")

    wire("A", "O.plus")
    wire("O.out", "C")
    wire("B", "MB")

    zwire("O.minus", "MM", ratio: -30%)

    joham.ground("G1", "G", scale: 0.7)

    joham.voltage("A", "B", $u_"in"$)
    joham.voltage("C", "D", anchor: "west", $u_"out"$)
  }),
  zap.circuit({
    import zap: *

    set-style(vsource: (radius: 0.3))

    joham.ideal-opamp("O", (2, 0), scale: 0.4)

    node("G", (2, -0.75))
    node("MU", (rel: (-0.4, 0), to: "O.minus"))
    node("MB", ("MU", "|-", "G"))

    node("A", (rel: (-1.2, 0), to: "O.minus"), fill: false)
    node("B", ("A", "|-", "G"), fill: false)

    joham.diode("D1", (rel: (0, 0.5), to: "MU"), (rel: (1.4, 0)), scale: 0.6, type: "real")

    wire("O.ground", "G")
    wire("B", "G")

    wire("A", "O.minus")

    zwire("MB", "O.plus", ratio: 0%)

    wire("D1.in", "MU")
    zwire("D1.out", "O.out", ratio: 0%)

    joham.ground("G1", "G", scale: 0.7)

    joham.voltage("A", "B", $u$)
  }),
  zap.circuit({
    import zap: *

    node("A", (-1.4, 0), fill: false)
    node("C", (1.4, 0), fill: false)
    node("B", (-1.4, -1.4), fill: false)
    node("D", (1.4, -1.4), fill: false)

    node("E", (0.7, -1.4))
    node("F", (0, 0))

    joham.ideal-opamp("O", (0, -0.6), scale: 0.4, angle: 90deg)

    wire("B", "E")
    wire("D", "E")

    node("G", (to: "A", rel: (0.4, 0)))
    node("H", (to: "C", rel: (-0.4, 0)))

    wire("A", "G", i: (content: $i_"in"$, distance: 3pt, scale: 0.7))
    wire("C", "H")

    resistor("R1", "G", "F", scale: 0.4, label: (content: $R$, anchor: "south", distance: 3pt), fill: none)
    resistor("R2", "H", "F", scale: 0.4, label: (content: $R$, anchor: "north", distance: 3pt), fill: none)

    zwire("G", "O.minus", ratio: 115%, axis: "y")
    zwire("H", "O.plus", ratio: 115%, axis: "y")

    wire("O.out", "F")

    zwire("E", "O.ground", ratio: 0%)

    joham.ground("G1", "E", scale: 0.7)

    resistor("R3", (rel: (0.2, 0), to: "C"), (rel: (0.2, 0), to: "D"), scale: 0.4, label: (content: $R_L$, distance: 3pt), fill: none)

    wire("R3.in", "C")
    wire("R3.out", "D")

    joham.voltage("A", "B", $u_"in"$)
  }),
  scale(80%, zap.circuit({
    import zap: *

    set-style(vsource: (radius: 0.3))

    joham.ideal-opamp("O1", (-1, 0), scale: 0.4, angle: 90deg, invert: true)
    joham.ideal-opamp("O2", (1, 0), scale: 0.4, angle: 90deg)

    node("MM", (0, -0.6))
    node("MB", (0, -0.9))
    node("MBL", (rel: (-0.3, 0), to: "MB"))
    node("MBR", (rel: (0.3, 0), to: "MB"))
    node("MU", (0, 1))

    node("RU", (1, 1))
    node("LU", (-1, 1))

    node("LLU", (rel: (-1, 0), to: "LU"))
    node("RRU", (rel: (1.3, 0), to: "RU"))

    node("A", (rel: (-0.5, 0), to: "LLU"), fill: false)
    node("B", ("A", "|-", "MB"), fill: false)

    node("D", (rel: (0.2, 0.4), to: "RU"), fill: false)
    node("C", (rel: (-0.2, 0.4), to: "RRU"), fill: false)

    resistor("R1", "MU", "LU", scale: 0.4, label: (content: $R$, distance: 3pt, anchor: "south"))
    resistor("R2", "RU", "MU", scale: 0.4, label: (content: $R$, distance: 3pt, anchor: "south"))

    resistor("R3", "LU", "LLU", scale: 0.4, label: (content: $R$, distance: 3pt, anchor: "south"))

    resistor("R4", (rel: (0.3, 0), to: "RRU"), ((rel: (0.3, 0), to: "RRU"), "|-", "MB"), scale: 0.4, label: (content: $R$, distance: 3pt))

    joham.ground("G", "MB", scale: 0.7)

    wire("O1.out", (rel: (0, 0.2)), (rel: (0, -0.2), to: "RU"), "RU")
    wire("O2.out", (rel: (0, 0.2)), (rel: (0, -0.2), to: "LU"), "LU")

    wire("MM", "MU")

    zwire("O1.minus", "MM", ratio: 0%)
    zwire("O2.minus", "MM", ratio: 0%)

    wire("O1.plus", ("O1.plus", "|-", "MM"), ("MM", "-|", "LLU"), "LLU")

    wire("LLU", "A")
    wire("MBL", "B")

    wire("MBL", "MB")
    wire("MBR", "MB")

    zwire("O1.ground", "MBL", ratio: 100%)
    zwire("O2.vcc", "MBR", ratio: 100%)

    wire("O2.plus", ("O2.plus", "|-", "MM"), ("MM", "-|", "RRU"), "RRU")

    wire("R4.in", "RRU")
    wire("R4.out", "MBR")

    zwire("D", "RU", ratio: 0%)
    zwire("C", "RRU", ratio: 0%)

    joham.voltage("A", "B", $u_"in"$)
    joham.voltage("D", "C", anchor: "south", $u_"out"$)
  })),
  $u_"out" = u_"in"$,
  $u_"out" = - R_0/R_1 u_"in"$,
  $u_"out" = (1 + R_0 / R_1) u_"in"$,
  $$,
  $u_"in" = -R_L i_"in"$
)