"""Build the complete review kit from the selected L2 and original font files."""
from pathlib import Path
from importlib.metadata import version
from html import escape
import json
from graphics import *
from identity import build_identity,recolor
from applications import build_applications,fit
from guide import build_guide

def supporting_files():
    tokens={
        'name':'Stand The Fight',
        'descriptor':['TRADITIONAL JAPANESE','JIU-JITSU'],
        'katakana_working_name':'スタンド・ザ・ファイト',
        'colour':{'ink':INK,'red':RED,'paper':PAPER,'white':WHITE,'muted':MUTED},
        'print_starting_values':{'ink':'C0 M0 Y0 K100','red_approximate':'C0 M86 Y82 K22'},
        'type':{'display':'Anton','body':'Epilogue','japanese':'Noto Sans JP','descriptor_weight':300,'body_weight':400,'label_weight':600},
        'space_px':[4,8,16,24,32,48,64,96],
        'status':'Refined L2 / brand kit 1.2 / review edition',
    }
    (ROOT/'web/brand-tokens.json').write_text(json.dumps(tokens,ensure_ascii=False,indent=2)+'\n')
    (ROOT/'web/brand-tokens.css').write_text('''/* Stand The Fight: local fonts and brand tokens. */
@font-face {font-family:'STF Anton';src:url('../fonts/anton/Anton-Regular.ttf');font-weight:400;font-display:swap;}
@font-face {font-family:'STF Epilogue';src:url('../fonts/epilogue/Epilogue[wght].ttf');font-weight:100 900;font-display:swap;}
@font-face {font-family:'STF Japanese';src:url('../fonts/notosansjp/NotoSansJP[wght].ttf');font-weight:100 900;font-display:swap;}
:root {--stf-ink:#111111;--stf-red:#c81d25;--stf-paper:#f4f1eb;--stf-white:#fff;--stf-muted:#66625c;--stf-display:'STF Anton',sans-serif;--stf-body:'STF Epilogue',sans-serif;--stf-japanese:'STF Japanese',sans-serif;--stf-space:8px;}
.stf-display {font-family:var(--stf-display);font-weight:400;text-transform:uppercase;line-height:.98;}
.stf-copy {font-family:var(--stf-body);font-weight:400;line-height:1.55;}
.stf-label {font-family:var(--stf-body);font-weight:300;letter-spacing:.12em;text-transform:uppercase;}
/* Use the outlined SVG logo. These classes are for surrounding content. */
''')
    deps=['Pillow','numpy','fonttools','reportlab','pypdf','pypdfium2','vtracer','shapely']
    (ROOT/'source/requirements.txt').write_text('\n'.join(f'{p}=={version(p)}' for p in deps)+'\n')

def overview(f,mark,logos,apps):
    s=Scene(1800,1280,title='Stand The Fight | Brand kit overview')
    s.rect(0,0,1800,1280,PAPER)
    f['medium'].text(s,'STAND THE FIGHT / REFINED L2',60,43,16,tracking=.04)
    f['body'].text(s,'BRAND KIT 1.2',1740,43,14,align='right',fill=MUTED)
    s.line(60,89,1740,89,RULE)
    fit(s,logos['primary'],40,112,900,800)
    f['display'].text(s,'ONE IDENTITY.',1010,145,65,width=700)
    f['display'].text(s,'EVERY MEDIUM.',1010,225,65,width=700)
    f['body'].paragraph(s,'The selected throw, a quiet descriptor and anchored Japanese lettering. Built once as vector artwork, then adapted for digital, print and clothing.',1014,344,670,22,34)
    for x,col,name in [(1014,INK,'INK'),(1240,RED,'STF RED'),(1466,WHITE,'WHITE')]:
        s.rect(x,503,205,125,col)
        f['medium'].text(s,name,x,654,12,tracking=.04)
    fit(s,apps['card-front'],1007,733,345,224)
    fit(s,apps['square'],1395,730,280,280)
    s.line(60,1010,1320,1010,RULE)
    f['display'].text(s,'ANTON',60,1072,60)
    f['light'].text(s,'EPILOGUE LIGHT',395,1082,30,tracking=.05)
    f['jp'].text(s,'スタンド・ザ・ファイト',60,1170,27)
    f['body'].text(s,'VECTOR LOGOS / DIGITAL / PRINT / PATCHES',700,1172,15,tracking=.03)
    s.export(ROOT/'brand-overview',png_width=1800,transparent=False)
    return s

def web_context(f,pmark,logos):
    s=Scene(1440,1100,title='Stand The Fight | Website context study')
    s.rect(0,0,1440,1100,PAPER)
    fit(s,logos['compact'],42,15,150,126)
    for x,txt in [(750,'TRAINING'),(954,'ACADEMY'),(1155,'CONTACT')]:f['medium'].text(s,txt,x,69,15,tracking=.04)
    s.line(64,150,1376,150,RULE)
    f['light'].text(s,'STAND THE FIGHT',72,232,16,tracking=.15)
    f['display'].text(s,'TRADITIONAL',72,297,93,width=710)
    f['display'].text(s,'JAPANESE',72,414,93,width=600)
    f['display'].text(s,'JIU-JITSU',72,531,93,width=557)
    s.rect(75,696,236,64,INK)
    f['medium'].text(s,'CLASS DETAILS',102,721,15,fill=WHITE)
    s.add(pmark,925,217,1.18)
    s.line(72,877,1368,877,INK,2)
    f['display'].text(s,'THE ACADEMY',72,932,40)
    f['body'].paragraph(s,'A visual context study for the brand kit. Class details, venue information and instructor content are to be supplied by the academy.',650,937,660,18,29)
    s.export(ROOT/'previews/web-context',png_width=1440,transparent=False)
    return s

def write_index(pages=None):
    from html_guide import write_html
    write_html(pages)


def main():
    from balance_patch import main as build_balanced_patch
    build_balanced_patch()
    f,mark,pmark,logos=build_identity()
    apps=build_applications(f,mark,pmark,logos)
    supporting_files()
    overview(f,mark,logos,apps)
    web_context(f,pmark,logos)
    pages=build_guide(f,mark,pmark,logos,apps)
    render_pdf(ROOT/'guide/stand-the-fight-brand-guide-v1.pdf',ROOT/'previews/guide-pages',width=1440)
    write_index(pages)
    print(f'Built {len(pages)} guide pages and {len(logos)} logo families.')

if __name__=='__main__':main()
