"""A concise, visual brand guide assembled from the production vector master."""
from graphics import *
from identity import recolor,patch,emblem_logo
from applications import fit

W,H=1440,1018

def build_guide(f,mark,pmark,logos,apps):
    pages=[]
    def page(number,title,sub='',dark=False):
        s=Scene(W,H,title='Stand The Fight | '+title);s.rect(0,0,W,H,INK if dark else PAPER)
        fg=WHITE if dark else INK;muted='#BEB9B1' if dark else MUTED
        f['medium'].text(s,'STAND THE FIGHT / BRAND GUIDE',72,42,12,tracking=.035,fill=fg)
        f['body'].text(s,f'{number:02d} / 13',1368,42,12,fill=muted,align='right')
        if title:f['display'].text(s,title.upper(),72,105,60,fill=fg)
        if sub:f['body'].text(s,sub,72,181,15,fill=muted)
        s.line(72,955,1368,955,'#3B3834' if dark else RULE,1)
        f['light'].text(s,'TRADITIONAL JAPANESE JIU-JITSU',72,976,10,tracking=.06,fill=fg)
        f['body'].text(s,'IDENTITY SYSTEM 1.2 / REVIEW EDITION',1368,976,10,fill=muted,align='right')
        pages.append(s);return s
    def label(s,text,x,y,colour=INK):f['medium'].text(s,text,x,y,13,tracking=.03,fill=colour)
    def body(s,text,x,y,w=370,size=18,leading=29,colour=INK):return f['body'].paragraph(s,text,x,y,w,size,leading,colour)

    # 01
    s=page(1,'',dark=True)
    fit(s,logos['primary'],52,115,900,790,'reverse')
    f['display'].text(s,'IDENTITY',1000,259,66,fill=WHITE)
    f['display'].text(s,'SYSTEM',1000,341,66,fill=WHITE)
    s.rect(1003,451,64,6,RED)
    label(s,'VERSION 1.2',1003,500,WHITE)
    body(s,'A controlled refinement of the selected L2 direction. Logos, type, colour and practical assets for digital, print and clothing.',1003,552,330,19,31,'#D8D3CB')
    label(s,'FOR REVIEW / NOVEMBER 2026',1003,795,WHITE)

    # 02
    s=page(2,'The refined identity','One master, with deliberate overlap, readable movement and a grounded Japanese column.')
    fit(s,logos['primary'],42,215,850,725)
    for y,n,title,copy in [
        (250,'01','CONTROLLED MOVEMENT','The lean stance and defined feet hold the throw in balance. Large open shapes keep the movement readable.'),
        (452,'02','A SHARED EDGE','The body curve overlaps the S through a real negative-space cut. There is no white background patch in the artwork.'),
        (654,'03','A QUIETER SECOND TIER','Light uppercase English and a fixed-height katakana column support the name. The column ends level with the descriptor.')]:
        f['display'].text(s,n,965,y,31,fill=RED);label(s,title,1022,y+4)
        body(s,copy,965,y+63,389,18,28)
    label(s,'PRIMARY LOGO / COLOUR',100,907)

    # 03
    s=page(3,'A useful logo family','Choose the level of detail to suit the space. Keep the approved proportions intact.')
    cells=[('primary','PRIMARY / BILINGUAL',72,226),('english','ENGLISH / FULL',758,226),('compact','COMPACT / NAME',72,594),('emblem','EMBLEM / SMALL SPACE',758,594)]
    for name,title,x,y in cells:
        s.rect(x,y,610,320,WHITE)
        logo=logos[name];scale=min(530/logo.w,278/logo.h)
        s.add(logo,x+(610-logo.w*scale)/2,y+6,scale)
        label(s,title,x+24,y+292)

    # 04
    s=page(4,'Colour, with restraint','One red head is the focal point. The same artwork also works in a single ink.')
    swatches=[('INK',INK,'RGB 17 / 17 / 17','PRINT K100',72),('STF RED',RED,'RGB 200 / 29 / 37','CMYK APPROX. 0 / 86 / 82 / 22',513),('PAPER',PAPER,'RGB 244 / 241 / 235','OPTIONAL BACKGROUND',954)]
    for name,col,rgb,cmyk,x in swatches:
        s.rect(x,237,414,195,col,stroke=RULE if col==PAPER else None)
        fg=INK if col==PAPER else WHITE
        label(s,name,x+24,262,fg);f['display'].text(s,col.upper(),x+24,314,42,fill=fg)
        f['body'].text(s,rgb,x,458,13);f['body'].text(s,cmyk,x,485,11,fill=MUTED)
    for x,kind,bg,title in [(72,'colour',WHITE,'COLOUR / LIGHT'),(513,'black',WHITE,'ONE INK'),(954,'reverse',INK,'REVERSE / DARK')]:
        s.rect(x,552,414,304,bg);fit(s,logos['compact'],x+55,550,305,270,kind)
        label(s,title,x+24,824,WHITE if bg==INK else INK)
    body(s,'Use white or reverse files on dark backgrounds. Paper is a supporting surface. On the bilingual patch, Japanese lettering may use STF red. The CMYK red is a starting conversion; match the actual stock or thread with the supplier.',72,880,1280,15,24)

    # 05
    s=page(5,'Space and size','Protect the silhouette and the quiet descriptor. Switch versions before detail becomes too small.')
    fit(s,logos['primary'],40,221,760,650)
    # Artwork bounds and a representative half-head clear-space measure.
    scale=760/logos['primary'].w
    bx=40+80*scale;by=221+80*scale;bw=(995-80)*scale;bh=(826-80)*scale
    cs=56*scale;s.rect(bx-cs,by-cs,bw+2*cs,bh+2*cs,None,stroke=RED,sw=1.3)
    label(s,'KEEP AT LEAST HALF A HEAD DIAMETER CLEAR',72,879)
    body(s,'Measure from the visible artwork, including the Japanese column. The exported masters already include a clear margin.',72,910,710,15,22)
    label(s,'STARTING MINIMUMS',905,251)
    rows=[('FULL BILINGUAL','420 px / 100 mm wide'),('ENGLISH FULL','340 px / 80 mm wide'),('COMPACT NAME','180 px / 45 mm wide'),('EMBLEM','64 px / 18 mm wide')]
    for k,(a,b) in enumerate(rows):
        y=307+k*112;label(s,a,905,y);f['body'].text(s,b,905,y+35,20);s.line(905,y+78,1368,y+78,RULE)
    body(s,'These are starting limits for clean reproduction. For an avatar or favicon, use the dedicated icon files. For embroidery, use the open-gap patch artwork at the supplied size.',905,788,451,17,27)

    # 06
    s=page(6,'Type that knows its job','Heavy condensed names. Light, open supporting text. Correct Japanese glyphs.')
    f['display'].text(s,'STAND THE FIGHT',72,244,118,width=1296)
    label(s,'ANTON / DISPLAY AND WORDMARK BASIS',72,401)
    body(s,'Use Anton for short display headlines. The logo is a fixed outlined composition with adjusted proportions and a custom overlap; always place the supplied artwork.',72,447,610,18,28)
    f['light'].text(s,'TRADITIONAL JAPANESE',760,444,25,tracking=.12)
    f['light'].text(s,'JIU-JITSU',760,495,25,tracking=.2)
    label(s,'EPILOGUE / LIGHT 300',760,565)
    body(s,'Regular 400 for body copy. Semibold 600 for labels and useful information. Keep tracking natural in sentences; reserve wider spacing for short uppercase labels.',760,610,598,18,28)
    f['jp'].text(s,'スタンド・ザ・ファイト',72,717,41,width=610)
    label(s,'NOTO SANS JP / REGULAR 400',72,797)
    body(s,'The original, unmodified font files and their Open Font Licences are included. Logo files are outlined and need no installed fonts. Editable templates require the supplied fonts.',72,852,1278,16,26)

    # 07
    s=page(7,'Japanese, with a purpose','The English wording remains fully readable. Japanese is a secondary rendering of the academy name.')
    fit(s,logos['primary'],50,210,755,650)
    label(s,'WORKING NAME RENDERING',890,255)
    f['jp'].text(s,'スタンド・ザ・ファイト',890,303,30,width=460)
    body(s,'This is a phonetic katakana rendering of Stand The Fight. It preserves the sound of the academy name. The English discipline descriptor remains unchanged.',890,382,460,18,29)
    label(s,'KEEP THE COLUMN AS A UNIT',890,568)
    body(s,'Keep the small kana, voiced marks and centred punctuation. Do not rotate a horizontal text line to imitate vertical Japanese. Use the supplied outlined column.',890,619,460,18,29)
    label(s,'VOICE',890,810)
    body(s,'Direct, calm and welcoming. Make class details easy to find. Use only confirmed instructor, venue and affiliation information.',890,853,460,16,25)

    # 08
    s=page(8,'Social, without starting over','Square, portrait and story templates keep the same type hierarchy and room for class information.')
    for name,x,w,h in [('square',72,390,390),('portrait',519,390,488),('story',1010,300,533)]:
        a=apps[name];s.rect(x-1,236-1,w+2,h+2,RULE);fit(s,a,x,236,w,h)
        label(s,name.upper(),x,236+h+28)
    body(s,'The SVG templates have editable headline, date, venue and contact fields. The PDFs and PNGs are previews of those labelled fields, not finished announcements. Replace every field before publishing.',72,845,820,17,27)
    body(s,'Use the emblem avatar where the full logo would be too small. Keep its clearance for circular crops.',1007,845,340,16,26)

    # 09
    s=page(9,'Digital assets','Transparent SVG for interfaces. PNG for platforms and tools that need a raster file.')
    fit(s,apps['banner'],72,235,1296,486)
    label(s,'WEB / SOCIAL BANNER',72,747)
    body(s,'The banner is a flexible 1600 x 600 composition, not a platform-specific safe-area guarantee. The kit also includes light and dark avatars, SVG app icon, PNG icon sizes and an ICO file.',72,792,792,18,29)
    label(s,'TOKENS INCLUDED',960,792)
    body(s,'Ink, red, paper, typography and spacing are recorded in JSON and CSS. Use the compact logo or emblem in small headers.',960,833,395,17,27)

    # 10
    s=page(10,'Print that is ready to adapt','Editable class poster, blank letterhead, contact card templates and process-colour logo PDFs.')
    fit(s,apps['poster'],72,236,430,608)
    fit(s,apps['card-front'],568,247,420,272)
    fit(s,apps['card-back'],568,557,420,272)
    fit(s,apps['letterhead'],1040,247,300,424)
    label(s,'A3 POSTER',72,872);label(s,'85 x 55 MM CARDS',568,872);label(s,'A4 LETTERHEAD',1040,708)
    body(s,'Poster and card print PDFs include 3 mm bleed and trim boxes. Their labelled fields are templates. Replace them, then regenerate the print file. CMYK files are process-colour starting art; ask the printer to proof on the actual stock.',72,912,1280,14,22)

    # 11
    s=page(11,'Clothing and patches','Name patch and bilingual circular patch, with open gaps for stitching.')
    np=patch(f,pmark);rp=load_svg_scene(ROOT/'patches/balanced-circular/stf-n1-balanced-patch-75mm-colour.svg')
    fit(s,np,72,250,610,519);fit(s,rp,870,252,460,460)
    label(s,'NAME PATCH / 100 x 85 MM',72,817)
    body(s,'Two thread colours on a natural ground, or one black thread. The open-gap mark increases separation around the bodies and trouser hems.',72,859,610,17,27)
    label(s,'BILINGUAL PATCH / 75 OR 90 MM',870,757)
    body(s,'English and Japanese names follow balanced arcs. Colour and mono artwork is supplied at 75 and 90 mm. These are vectors, not stitch files; ask the supplier to digitise and sew a sample.',870,802,460,17,27)

    # 12
    s=page(12,'Patches in context','Illustrative material mockups. The vector artwork remains the production reference.')
    s.image(ROOT/'previews/embroidered-patch-mockup.png',72,250,615,545)
    s.image(ROOT/'previews/embroidered-circular-patch-mockup.png',753,250,615,545)
    label(s,'RECTANGULAR NAME PATCH / 100 x 85 MM',72,835)
    label(s,'BILINGUAL CIRCULAR PATCH / 90 MM',753,835)
    body(s,'Black and STF red on a natural ground.',72,878,615,17,27)
    body(s,'STF red Japanese lettering, paired with the English name.',753,878,615,17,27)

    # 13
    s=page(13,'Handoff and everyday use','The package is built around one set of vector shapes, with clear choices for each medium.')
    entries=[('logos/','Outlined SVG, transparent PNG and vector PDF. Primary, English, compact, emblem and wordmark. Colour, black, white and reverse.'),('social/ + web/','Editable SVG social templates, avatars, banner, icons, CSS and JSON tokens.'),('print/ + patches/','Print templates, CMYK PDFs, measured patch artwork and shirt artwork.'),('fonts/ + source/','Original licensed fonts, source code, selected reference and rebuild instructions.')]
    for k,(name,copy) in enumerate(entries):
        y=248+k*153;f['display'].text(s,name.upper(),72,y,34)
        body(s,copy,72,y+58,700,17,27)
    label(s,'KEEP THESE CONSTANT',925,251)
    y=305
    for copy in ['Preserve the overlap and its transparent gap.','Use the full English descriptor exactly as supplied.','Keep only one red head. Use a supplied mono file for one ink.','Use the compact or emblem file before shrinking microcopy.','Never stretch the logo, rebuild the name in live type, or move individual Japanese characters.']:
        s.circle(933,y+8,3,RED);y=body(s,copy,953,y,400,17,27)+29
    body(s,'Review status: refined identity and application kit for your assessment. The Japanese name is a working phonetic rendering. Print stock, thread choices and stitch files belong to the supplier handoff.',925,815,420,15,24)
    save_multipage(pages,ROOT/'guide/stand-the-fight-brand-guide-v1.pdf')
    return pages
