"""Lecture 36: Hull's business jet at Mach 0.6 with the CG at 0.30 of the mean chord: the pitching-moment coefficient
against angle of attack of the wing-body, the tail, the thrust and the whole aircraft, elevator at zero.
Writes img/fig-36-sbj.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK
import _stab as S
from flight import stability as ST

A = np.linspace(-4, 10, 100)
PARTS = [("wing-body", BLUE, dict(tail=False, thrust=False)), ("horizontal tail", GREEN, dict(wing=False, thrust=False)),
         ("thrust", AMBER, dict(wing=False, tail=False)), ("whole aircraft", MAROON, {})]
CM0, CMA, _ = ST.moment_coefficients(S.XCG, S.WING, S.TAIL, S.THRUST)
TRIM = np.degrees(ST.trim_angle(CM0, CMA))

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    for lab, c, kw in PARTS:
        ax.plot(A, S.cm_line(S.XCG, np.radians(A), **kw), color=c, lw=3 if lab == "whole aircraft" else 2.0, label=lab)
    ax.plot(TRIM, 0, "o", color=MAROON, ms=10)
    ax.annotate(f"trim, {TRIM:.1f}°", (TRIM, 0), textcoords="offset points", xytext=(12, 10), fontsize=11, color=MAROON)
    ax.axhline(0, color=INK, lw=0.9); ax.axvline(0, color=INK, lw=0.9)
    ax.set_xlabel("angle of attack (°)"); ax.set_ylabel("$C_m$"); ax.set_xlim(-4, 10)
    ax.legend(frameon=False, fontsize=11, ncol=2, loc="lower left")
    despine(ax)
    save(fig, "fig-36-sbj")
