from PIL import Image, ImageDraw, ImageFont, ImageFilter
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
ASSET = ROOT / "assets" / "mcw-live"
OUT = ROOT / "design" / "multicoreware-homepage-1920-basier-square-headings-v28.jpg"
W, H = 1920, 7634
L, R = 260, 1660
INK, BLUE, CYAN, PALE, WHITE = "#06172f", "#3A92FF", "#3A92FF", "#f3f7fb", "#ffffff"

def font(size, bold=False, family=None):
    if family is None and bold:
        family = "Basier Square"
    if family == "Basier Square":
        # Montserrat is the local render fallback for Basier Square in the JPG mockup.
        basier_fallback = Path("C:/Users/Admin/AppData/Local/Microsoft/Windows/Fonts/Montserrat-Bold.ttf" if bold else "C:/Users/Admin/AppData/Local/Microsoft/Windows/Fonts/Montserrat-Regular.ttf")
        if basier_fallback.exists(): return ImageFont.truetype(str(basier_fallback), size)
    if family == "Montserrat":
        montserrat_font = Path("C:/Users/Admin/AppData/Local/Microsoft/Windows/Fonts/Montserrat-Bold.ttf" if bold else "C:/Users/Admin/AppData/Local/Microsoft/Windows/Fonts/Montserrat-Regular.ttf")
        if montserrat_font.exists(): return ImageFont.truetype(str(montserrat_font), size)
    if family == "Space Grotesk":
        space_font = Path("C:/Users/Admin/AppData/Local/Microsoft/Windows/Fonts/SpaceGrotesk-Bold.ttf" if bold else "C:/Users/Admin/AppData/Local/Microsoft/Windows/Fonts/SpaceGrotesk-Regular.ttf")
        if space_font.exists(): return ImageFont.truetype(str(space_font), size)
    paths = [
        Path("C:/Windows/Fonts/segoeuib.ttf" if bold else "C:/Windows/Fonts/segoeui.ttf"),
        Path("C:/Windows/Fonts/arialbd.ttf" if bold else "C:/Windows/Fonts/arial.ttf"),
    ]
    for p in paths:
        if p.exists(): return ImageFont.truetype(str(p), size)
    return ImageFont.load_default()

im = Image.new("RGB", (W, H), WHITE)
d = ImageDraw.Draw(im)

def txt(x,y,s,size,color=INK,bold=False,spacing=4,anchor=None,family=None):
    resolved_family = family or ("Basier Square" if bold else None)
    d.text((x,y),s,font=font(size,bold,resolved_family),fill=color,spacing=spacing,anchor=anchor)

def wrap(s, width, f):
    words=s.split(); lines=[]; line=""
    for word in words:
        test=(line+" "+word).strip()
        if d.textlength(test,font=f)<=width: line=test
        else:
            if line: lines.append(line)
            line=word
    if line: lines.append(line)
    return "\n".join(lines)

