"""Lecture 14: block diagram of a two-spool turbofan with separate exhaust streams: the order of the parts, the core and
bypass air paths, and which turbine drives which compressor. No numbers. Writes img/fig-14-turbofan-schematic.png."""
from matplotlib.patches import Polygon, Rectangle
from _visual import plt, save, MAROON, BLUE, GREEN, INK, GREY

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10.8, 5.2))

    def block(x, w, h0, h1, label, c="#e6e1d6", y=0.0, ty=None):
        ax.add_patch(Polygon([(x, y + h0 / 2), (x + w, y + h1 / 2), (x + w, y - h1 / 2), (x, y - h0 / 2)], closed=True, facecolor=c, edgecolor=INK, lw=1.8))
        ax.text(x + w / 2 + (0.22 if label.startswith("low-pressure\ncomp") else 0.0), ty if ty is not None else y - max(h0, h1) / 2 - 0.18, label, ha="center", va="top", fontsize=11)

    block(0.0, 0.8, 2.2, 2.0, "inlet", ty=-1.45)
    block(1.0, 0.6, 2.9, 2.5, "fan", "#d9e4ee", ty=-1.65)
    block(2.0, 0.9, 1.3, 1.0, "low-pressure\ncompressor", ty=-0.9)
    block(3.2, 0.9, 1.0, 0.75, "high-pressure\ncompressor", ty=-1.55)
    block(4.45, 1.1, 0.85, 0.85, "combustor", "#f3e3c3", ty=-0.9)
    block(5.9, 0.75, 0.75, 1.0, "high-pressure\nturbine", ty=-1.55)
    block(6.95, 0.9, 1.0, 1.35, "low-pressure\nturbine", ty=-0.9)
    block(8.2, 1.2, 1.3, 0.8, "core\nnozzle", ty=-1.5)
    for x0, x1, y, c, lab in ((3.65, 6.27, 0.95, GREEN, "high-pressure shaft"), (1.3, 7.4, 1.85, MAROON, "low-pressure shaft: fan and low-pressure compressor")):
        ax.plot([x0, x0, x1, x1], [y - (0.4 if y > 1.5 else 0.45), y, y, y - (1.15 if y > 1.5 else 0.45)], color=c, lw=2.2)
        ax.text((x0 + x1) / 2, y + 0.05, lab, ha="center", va="bottom", fontsize=10.5, color=c)
    ax.annotate("", xy=(9.3, 2.55), xytext=(1.65, 2.55), arrowprops=dict(arrowstyle="-|>", color=BLUE, lw=2.6))
    ax.plot([1.65, 1.65], [1.15, 2.55], color=BLUE, lw=2.6)
    ax.text(5.4, 2.65, "bypass air: through the fan only, then its own nozzle", ha="center", va="bottom", fontsize=12, color=BLUE)
    ax.annotate("", xy=(10.4, 0), xytext=(9.45, 0), arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=3)); ax.text(9.95, 0.15, "hot jet", ha="center", fontsize=11.5, color=MAROON)
    ax.annotate("", xy=(-0.05, 0), xytext=(-0.9, 0), arrowprops=dict(arrowstyle="-|>", color=INK, lw=3)); ax.text(-0.5, 0.15, "air", ha="center", fontsize=12)
    ax.text(5.0, -2.85, "core air: through every block along the centre", ha="center", fontsize=11.5)
    ax.set_xlim(-1.0, 10.6); ax.set_ylim(-3.0, 3.1); ax.axis("off")
    save(fig, "fig-14-turbofan-schematic")
