from drawing_core import *
P=json.loads((ROOT.parent/'02_CAD_and_Parameters'/'design_parameters.json').read_text(encoding='utf-8'))

def smooth_profile(points):
    p=sorted(points);n=len(p);sl=[(p[i+1][1]-p[i][1])/(p[i+1][0]-p[i][0]) for i in range(n-1)]
    m=[0]+[0 if sl[i-1]*sl[i]<=0 else 2*sl[i-1]*sl[i]/(sl[i-1]+sl[i]) for i in range(1,n-1)]+[sl[-1]]
    out=[]
    for i in range(n-1):
        x0,y0=p[i];x1,y1=p[i+1];h=x1-x0
        for j in range(12):
            t=j/12;z=(2*t**3-3*t*t+1)*y0+(t**3-2*t*t+t)*h*m[i]+(-2*t**3+3*t*t)*y1+(t**3-t*t)*h*m[i+1]
            out.append((x0+t*h,z))
    return out+[p[-1]]

br=[(0,75)]+smooth_profile([(90,75),(120,77),(140,87),(154,105),(158,122),(162,150),(163.5,153)])
BOWL=[(-r,z) for r,z in br[::-1]]+br[1:]
LR=smooth_profile(P['lid_profile_rz'])+[(166,155)]
LID=[(-r,z) for r,z in LR[::-1]]+LR[1:]
LI=[]
for i,(r,z) in enumerate(LR):
    a=LR[max(0,i-1)];b=LR[min(len(LR)-1,i+1)];dr=b[0]-a[0];dz=b[1]-a[1];le=math.hypot(dr,dz)
    LI.append((max(0,r+.7*dz/le),z-.7*dr/le))
LID_IN=[(-r,z) for r,z in LI[::-1]]+LI[1:]
def z_at(points,r):
    for (x1,z1),(x2,z2) in zip(points,points[1:]):
        if x1<=r<=x2:return z1+(z2-z1)*(r-x1)/(x2-x1)
    raise ValueError(r)
CENTER=z_at(LI,0)-150;EDGE=z_at(LI,140)-150
PACK_BASE=P['stow_drop']

def front(v,section=False,fold=False,carry=False,food=False):
    v.poly(BOWL,'CUT' if section else 'OUTLINE');v.poly(LID,'CUT' if section else 'OUTLINE')
    v.line((-166,155),(166,155),'THIN')
    v.rect(-50,53,100,22,'THIN');v.line((-100,45),(100,45),'THIN')
    for x in [-80,80]:v.line((x,75),(x,45),'THIN')
    v.rect(-8,50,16,4,'THIN')
    if not section:
        for x in [-120,120]:v.rect(x-6,151,12,8,'THIN')
    z=P['carry_rail_Z'] if carry else P['stowed_rail_Z'] if fold else 7
    for halfspan in [170,178]:
        v.rod([(-halfspan,112),(-halfspan,z),(halfspan,z),(halfspan,112)],6)
    v.rect(-40,z-7,80,14,'THIN')
    for x in [-115,115]:v.rect(x-10,z-7,20,14,'THIN')
    for sg in [-1,1]:
        v.rect(sg*170-13,97,26,31,'THIN');v.circle((sg*170,112),2.5,'THIN')
    v.cl(0,0,0,247 if not carry else 310)
    if section:
        v.poly(LID_IN,'THIN')
        v.line((-152.5,150),(152.5,150),'CUT');v.line((-110,95),(110,95),'CUT')
        for sg in [-1,1]:v.rod([(sg*100,92.5),(sg*75,76.5)],3)
        for x in [-70,-40,-10,20,50]:v.rect(x,96,23,25,'THIN')
        v.arrow((-25,63),(-25,88));v.arrow((25,103),(25,138));v.arrow((-105,228),(-110,251))
    if food:v.rect(-140,150,280,50,'HIDDEN')

def side(v):
    v.poly(BOWL);v.poly(LID);v.line((-166,155),(166,155),'THIN')
    v.rect(-50,53,100,22,'THIN');v.line((-100,45),(100,45),'THIN')
    for sg in [-1,1]:
        v.rod([(0,112),(sg*148,7)],6);v.circle((sg*148,7),7,'THIN')
    v.circle((0,112),5,'THIN')
    v.cl(0,0,0,250)

def top(v):
    v.circle((0,0),166);v.circle((0,0),165,'THIN')
    v.cl(-185,0,185,0);v.cl(0,-185,0,185)
    # Tangent-plane vent windows projected schematically in this top view.
    v.circle((-105,-50),27.5,'THIN')
    for a in [0,120,240]:v.arcslot((-105,-50),18,a,a+70,6)
    for sg,h in [(1,170),(-1,178)]:
        v.poly([(-h,0),(-h,sg*148),(h,sg*148),(h,0)],'THIN')
        v.rect(-40,sg*148-7,80,14,'THIN')
        for x in [-115,115]:v.rect(x-10,sg*148-7,20,14,'THIN')

def grate(v,D,pitch,wire,ring):
    v.circle((0,0),D/2);v.circle((0,0),D/2-ring,'THIN');r=D/2-ring
    n=int((r-wire)/pitch)
    for i in range(-n,n+1):
        x=i*pitch;h=math.sqrt(r*r-x*x);v.line((x,-h),(x,h),'THIN')
    for y in [-65,65]:v.line((-math.sqrt(r*r-y*y),y),(math.sqrt(r*r-y*y),y),'THIN')
    v.cl(-D*.56,0,D*.56,0);v.cl(0,-D*.56,0,D*.56)

def table(x,y,widths,headers,rows,rh=8,size=2.5):
    for i,row in enumerate([headers]+rows):
        xx=x;yy=y+i*rh
        for w,t in zip(widths,row):
            box(xx,yy,w,rh,LIGHT if i==0 else None)
            tx(xx+2,yy+rh*.66,str(t),size,'B' if i==0 else 'CN');xx+=w

