"""Option 2 EXIT, natural: the A's exit tail itself flows into the climb-out. No separate trail line."""
import math, numpy as np, skia
import compose as C, jetlib as J
from compose import hdir, taper, sample, plan_from_tail
U,D=J.U,J.D
E=np.array([799.48,415.0])                 # exact end of the approved exit stroke
T0=np.array([23.74,-12.61]); T0=T0/np.linalg.norm(T0)   # exact end tangent (28 deg climb)
H0=math.degrees(math.atan2(T0[0],-T0[1]))  # heading 62 deg (clockwise from up)
SW=30.0
def climb_h(c): return 90.0-c             # climb angle above horizon -> heading
def extension(length, c_end, k0=0.40, k1=0.35):
    h1=climb_h(c_end); dh=hdir(h1)
    # chord: average of start/end directions, so curvature decays naturally into a steady climb
    mid=(T0+dh); mid/=np.linalg.norm(mid); T=E+mid*length
    return [(tuple(E),tuple(E+T0*length*k0),tuple(T-dh*length*k1),tuple(T))], T, h1
def ease_taper(w_end, hold=0.0, p=1.0):
    """cosine taper: zero slope where it leaves the approved stroke (no cap bump, no corner),
    edges stay convex all the way (no feather/flame inflection)."""
    def w(u):
        x=max(0.0,(u-hold)/(1-hold))
        return w_end+(SW-w_end)*math.cos(x*math.pi/2)**p
    return w
def tip(segs):
    pts,tg,u=sample(segs); t=tg[-1]; return pts[-1], math.degrees(math.atan2(t[0],-t[1]))
def variant(k):
    if k=='a':   # stroke flows straight into the fuselage: the stroke becomes the aircraft
        segs,T,h=extension(118, 33); L=92
        ext=taper(segs, ease_taper(5.0, hold=0.0, p=1.0), knots=20)
        p,h=tip(segs); sc=L/9.42
        jet=J.place(J.plan('clean'), p[0], p[1], L, h, pivot=(0,8.05))   # stroke enters the rear fuselage between the V-tail roots
        return dict(ext=ext, jet=jet, title='Becomes', line='The exit stroke climbs out and narrows straight into the jet\'s tail cone: the stroke becomes the aircraft.')
    if k=='b':   # pen-lift: stroke tapers to nothing, jet lifts off just past it
        segs,T,h=extension(96, 33)
        ext=taper(segs, ease_taper(0.0, hold=0.0, p=1.0), knots=20)
        p,h=tip(segs)
        jet=plan_from_tail(p, h, 84, 9)
        return dict(ext=ext, jet=jet, title='Lift-off', line='The stroke thins to nothing like a lifted pen; the jet is airborne a breath beyond it, on the same line.')
    if k=='c':   # short curl, smaller jet
        segs,T,h=extension(58, 42, k0=0.42, k1=0.32)
        ext=taper(segs, ease_taper(0.0, hold=0.0, p=1.0), knots=20)
        p,h=tip(segs)
        jet=plan_from_tail(p, h, 72, 7)
        return dict(ext=ext, jet=jet, title='Curl', line='The tail curls up a touch and releases a smaller jet, already climbing.')
    if k=='d':   # profile: the stroke is the runway roll, rotation and climb; SF50 profile rides the tip
        segs,T,h=extension(104, 31)
        ext=taper(segs, ease_taper(2.8, hold=0.0, p=1.0), knots=20)
        p,h=tip(segs); climb=90-h; L=136
        jet=J.place(J.side('full'), p[0], p[1], L, climb, pivot=(9.30,-0.76), view='side')
        return dict(ext=ext, jet=jet, title='Climb-out', line='Seen in profile: ground roll, rotation and climb are one stroke, and the SF50 rides its tip, nose up along the tangent.')
