#import "@preview/zap:0.5.0"
#import "@preview/cetz-plot:0.1.3"


#set page(width: auto, height: auto)

#let FetModelSubstrate = zap.circuit({
  import zap: *
  import cetz.draw: *

  rect(
    (0, 0),
    (12, 4),
    fill: rgb("#ffb1b1"),
    name: "p",
  )
  rect((0, -0.05), (12, -0.05), stroke: 3pt, name: "substrate")
  earth("g1", (11.5, 0))

  content("substrate", [Bulk], anchor: "north", padding: 0.2)
})

#let FetModel1 = zap.circuit({
  import zap: *
  import cetz.draw: *

  rect(
    (0, 0),
    (12, 4),
    fill: rgb("#ffb1b1"),
    name: "p",
  )
  rect((0, -0.05), (12, -0.05), stroke: 3pt, name: "substrate")
  earth("g1", (11.5, 0))

  rect((2.75, 3), (5.25, 4), fill: rgb("#61ff9f"), name: "kanal1", radius: (
    south: 0.2,
  ))
  rect((6.75, 3), (9.25, 4), fill: rgb("#61ff9f"), name: "kanal2", radius: (
    south: 0.2,
  ))

  content("kanal1", [n+], anchor: "center", padding: 0.2)
  content("kanal2", [n+], anchor: "center", padding: 0.2)
  content("substrate", [Bulk], anchor: "north", padding: 0.2)
})

#let FetModel2 = zap.circuit({
  import zap: *
  import cetz.draw: *

  rect(
    (0, 0),
    (12, 4),
    fill: rgb("#ffb1b1"),
    name: "p",
  )
  rect((0, -0.05), (12, -0.05), stroke: 3pt, name: "substrate")
  earth("g1", (11.5, 0))

  rect((2.75, 3), (5.25, 4), fill: rgb("#61ff9f"), name: "kanal1", radius: (
    south: 0.2,
  ))
  rect((6.75, 3), (9.25, 4), fill: rgb("#61ff9f"), name: "kanal2", radius: (
    south: 0.2,
  ))

  content("kanal1", [n+], anchor: "center", padding: 0.2)
  content("kanal2", [n+], anchor: "center", padding: 0.2)
  content("substrate", [Bulk], anchor: "north", padding: 0.2)


  rect((0, 4), (12, 4.5), fill: rgb("#fffc61"), name: "isolator")
  content("isolator.west", [Isolator ($"SiO"_2$)], anchor: "west", padding: .2)
})

#let FetModel3 = zap.circuit({
  import zap: *
  import cetz.draw: *

  rect(
    (0, 0),
    (12, 4),
    fill: rgb("#ffb1b1"),
    name: "p",
  )
  rect((0, -0.05), (12, -0.05), stroke: 3pt, name: "substrate")
  earth("g1", (11.5, 0))

  rect((2.75, 3), (5.25, 4), fill: rgb("#61ff9f"), name: "kanal1", radius: (
    south: 0.2,
  ))
  rect((6.75, 3), (9.25, 4), fill: rgb("#61ff9f"), name: "kanal2", radius: (
    south: 0.2,
  ))

  content("kanal1", [n+], anchor: "center", padding: 0.2)
  content("kanal2", [n+], anchor: "center", padding: 0.2)
  content("substrate", [Bulk], anchor: "north", padding: 0.2)


  rect((0, 4), (3, 4.5), fill: rgb("#fffc61"), name: "isolator2")
  rect((5, 4), (7, 4.5), fill: rgb("#fffc61"))
  rect((9, 4), (12, 4.5), fill: rgb("#fffc61"), name: "isolator")

  rect((3, 4), (5, 4.25), fill: gray)
  rect((7, 4), (9, 4.25), fill: gray)
  rect((5.1, 4.5), (6.9, 4.75), fill: gray)

  content("isolator", [$"SiO"_2$])
  content("isolator2", [Isolator])
})

#let FetModel(type: "N", s: 100%) = zap.circuit({
  import zap: *
  import cetz.draw: rect, content

  cetz.draw.scale(s)

  let pTypeFill = rgb("#ffb1b1");
  let pTypeFill = rgb("#ffb1b1");
  let nTypeFill = rgb("#61ff9f")

  rect(
    (0, 0),
    (12, 4),
    fill: if(type == "N") { pTypeFill } else { nTypeFill },
    name: "p",
  )
  rect((0, -0.05), (12, -0.05), stroke: 3pt, name: "substrate")
  earth("g1", (11.5, 0))

  wire((13, 5.5), (13, 0), mark: (end: ">"))
  node("n-r1", (13, 5.75))

  wire((6, 4.5), (6, 5.75))
  node("n-g1", (6, 5.75))
  wire("n-g1", "n-r1")

  node("n-s1", (4, 5))
  wire("n-s1", (4, 4.25))

  node("n-d1", (8, 5))
  wire("n-d1", (8, 4.25))

  rect((0, 4), (3, 4.5), fill: rgb("#fffc61"), name: "isolator2")
  rect((5, 4), (7, 4.5), fill: rgb("#fffc61"))
  rect((9, 4), (12, 4.5), fill: rgb("#fffc61"), name: "isolator")

  rect((3, 4), (5, 4.25), fill: gray)
  rect((7, 4), (9, 4.25), fill: gray)
  rect((5.1, 4.5), (6.9, 4.75), fill: gray)

  content("n-g1", [*G*\ate], anchor: "south", padding: 0.2, auto-scale: true)
  content("n-s1", [*S*\ource], anchor: "south", padding: 0.2)
  content("n-d1", [*D*\rain], anchor: "south", padding: 0.2, auto-scale: true)
  content("substrate", [Bulk], anchor: "north", padding: 0.2, auto-scale: true)
  content("p", if type == "N" [p] else [n], auto-scale: true)
  content("isolator", [$"SiO"_2$], auto-scale: true)

  content("isolator2", [Isolator], auto-scale: true)

  rect((2.75, 3), (5.25, 4), fill: if(type == "N") { nTypeFill }  else { pTypeFill }, name: "kanal1", radius: (
    south: 0.2,
  ))
  rect((6.75, 3), (9.25, 4), fill: if(type == "N") { nTypeFill } else { pTypeFill }, name: "kanal2", radius: (
    south: 0.2,
  ))

  content("kanal1", if type == "N" [n+] else [p+], anchor: "center", padding: 0.2, auto-scale: true)
  content("kanal2", if type == "N" [n+] else [p+], anchor: "center", padding: 0.2, auto-scale: true)
})