def image_cover(path, box, dark=0):
    x,y,w,h=box; src=Image.open(path).convert("RGB")
    ratio=max(w/src.width,h/src.height); nw,nh=int(src.width*ratio),int(src.height*ratio)
    src=src.resize((nw,nh),Image.Resampling.LANCZOS)
    src=src.crop(((nw-w)//2,(nh-h)//2,(nw+w)//2,(nh+h)//2))
    if dark:
        shade=Image.new("RGBA",src.size,(2,13,30,dark)); src=Image.alpha_composite(src.convert("RGBA"),shade).convert("RGB")
    im.paste(src,(x,y))

def eyebrow(x,y,s,color=BLUE):
    txt(x,y,s.upper(),14,color,True)

def line(x1,y1,x2,y2,color="#dce6f0",width=1): d.line((x1,y1,x2,y2),fill=color,width=width)

# Header
d.rectangle((0,0,W,64),fill=WHITE); line(0,63,W,63,"#e6edf4")
logo=Image.open(ASSET/"logo.png").convert("RGBA"); logo.thumbnail((190,38),Image.Resampling.LANCZOS); im.paste(logo,(L,14),logo)
nav=[("EXPLORE US",True),("WHAT WE DO",True),("INDUSTRIES",True),("PARTNERS",True),("CAREERS",False),("RESOURCES",True)]
x=535
for item,has_menu in nav:
    txt(x,25,item,13,INK,True); item_w=d.textlength(item,font=font(13,True))
    if has_menu:
        cx=x+item_w+9; d.line((cx-4,29,cx,33),fill=INK,width=1); d.line((cx,33,cx+4,29),fill=INK,width=1)
    x+=item_w+(50 if has_menu else 38)
txt(x,25,"CONTACT US",13,INK,True); x+=d.textlength("CONTACT US",font=font(13,True))+30
d.rectangle((x,0,x+64,64),fill=BLUE); txt(x+32,32,"⌕",27,WHITE,False,anchor="mm"); x+=85
txt(x,25,"◎  EN",13,INK,True); d.line((x+51,29,x+55,33),fill=INK,width=1); d.line((x+55,33,x+59,29),fill=INK,width=1)

# Hero
Y=64; hero_h=680; image_cover(ASSET/"hero-alt.webp",(0,Y,W,hero_h))
overlay=Image.new("RGBA",(W,hero_h),(0,0,0,0)); od=ImageDraw.Draw(overlay)
for x in range(W):
    a=int(250*max(0,1-x/1350)); od.line((x,0,x,hero_h),fill=(2,15,35,a))
im.paste(overlay,(0,Y),overlay)
txt(L,Y+145,"MulticoreWare and AMD\nCollaborate to Advance\nPhysical AI and Autonomous\nRobotics on AMD Platforms.",48,WHITE,True,spacing=4,family="Basier Square")
d.rounded_rectangle((L,Y+470,L+470,Y+540),radius=28,fill=BLUE); txt(L+32,Y+494,"READ PRESS RELEASE",15,WHITE,True)

# news
Y+=hero_h; d.rectangle((0,Y,W,Y+44),fill=BLUE); news_text="MulticoreWare achieves ISO 9001 and ISO 27001 certifications"; news_x=L+245; txt(L,Y+15,"NOW AT MULTICOREWARE",11,WHITE,True); txt(news_x,Y+13,news_text,13,WHITE); read_x=news_x+d.textlength(news_text,font=font(13))+20; txt(read_x,Y+14,"READ NEWS  ›",11,WHITE,True)

# Company introduction and statistics
Y+=44; intro_h=440; d.rectangle((0,Y,W,Y+intro_h),fill=WHITE)
company_copy="Since 2009, MulticoreWare has been crafting solutions to complex technological problems with next generation software. We help companies enhance their digital experiences and enable greater efficiency across products and platforms."
txt(W//2,Y+55,wrap(company_copy,1320,font(26)),26,INK,False,11,anchor="ma")
stats=[("2009","Founded in"),("100+","Completed Projects"),("500+","Team"),("30+","years of technology experience")]
stat_top=Y+215; stat_w=350
for i,(number,label) in enumerate(stats):
    sx=L+i*stat_w
    if i>0: line(sx,stat_top-15,sx,stat_top+155,"#d2dce7",2)
    txt(sx+stat_w//2,stat_top,number,62,BLUE,True,anchor="ma")
    txt(sx+stat_w//2,stat_top+85,label,16,"#506177",False,anchor="ma")

# What we do
Y+=intro_h; cap_h=950; d.rectangle((0,Y,W,Y+cap_h),fill=INK)
txt(W//2,Y+70,"WHAT WE DO",58,WHITE,True,anchor="ma")
what_copy="We develop high-performance software, AI platforms, embedded intelligence, and silicon enablement solutions across cloud, edge, and heterogeneous compute architectures. Our expertise spans AI frameworks, distributed runtimes, Generative and Agentic AI, sensor perception, radar and vision systems, AI accelerators, semiconductor verification, and intelligent automation."
txt(W//2,Y+150,wrap(what_copy,1200,font(18)),18,"#c8d6e5",False,8,anchor="ma")
services=[("Performance Engineering","icon-performance.png"),("Audio-video Solutions","icon-audio.png"),("Machine Learning Frameworks","icon-ml.png"),("AI Application & Tools","icon-ai.png"),("Sensor Data & Fusion Engg.","icon-sensor.png"),("Autonomous Mobility","icon-mobility.png"),("Functional Safety","icon-safety.png"),("Embedded Systems & S/W Services","icon-embedded.png"),("Security & Compliance","icon-security.png")]
card_w,card_h,gap=448,165,28
for i,(title,icon_name) in enumerate(services):
    col,row=i%3,i//3; x=L+col*(card_w+gap); yy=Y+270+row*(card_h+gap)
    d.rounded_rectangle((x,yy,x+card_w,yy+card_h),radius=14,fill=WHITE)
    title_text=wrap(title,270,font(20,True)); title_lines=title_text.count("\n")+1; title_y=yy+(card_h-(title_lines*27))//2
    txt(x+38,title_y,title_text,20,INK,True,5)
    chev_x=x+42+d.textlength(title_text.split("\n")[-1],font=font(20,True)); txt(min(chev_x,x+310),title_y+2+(title_lines-1)*27,"›",24,INK,True)
    icon=Image.open(ASSET/icon_name).convert("RGBA")
    im.paste(icon,(x+card_w-118,yy+(card_h-icon.height)//2),icon)

# Featured work
Y+=cap_h; feat_h=1420; d.rectangle((0,Y,W,Y+feat_h),fill=WHITE); txt(L,Y+120,"BLOGS",68,INK,True)
txt(R-190,Y+155,"EXPLORE ALL  ›",13,BLUE,True)
main_y=Y+340; image_cover(ASSET/"android.webp",(L,main_y,880,560)); d.rectangle((1140,main_y,R,main_y+560),fill=PALE)
txt(1190,main_y+60,"MAY 11, 2026",12,"#697b90",True); txt(1190,main_y+110,wrap("Optimizing Android Application Performance for Remote GPU Rendering Platforms",410,font(39,True)),39,INK,True,2); txt(1190,main_y+490,"READ MORE  ›",13,BLUE,True)
small_y=main_y+600; cards=[("agentic.webp","AI & MACHINE LEARNING","Agentic AI for RAN observability, explainability and orchestration"),("drone.webp","EMBEDDED INTELLIGENCE","Vulkan-based Linux graphics enablement for a distributed GPU platform")]
for i,(img,cat,title) in enumerate(cards):
    x=L+i*715; image_cover(ASSET/img,(x,small_y,330,330)); d.rectangle((x+330,small_y,x+690,small_y+330),fill="#f7f9fc"); txt(x+365,small_y+45,cat,11,BLUE,True); txt(x+365,small_y+85,wrap(title,300,font(25,True)),25,INK,True,4); txt(x+365,small_y+270,"READ MORE  ›",12,BLUE,True)

# Ecosystem
Y+=feat_h; eco_h=530; d.rectangle((0,Y,W,Y+eco_h),fill=INK); txt(L,Y+125,"TECHNOLOGY\nSTACK",62,WHITE,True)
tech=["TensorFlow","PyTorch","ONNX","OpenVINO","CUDA","RISC-V","ROS 2","OpenCV","LLVM"]
tx=880; ty=Y+105
for i,name in enumerate(tech):
    col=i%3; row=i//3; x=tx+col*255; yy=ty+row*105; d.rectangle((x,yy,x+235,yy+88),outline="#4f7192",width=1); txt(x+117,yy+44,name,18,WHITE,True,anchor="mm")

# Industries
Y+=eco_h; ind_h=1190; d.rectangle((0,Y,W,Y+ind_h),fill=WHITE); txt(L,Y+110,"INDUSTRIES",68,INK,True); txt(L,Y+205,wrap("Our solutions and services span across different technology areas and can be customized to serve a variety of industries with multifarious use-cases.",950,font(19)),19,"#5d7086",False,8)
vis_y=Y+330; image_cover(ASSET/"automation.webp",(L,vis_y,820,690)); grad=Image.new("RGBA",(820,250),(0,0,0,0)); gd=ImageDraw.Draw(grad)
for yy in range(250): gd.line((0,yy,820,yy),fill=(4,20,42,int(230*yy/250)))
im.paste(grad,(L,vis_y+440),grad); txt(L+45,vis_y+570,"Industry 4.0",38,WHITE,True); txt(L+45,vis_y+630,"Intelligent automation. Autonomous inspection.\nReal-time decisions at the edge.",16,"#d1dfed")
inds=["Compute","Automotive","Robotics","Media & Entertainment","Smart Health","Smart Cities & Industry 4.0","Semiconductor"]
ix=1140; iy=vis_y; line(ix,iy,R,iy,"#cbd7e3")
for i,name in enumerate(inds):
    txt(ix+15,iy+25,name,21,BLUE if i==5 else INK,True); txt(R-15,iy+28,"›",22,BLUE,True); iy+=91; line(ix,iy,R,iy,"#cbd7e3")

# Insights
Y+=ind_h; ins_h=960; d.rectangle((0,Y,W,Y+ins_h),fill=PALE); txt(L,Y+120,"CASE STUDIES",68,INK,True); txt(R-165,Y+155,"EXPLORE ALL  ›",13,BLUE,True)
cy=Y+330; image_cover(ASSET/"audio.webp",(L,cy,670,500),35); d.rectangle((L,cy+320,L+670,cy+500),fill="#07182f"); txt(L+35,cy+345,"CASE STUDY  ·  AUG 2026",11,CYAN,True); txt(L+35,cy+380,"Spatial audio optimization\non Cadence HiFi 4 DSP",28,WHITE,True)
d.rectangle((955,cy,1290,cy+500),fill=WHITE); txt(990,cy+45,"BLOG · SEP 2026",11,BLUE,True); txt(990,cy+90,wrap("ThreadedME: breaking x265’s motion-estimation bottleneck on multi-core CPUs",265,font(24,True)),24,INK,True,4); txt(990,cy+390,"READ ARTICLE  ›",12,BLUE,True)
image_cover(ASSET/"building.webp",(1320,cy,340,500),45); d.rectangle((1320,cy+325,1660,cy+500),fill="#07182f"); txt(1350,cy+350,"CASE STUDY · SEP 2026",10,CYAN,True); txt(1350,cy+385,"Building a multi-silicon\nAI cloud",23,WHITE,True)

# Partner / CTA / footer
Y+=ins_h; partner_h=520; d.rectangle((0,Y,W,Y+partner_h),fill=WHITE); txt(L,Y+100,"CLIENTS & PARTNERS",58,INK,True); txt(L,Y+190,"Our clients and partners span across silicon platform partners to edge sensor makers to Tier-1 to OEMs in various verticals.",18,"#5d7086")
logos=["AMD","TEXAS INSTRUMENTS","CADENCE","V-NOVA","ECARX","NVIDIA"]
for i,name in enumerate(logos):
    x=L+i*(1400//6); d.ellipse((x+25,Y+285,x+190,Y+450),outline="#dce5ee",width=2); txt(x+107,Y+368,name,16,"#334a64",True,anchor="mm")
Y+=partner_h; d.rectangle((0,Y,W,H),fill="#04152d"); logo2=logo.copy(); im.paste(logo2,(L,Y+75),logo2)
cols=[("EXPLORE US",["About us","Leadership","Our story","News & events","Careers"]),("WHAT WE DO",["Engineering services","Products & platforms","Open source","Success stories"]),("INDUSTRIES",["Compute","Automotive","Robotics","Media & entertainment","Smart health"]),("CONNECT",["Contact us","Global locations","Partnership inquiries","LinkedIn ›"])]
for i,(head,items) in enumerate(cols):
    x=670+i*245; txt(x,Y+80,head,12,CYAN,True); yy=Y+125
    for item in items: txt(x,yy,item,13,"#b0c1d2"); yy+=34
line(L,H-80,R,H-80,"#2c465f"); txt(L,H-55,"© 2026 MulticoreWare, Inc.",12,"#8198b0"); txt(R,H-55,"Privacy Policy    Terms of Use    Sitemap",12,"#8198b0",anchor="ra")

OUT.parent.mkdir(parents=True,exist_ok=True)
im.save(OUT,"JPEG",quality=94,subsampling=0,optimize=True)
print(f"{OUT}\n{W}x{H}")
