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

OUT=Path(__file__).resolve().parents[1]/'assets'
OUT.mkdir(exist_ok=True)

NAVY=(5,14,25); NAVY2=(7,35,56); BLUE=(70,190,235); CYAN=(105,232,255); WHITE=(232,247,255); MUTED=(125,170,190); GOLD=(245,196,83)

def font(size,bold=False):
    paths=[
        '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf' if bold else '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
        '/usr/share/fonts/truetype/liberation2/LiberationSans-Bold.ttf' if bold else '/usr/share/fonts/truetype/liberation2/LiberationSans-Regular.ttf'
    ]
    for p in paths:
        try: return ImageFont.truetype(p,size)
        except: pass
    return ImageFont.load_default()

def vertical_gradient(w,h,c1,c2):
    im=Image.new('RGB',(w,h),c1)
    pix=im.load()
    for y in range(h):
        t=y/(h-1)
        for x in range(w):
            r=int(c1[0]*(1-t)+c2[0]*t); g=int(c1[1]*(1-t)+c2[1]*t); b=int(c1[2]*(1-t)+c2[2]*t)
            pix[x,y]=(r,g,b)
    return im.convert('RGBA')

def glow(draw_layer, shape_fn, blur=18):
    g=Image.new('RGBA', draw_layer.size,(0,0,0,0)); d=ImageDraw.Draw(g); shape_fn(d); return g.filter(ImageFilter.GaussianBlur(blur))

def draw_box(d,cx,cy,s,stroke=CYAN):
    # isometric transparent public cash box / open cube
    top=[(cx-s*.62,cy-s*.30),(cx,cy-s*.62),(cx+s*.62,cy-s*.30),(cx,cy+s*.02)]
    left=[(cx-s*.62,cy-s*.30),(cx,cy+s*.02),(cx,cy+s*.70),(cx-s*.62,cy+s*.34)]
    right=[(cx+s*.62,cy-s*.30),(cx,cy+s*.02),(cx,cy+s*.70),(cx+s*.62,cy+s*.34)]
    d.polygon(top, fill=(43,151,199,45), outline=stroke)
    d.polygon(left, fill=(22,97,140,55), outline=stroke)
    d.polygon(right, fill=(15,116,165,45), outline=stroke)
    for poly in [top,left,right]:
        d.line(poly+[poly[0]], fill=stroke, width=max(4,int(s*.045)), joint='curve')
    # public bars / ledger lines inside
    for i in range(3):
        y=cy+s*(.17+i*.13)
        d.line((cx-s*.30,y,cx+s*.30,y), fill=(220,245,255,150), width=max(2,int(s*.02)))
    d.ellipse((cx-s*.16,cy-s*.18,cx+s*.16,cy+s*.14), fill=GOLD, outline=(255,222,130), width=max(3,int(s*.025)))
    d.text((cx,cy+s*.01),'$',font=font(int(s*.22),True),anchor='mm',fill=NAVY)

def profile():
    W=H=1024
    im=vertical_gradient(W,H,NAVY,NAVY2)
    # vignette/grid
    layer=Image.new('RGBA',(W,H),(0,0,0,0)); d=ImageDraw.Draw(layer)
    for r in [190,300,410]: d.ellipse((W/2-r,H/2-r,W/2+r,H/2+r), outline=(105,232,255,28), width=4)
    for a in range(0,360,30):
        x=W/2+420*math.cos(math.radians(a)); y=H/2+420*math.sin(math.radians(a)); d.line((W/2,H/2,x,y), fill=(105,232,255,15), width=2)
    # glow box
    im.alpha_composite(glow(im, lambda gd: draw_box(gd,W/2,H/2-15,470,stroke=(105,232,255,210)), 22))
    d=ImageDraw.Draw(im); draw_box(d,W/2,H/2-15,470,stroke=CYAN)
    # subtle sun ticks
    d.ellipse((705,175,805,275), fill=GOLD)
    for a in range(0,360,30):
        x1=755+68*math.cos(math.radians(a)); y1=225+68*math.sin(math.radians(a))
        x2=755+95*math.cos(math.radians(a)); y2=225+95*math.sin(math.radians(a))
        d.line((x1,y1,x2,y2), fill=GOLD, width=10)
    d.text((755,231),'$',font=font(64,True),anchor='mm',fill=NAVY)
    im.save(OUT/'profile-la-caja-publica.png')

def banner():
    W,H=1500,500
    im=vertical_gradient(W,H,NAVY,NAVY2); d=ImageDraw.Draw(im)
    # grid + data flows
    for x in range(0,W,75): d.line((x,0,x,H),fill=(105,232,255,18),width=1)
    for y in range(25,H,55): d.line((0,y,W,y),fill=(105,232,255,14),width=1)
    random.seed(2)
    for j in range(6):
        pts=[]
        base=350-random.randint(0,160)
        for x in range(-20,W+50,90):
            y=base+math.sin(x/130+j)*30+random.randint(-12,12)
            pts.append((x,y))
        d.line(pts,fill=(105,232,255,32+j*8),width=3)
    # left text
    d.text((85,150),'La Caja Pública',font=font(86,True),fill=WHITE)
    d.text((91,230),'pagos y transferencias de Nación',font=font(35,True),fill=CYAN)
    d.text((91,285),'en datos oficiales',font=font(35,True),fill=(190,224,240))
    d.rounded_rectangle((91,365,560,420),radius=26,fill=(14,165,233,45),outline=(105,232,255,105),width=2)
    d.text((120,379),'Presupuesto Abierto · Argentina',font=font(25,True),fill=WHITE)
    # right symbol
    im.alpha_composite(glow(im, lambda gd: draw_box(gd,1160,250,260,stroke=(105,232,255,210)), 18))
    d=ImageDraw.Draw(im); draw_box(d,1160,250,260,stroke=CYAN)
    # scan ring
    for r in [120,185]: d.ellipse((1160-r,250-r,1160+r,250+r),outline=(105,232,255,45),width=5)
    d.arc((1160-215,250-215,1160+215,250+215),start=305,end=35,fill=CYAN,width=10)
    im.save(OUT/'banner-la-caja-publica.png')

profile(); banner()
print(OUT/'profile-la-caja-publica.png')
print(OUT/'banner-la-caja-publica.png')