#let FetModelConducting = zap.circuit({
  import zap: *
  import cetz.draw: *

  rect(
    (0, 0),
    (12, 4),
    fill: rgb("#ffb1b1"),
    name: "p",
  )
  rect((0, -0.05), (12, -0.05), stroke: 3pt, name: "substrate")
  earth("g1", (11.5, 0))

  wire((13, 5.5), (13, 0), mark: (end: ">"))
  node("n-r1", (13, 5.75))

  wire((6, 4.5), (6, 5.75))
  node("n-g1", (6, 5.75))
  wire("n-g1", "n-r1")

  node("n-s1", (4, 5))
  wire("n-s1", (4, 4.25))

  node("n-d1", (8, 5))
  wire("n-d1", (8, 4.25))

  rect((0, 4), (3, 4.5), fill: rgb("#fffc61"), name: "isolator2")
  rect((5, 4), (7, 4.5), fill: rgb("#fffc61"))
  rect((9, 4), (12, 4.5), fill: rgb("#fffc61"), name: "isolator")

  rect((3, 4), (5, 4.25), fill: gray)
  rect((7, 4), (9, 4.25), fill: gray)
  rect((5.1, 4.5), (6.9, 4.75), fill: gray)

  content("n-g1", [*G*\ate], anchor: "south", padding: 0.2)
  content("n-s1", [*S*\ource], anchor: "south", padding: 0.2)
  content("n-d1", [*D*\rain], anchor: "south", padding: 0.2)
  content("substrate", [Bulk], anchor: "north", padding: 0.2)
  content("p", [p])
  content("isolator", [$"SiO"_2$])
  content("isolator2", [Isolator])

  rect((2.75, 3), (5.25, 4), fill: rgb("#61ff9f"), name: "kanal1", radius: (
    south: 0.2,
  ))
  rect((6.75, 3), (9.25, 4), fill: rgb("#61ff9f"), name: "kanal2", radius: (
    south: 0.2,
  ))
  rect((5.20, 3.99), (6.8, 3.9), fill: rgb("#61ff9f"), stroke: none)

  content("kanal1", [n+], anchor: "center", padding: 0.2)
  content("kanal2", [n+], anchor: "center", padding: 0.2)

  rect((0.5, -1), (1, -0.70), fill: gray, stroke: none, name: "metal")
  content("metal", [metal], anchor: "west", padding: 0.3)

  rect((0.5, -1.2), (1, -1.5), fill: rgb("#61ff9f"), stroke: none, name: "n")
  content("n", [n+], anchor: "west", padding: 0.3)

  rect(
    (2.5, -1),
    (3, -0.7),
    fill: rgb("#ffb1b1"),
    stroke: none,
    name: "p-substrate",
  )
  content("p-substrate", [p], anchor: "west", padding: 0.3)

  rect((2.5, -1.2), (3, -1.5), fill: rgb("#fffc61"), stroke: none, name: "siO2")
  content("siO2", [oxide], anchor: "west", padding: 0.3)
})

#let FetPlot() = {
  let u_gs = 1
  let beta = 1

  cetz.canvas({
    import cetz-plot: plot
    import cetz: draw.content

    cetz.draw.set-style(axes: (
      shared-zero: false,
      overshoot: 0.2,
      x: (mark: (end: ">", fill: black, scale: 0.6)),
      y: (mark: (end: ">", fill: black, scale: 0.6)),
    ))

    plot.plot(
      size: (2, 2),
      name: "plot",
      axis-style: "school-book",
      x-min: 0,
      x-tick-step: none,
      y-tick-step: none,
      x-label: $U_"GS"$,
      y-label: $U_"DS"$,
      {
        plot.add-fill-between(domain: (1, 6), ((1, 0), (1, 5)), u_gs => u_gs - u_t)

        plot.add(domain: (0, 5), fill: true, axes: ("y", "x"), _ => 1)

        plot.add(domain: (1, 6), fill: true, u_gs => u_gs - u_t)

        plot.add-anchor("I", (0.5, 2.5))
        plot.add-anchor("II", (4.5, 1.5))
        plot.add-anchor("III", (2.5, 3.5))

        plot.add-anchor("ut", (u_t, 0))
      }
    )
    content("plot.ut", $U_t$, anchor: "north", padding: 0.1)

    content("plot.I", [I])
    content("plot.II", [II])
    content("plot.III", [III])
  })
}