"""Lecture 38, one ruler of CG positions for Hull's business jet (_stab): the forward limit for C_L = 1.2 with 20 deg up
elevator, Hull's CG range, the stick-free and stick-fixed neutral points, and the stick-fixed manoeuvre point at
30,000 ft and at sea level (flight.stability), in chords aft of the leading edge of the mean chord.
Writes img/fig-38-cg-ruler.png."""
import numpy as np
from matplotlib.patches import Rectangle
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, INK, GREY
import _stab as S
from flight import stability as ST

FREE = ST.free_tail(S.TAIL, S.HINGE)
X_FWD = ST.forward_cg_limit(1.2, S.DE_MIN, S.WING, S.TAIL, S.THRUST)
RANGE = S.CG_RANGE
X_NP_FREE = ST.neutral_point(S.WING, FREE)
X_NP = ST.neutral_point(S.WING, S.TAIL)
KAPPA = [ST.mass_parameter(r, S.AREA, S.MAC_M, S.MASS) for r in (S.RHO, S.RHO_SL)]
X_MP, X_MP_SL = (ST.manoeuvre_point(S.WING, S.TAIL, k) for k in KAPPA)
CM_PER_CHORD = S.MAC_M * 100

if __name__ == "__main__":
    fig, (a, z) = plt.subplots(2, 1, figsize=(10.5, 6.2), gridspec_kw=dict(height_ratios=[1, 1.45], hspace=0.05))
    for ax, lim in ((a, (0.08, 0.60)), (z, (0.435, 0.565))):
        ax.plot(lim, [0, 0], color=INK, lw=1.2, zorder=0)
        ax.set_xlim(*lim); ax.axis("off")
    a.add_patch(Rectangle((RANGE[0], -0.14), RANGE[1] - RANGE[0], 0.28, facecolor=BLUE, alpha=0.2, edgecolor=BLUE))
    a.text(np.mean(RANGE), 0.22, f"Hull's CG range, {RANGE[0]} to {RANGE[1]}", ha="center", va="bottom", fontsize=12, color=BLUE)
    a.plot([X_FWD] * 2, [-0.18, 0.18], color=MAROON, lw=3)
    a.text(X_FWD, 0.55, f"forward limit {X_FWD:.3f}\n(elevator at its stop)", ha="center", va="bottom", fontsize=11.5, color=MAROON)
    a.plot([X_FWD, X_FWD], [0.18, 0.53], color=MAROON, lw=1)
    a.add_patch(Rectangle((0.435, -0.2), 0.13, 0.4, facecolor="none", edgecolor=GREY, lw=1.2, ls="--"))
    a.text(0.50, 0.28, "enlarged below", ha="center", va="bottom", fontsize=11, color=GREY)
    for x in np.arange(0.1, 0.59, 0.1):
        a.text(x, -0.32, f"{x:.1f}", ha="center", va="top", fontsize=10.5, color=GREY)
        a.plot([x, x], [-0.05, 0.05], color=GREY, lw=1)
    a.set_ylim(-0.75, 1.25)
    marks = [(X_NP_FREE, AMBER, "neutral point,\nstick free", 0.55),
             (X_NP, GREEN, "neutral point,\nstick fixed", -0.55),
             (X_MP, INK, "manoeuvre point,\n30,000 ft", 0.72),
             (X_MP_SL, MAROON, "manoeuvre point,\nsea level", -0.55)]
    for x, c, lab, y in marks:
        z.plot([x, x], [-0.14, 0.14], color=c, lw=3.5)
        z.plot([x, x], [0.14 * np.sign(y), y * 0.9], color=c, lw=1)
        z.text(x, y, f"{lab}\n{x:.3f}", ha="center", va="bottom" if y > 0 else "top", fontsize=11.5, color=c)
    z.annotate("", xy=(X_MP, 0.27), xytext=(X_NP, 0.27), arrowprops=dict(arrowstyle="<->", color=INK, lw=1.2))
    z.text((X_NP + X_MP) / 2, 0.33, f"{(X_MP - X_NP) * CM_PER_CHORD:.1f} cm", ha="center", va="bottom", fontsize=11, color=INK)
    z.text(0.437, -1.85, "positions in chords aft of the leading edge of the mean chord (1 chord = 213 cm)", fontsize=11, color=GREY)
    z.set_ylim(-1.95, 1.45)
    save(fig, "fig-38-cg-ruler")
