"""Lecture 36: schematic of what the tail feels when the aircraft's angle of attack rises by 2 degrees. The downwash
grows too, so the tail's angle of attack rises by (1 - eps_alpha) x 2 degrees (flight.stability.tail_alpha, Hull's
business jet). Angles are drawn exaggerated. Writes img/fig-36-downwash.png."""
import numpy as np
from _visual import plt, save, MAROON, BLUE, GREEN, INK, GREY
import _stab as S
from flight import stability as ST
from flight.constants import DEG

D_ALPHA = 2.0
D_TAIL = float((ST.tail_alpha(D_ALPHA * DEG, S.TAIL) - ST.tail_alpha(0.0, S.TAIL)) / DEG)
D_EPS = D_ALPHA - D_TAIL

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10.5, 3.9))
    for x0, c in ((0.0, "wing"), (6.2, "tail")):
        ax.plot([x0 - 0.55, x0 + 0.55], [0.12, -0.12], color=INK, lw=7, solid_capstyle="round")
        ax.text(x0, -1.35, c, ha="center", fontsize=13)

    def arrow(x, y, ang_deg, color, length=1.9, lw=2.4, ls="-"):
        a = np.radians(ang_deg)
        ax.annotate("", xy=(x, y), xytext=(x - length * np.cos(a), y - length * np.sin(a)),
                    arrowprops=dict(arrowstyle="-|>", color=color, lw=lw, ls=ls))
    EX = 7.0                                                    # drawing exaggeration of the angles
    arrow(-0.65, 0.02, 0, GREY); arrow(-0.65, 0.02, D_ALPHA * EX, MAROON)
    arrow(5.55, 0.02, 0, GREY); arrow(5.55, 0.02, D_TAIL * EX, MAROON); arrow(5.55, 0.02, D_ALPHA * EX, GREY, lw=1.4, ls="--")
    ax.text(-1.6, 0.85, f"at the wing the flow turns\nby the whole {D_ALPHA:.0f}°", ha="center", fontsize=12, color=MAROON)
    ax.text(3.9, 0.95, f"at the tail only {D_TAIL:.2f}°,\nsince the downwash\nhas grown by {D_EPS:.2f}°", ha="center", fontsize=12, color=MAROON)
    ax.text(-1.7, -0.85, "grey: the air before the change", fontsize=11, color=GREY)
    ax.text(3.7, -0.75, "dashed: the 2° it would feel\nwithout the downwash", fontsize=11, color=GREY, ha="center", va="top")
    ax.text(2.2, -1.85, "angles drawn seven times too large; arrows show the direction of the oncoming air", ha="center", fontsize=11)
    ax.set_xlim(-3.0, 7.2); ax.set_ylim(-2.05, 1.75); ax.axis("off")
    save(fig, "fig-36-downwash")
