"""Go Around wordmark generator.
Capitals G and A are the approved v2-refined GA letterforms, loaded verbatim from
v2-refined/svg/ga-a-stroke.svg (segments 0-6 = G, 6-12 = A, plus the crossbar).
Lowercase is new: Hobby splines + exact ellipse arcs, monoline, lighter weight.
Design space is unsheared; output = 9 deg shear + offset, identical to the monogram."""
import math, copy, numpy as np, skia
import base as B, runs as R, geo, low3
BL=490; W1=30
BASE=dict(xh=132, lw=22, dxa=420, go_th=228, go_rx=66,
          gbar=(0.10,0.56), abar=(0.60,0.42), liftoff=True, tip=1.0,
          S=dict(uw=112, nw=112, orx=68, drx=66, flag=44, og=50, ug=42, ng=62, dg=44,
                 dtop=190, r=16, dloop=True, swash=(190,-120)))
def _bar(seg,a,b_): return [B.split(B.split(seg,b_)[0],a/b_)[1]]
def build(p):
    """returns list of strokes: dict(id, runs) in writing order"""
    XT=BL-p['xh']; W2=p['lw']; xh=p['xh']; cy=(XT+BL)/2; DX=p['dxa']
    G=B.GA[:7]; ng=len(G)
    A=B.mv(B.GA[6:13],DX); na=len(A)
    # ---- G + o
    pen=geo.Pen(G)
    rx=p['go_rx']; ry=xh/2; th=p['go_th']
    d=B.end_dir(G); e=G[-1][3]
    L=(e[1]-cy-ry*math.sin(math.radians(th)))/(-d[1])
    j=(e[0]+d[0]*L, e[1]+d[1]*L); ocx=j[0]-rx*math.cos(math.radians(th))
    strokes=[]
    cs=B.mv(B.CROSS,DX); a0,a1=p['abar']
    if p.get('loop'):
        # G ends with its rising sweep; the o becomes a loop in one horizon line:
        # G bar -> o (full clockwise loop from 12 o'clock) -> A crossbar
        gp=geo.Pen(G); gp.to(p['g_tail'](e,d)); gs=gp.segs
        strokes.append(dict(id='g', runs=[(G,W1,W1),(gs[ng:],W1,p.get('g_tip',14),'late')]))
        sx,sy=p['loop_start']; ot=(ocx,cy-ry)
        b0=B.hpath([(sx,sy),(ot[0],ot[1],(1,0))])
        lp=geo.Pen(b0); n0=len(lp.segs)
        lp.ellipse(ocx,cy,rx,ry,270,270+360); n1=len(lp.segs)
        bar=[cs[0]]+([B.split(cs[1],a1)[0]] if a1>0 else [])
        c0=bar[0][0]; lp.to([(c0[0],c0[1],B.start_dir(bar))]); n2=len(lp.segs); ls=lp.segs
        strokes.append(dict(id='horizon-loop', runs=[(ls[:n0],W1,W1),(ls[n0:n0+1],W1,W2),(ls[n0+1:n1],W2,W2),(ls[n1:n2],W2,W1-4),(bar,W1-4,W1)]))
        go_runs=None
    elif p.get('linked'):
        # the o leaves at 12 o'clock and runs straight into the A's crossbar (verbatim crossbar geometry)
        pen.ellipse(ocx,cy,rx,ry,th,360+p.get('o_exit',270))
        n1=len(pen.segs)
        bar=[cs[0]]+([B.split(cs[1],a1)[0]] if a1>0 else [])
        c0=bar[0][0]
        pen.to(p.get('link_via',[])+[(c0[0],c0[1],B.start_dir(bar))]); n2=len(pen.segs)
        s=pen.segs
        go_runs=[(G,W1,W1),(s[ng:ng+1],W1,W2),(s[ng+1:n1],W2,W2),(s[n1:n2],W2,W1-4),(bar,W1-4,W1)]
    else:
        pen.ellipse(ocx,cy,rx,ry,th,th+360+40)
        s=pen.segs; go_runs=[(G,W1,W1),(s[ng:ng+1],W1,W2),(s[ng+1:],W2,W2)]
    # ---- A + round
    pen=geo.Pen(A); low3.lower(pen,870+DX,XT,p['S'])
    s=pen.segs; tail=1 if p.get('liftoff') else 0
    a_runs=[(A[:-1],W1,W1),([A[-1]]+s[na:na+1],W1,W2),(s[na+1:len(s)-tail],W2,W2)]
    if tail: a_runs.append((s[len(s)-tail:],W2,p['tip'],'late'))
    if go_runs: strokes.append(dict(id='go-o-crossbar' if p.get('linked') else 'go', runs=go_runs))
    if not p.get('loop'):
        gb=_bar(B.CROSS[0],*p['gbar'])
        strokes.append(dict(id='g-bar', runs=[(gb,W1,W1)]))
    strokes.append(dict(id='around', runs=a_runs))
    if p.get('loop'):
        # G ends with its rising sweep; the o becomes a loop in one horizon line:
        # G bar -> o (full clockwise loop from 12 o'clock) -> A crossbar
        gp=geo.Pen(G); gp.to(p['g_tail'](e,d)); gs=gp.segs
        strokes.append(dict(id='g', runs=[(G,W1,W1),(gs[ng:],W1,p.get('g_tip',14),'late')]))
        sx,sy=p['loop_start']; ot=(ocx,cy-ry)
        b0=B.hpath([(sx,sy),(ot[0],ot[1],(1,0))])
        lp=geo.Pen(b0); n0=len(lp.segs)
        lp.ellipse(ocx,cy,rx,ry,270,270+360); n1=len(lp.segs)
        bar=[cs[0]]+([B.split(cs[1],a1)[0]] if a1>0 else [])
        c0=bar[0][0]; lp.to([(c0[0],c0[1],B.start_dir(bar))]); n2=len(lp.segs); ls=lp.segs
        strokes.append(dict(id='horizon-loop', runs=[(ls[:n0],W1,W1),(ls[n0:n0+1],W1,W2),(ls[n0+1:n1],W2,W2),(ls[n1:n2],W2,W1-4),(bar,W1-4,W1)]))
        go_runs=None
    elif p.get('linked'): return strokes
    # ---- A crossbar
    abar=[B.split(cs[0],a0)[1]]+([B.split(cs[1],a1)[0]] if a1>0 else [])
    if p.get('horizon'):
        hp=geo.Pen(abar); n0=len(hp.segs); hp.to(p['horizon'](DX,XT)); hs=hp.segs
        strokes.append(dict(id='a-crossbar', runs=[(hs[:n0],W1,W1),(hs[n0:],W1,p['tip'],'late')]))
    else:
        strokes.append(dict(id='a-crossbar', runs=[(abar,W1,W1)]))
    return strokes
def all_runs(strokes): return [r for s in strokes for r in s['runs']]
def variant(name):
    p=copy.deepcopy(BASE)
    if name=='signature': pass
    elif name=='linked':
        p['linked']=True
    elif name=='loop':
        p['loop']=True; p['loop_start']=(262,371)
        p['g_tail']=lambda e,d: [(e[0]+d[0]*30, e[1]+d[1]*30)]
    elif name=='horizon':
        p['liftoff']=False; p['S']['swash']=(70,-46); p['abar']=(0.60,1.0)
        p['horizon']=lambda DX,XT: [(818+DX+330, 262,(1,-0.16))]
    return p
VARIANTS=['signature','linked','loop']
