"""Lambert: the eccentricity of the conic through two points, as a function of p, and the admissible windows."""
import numpy as np
import matplotlib.pyplot as plt
from _manstyle import CREAM, INK, MAROON, BLUE, GREEN, AMBER, PURPLE, despine
from _lambertp import conic_through, p_window

r1, r2, dth = 7000.0, 11000.0, np.radians(100.0)          # the same e(p) serves dtheta = 100 and 260 degrees
k, l = r1*r2*(1 - np.cos(dth)), r1 + r2
c2 = r1**2 + r2**2 - 2*r1*r2*np.cos(dth)
pi_, pii = p_window(r1, r2, dth)
pe, emin = k*l/c2, abs(r2 - r1)/np.sqrt(c2)
ps = np.linspace(0.02*pi_, 1.9*pii, 3000)
es = np.array([conic_through(q, r1, r2, dth)[0] for q in ps])

fig, ax = plt.subplots(figsize=(10.6, 5.6))
ax.axvspan(pi_, pii, color=BLUE, alpha=0.10, lw=0)
ax.plot(ps, es, color=INK, lw=2.8, zorder=4)
ax.axhline(1, color=AMBER, lw=1.4, ls=(0, (5, 3)))
ax.text(1.9*pii, 1.04, r"$e=1$", color=AMBER, fontsize=12, ha="right")
for q, lab in ((pi_, r"$p_{\rm i}$"), (pii, r"$p_{\rm ii}$")):
    ax.plot([q, q], [0, 1], color=AMBER, lw=1.4, ls=(0, (5, 3)))
    ax.plot(q, 1, "o", color=AMBER, ms=9, zorder=6)
    ax.text(q, -0.19, lab, color=AMBER, fontsize=13, ha="center")
ax.plot(pe, emin, "o", color=MAROON, ms=9, zorder=6)
ax.annotate(r"minimum eccentricity $e_{\min}=|r_2-r_1|/c$" + f"\nat $p=k\\ell/c^2$,  $e_{{\\min}}={emin:.3f}$",
            xy=(pe, emin), xytext=(1.08*pii, 0.30), fontsize=11.5, color=MAROON,
            arrowprops=dict(arrowstyle="-", color=MAROON, lw=1.0))
ax.text(0.5*(pi_ + pii), 1.78, "ellipses\n$e<1$", ha="center", fontsize=12, color=BLUE)
ax.text(0.45*pi_, 2.1, "hyperbolas", ha="center", fontsize=12, color=MAROON)
ax.text(1.62*pii, 1.45, "hyperbolas", ha="center", fontsize=12, color=MAROON)

# admissible windows, drawn as bars under the axis
y1, y2 = -0.52, -0.84
ax.annotate("", xy=(1.9*pii, y1), xytext=(pi_, y1), arrowprops=dict(arrowstyle="-|>", color=GREEN, lw=3.2), annotation_clip=False)
ax.plot(pi_, y1, "o", mfc=CREAM, mec=GREEN, mew=2.2, ms=10, clip_on=False, zorder=7)       # p_i: dt -> infinity
ax.plot(pii, y1, "o", color=GREEN, ms=10, clip_on=False, zorder=7)                         # p_ii: parabola, finite dt
ax.text(1.9*pii, y1 + 0.16, r"short way ($\Delta\theta<\pi$): $p>p_{\rm i}$   (open: $\Delta t\to\infty$;  filled: parabola, finite $\Delta t$)", color=GREEN, fontsize=11.5, va="center", ha="right", clip_on=False)
ax.annotate("", xy=(0.02*pi_, y2), xytext=(pii, y2), arrowprops=dict(arrowstyle="-|>", color=PURPLE, lw=3.2), annotation_clip=False)
ax.plot(pii, y2, "o", mfc=CREAM, mec=PURPLE, mew=2.2, ms=10, clip_on=False, zorder=7)      # p_ii: dt -> infinity
ax.plot(pi_, y2, "o", color=PURPLE, ms=10, clip_on=False, zorder=7)                        # p_i: parabola, finite dt
ax.text(1.9*pii, y2 + 0.16, r"long way ($\Delta\theta>\pi$): $p<p_{\rm ii}$", color=PURPLE, fontsize=11.5, va="center", ha="right", clip_on=False)

ax.set_xlim(0, 1.9*pii); ax.set_ylim(0, 2.8)
ax.set_xlabel(r"semi-latus rectum $p$  [km]", labelpad=88)
ax.set_ylabel(r"eccentricity $e$ of the conic through $\mathbf{r}_1$ and $\mathbf{r}_2$")
ax.set_title(r"One curve $e(p)$ serves both ways round ($\Delta\theta$ and $2\pi-\Delta\theta$); the admissible window differs", fontsize=12)
despine(ax)
fig.tight_layout()
fig.savefig("img/fig-lam-window.png", dpi=160, bbox_inches="tight")
print(f"wrote img/fig-lam-window.png   (p_i={pi_:.1f}, p_ii={pii:.1f}, p_e={pe:.1f}, e_min={emin:.4f})")
