from PIL import Image, ImageDraw, ImageFont

F = '/usr/share/fonts/truetype/wlg/Montserrat.ttf'
def f(s, n='Bold'):
    ft = ImageFont.truetype(F, s)
    try: ft.set_variation_by_name(n)
    except Exception: pass
    return ft

TEAL=(0,81,81); AMBER=(243,174,61); SKY=(175,214,230); WHITE=(255,255,255)
W,H=1200,1500; M=100; RW=W-2*M; LAB=400

def wrap(d, text, font, maxw):
    lines, cur = [], ''
    for w in text.split():
        t = (cur+' '+w).strip()
        if d.textlength(t, font=font) <= maxw: cur = t
        else:
            if cur: lines.append(cur)
            cur = w
    if cur: lines.append(cur)
    return lines

def block(d, y, text, font, fill, lh, maxw=RW, x=M):
    for ln in wrap(d, text, font, maxw):
        d.text((x, y), ln, font=font, fill=fill); y += lh
    return y

def spaced(s):
    return '  '.join(list(s.upper())).replace('     ', '    ')

CARDS = [
 dict(file='case_datacentre.png', eyebrow='CASE',
   who='A global data centre operator. R&D hub, Warsaw.',
   rows=[('68%','of the roles closed,','against a plan of 150 hires'),
         ('0','of our hires left in their','first eighteen months')],
   body='They were opening an R&D hub in Warsaw and needed 150 IT specialists inside eighteen months. We closed sixty eight per cent of the roles. Then the part that mattered more: not one of the people we placed walked away in their first eighteen months. The contract was extended to cover hiring for the data centres themselves.',
   kicker='Filling the role is the easy half.'),

 dict(file='case_ai_lab.png', eyebrow='CASE',
   who='A Chinese tech unicorn. AI lab, Warsaw.',
   rows=[('70%+','of the open positions filled',''),
         ('23','engineers hired into the lab','')],
   body='Two agencies were on the brief and we were one of them. We headhunted out of Google, Amazon Alexa, Yandex and Spotify, through six interview stages and a whiteboard. We also put the client in front of the Polish Investment and Trade Agency, so they arrived with the tax programmes already in hand.',
   kicker='The seats were the easy part. Landing the lab was not.'),

 dict(file='case_niche_market.png', eyebrow='CASE',
   who='A trading technology team in Poland. Five agencies had already failed.',
   rows=[('5','agencies had worked this','search and produced nobody'),
         ('4','companies in the country held','the skill, all closed to them')],
   body='The reason was structural rather than bad luck. That skill existed in exactly four Polish companies, and every one of them was off limits to those agencies under their own client contracts. We keep our client list clear of that conflict, which left the whole market open to us.',
   kicker='A crowded client list is a shrinking candidate market.'),

 dict(file='case_sourcing.png', eyebrow='HOW WE SOURCE',
   who='Where a third of our hires actually come from.',
   rows=[('32%','of our hires are people no','recruiter ever sees')],
   body='No social media. Never answer an advert. A LinkedIn profile half filled in, if there is one at all. We reach them through communities we have been part of for years, and through HH, Xing and GitHub. It is slower than posting a job. It is also where the person you actually want tends to be.',
   kicker='If they were easy to find, you would have found them.'),

 dict(file='case_scale.png', eyebrow='CASE',
   who='A Danish fintech serving more than fifty banks.',
   rows=[('600+','engineers in Poland today,','up from 120 when we started'),
         ('5','agencies work that same','account at the same time')],
   body='We came onto the account in 2018, when their Polish engineering base was 120 people. It has grown past 600 since, through every stage of the build. Five agencies work the same account, so nothing there is handed to you.',
   kicker='Scale is not one hire. It is years of not dropping the brief.'),
]

for c in CARDS:
    im = Image.new('RGB', (W,H), TEAL)
    d = ImageDraw.Draw(im)
    d.rectangle([0,0,W,10], fill=AMBER)

    d.text((M,78), 'WLG', font=f(44,'Bold'), fill=AMBER)
    _sf = f(22,'SemiBold')
    _st = '  '.join(list('BEYOND BORDERS')).replace('     ','    ')
    d.text((W-M-d.textlength(_st, font=_sf), 92), _st, font=_sf, fill=AMBER)
    d.text((M,178), spaced(c['eyebrow']), font=f(22,'SemiBold'), fill=SKY)
    y = block(d, 226, c['who'], f(38,'SemiBold'), WHITE, 52)

    y = 430
    for big, l1, l2 in c['rows']:
        d.text((M,y), big, font=f(96,'Bold'), fill=AMBER)
        if l2:
            d.text((LAB,y+12), l1, font=f(30,'Medium'), fill=WHITE)
            d.text((LAB,y+54), l2, font=f(30,'Medium'), fill=SKY)
        else:
            d.text((LAB,y+33), l1, font=f(30,'Medium'), fill=WHITE)
        y += 170

    block(d, y+50, c['body'], f(30,'Regular'), WHITE, 46)

    kf = f(38,'SemiBold')
    kl = wrap(d, c['kicker'], kf, RW)
    ky = 1352 - 50*len(kl)
    d.rectangle([M,ky-54,M+140,ky-48], fill=AMBER)
    block(d, ky, c['kicker'], kf, WHITE, 50)
    d.text((M,1418), 'Senior and director level technology hiring.  Poland and the Netherlands.',
           font=f(23,'Medium'), fill=SKY)

    im.save('/workspace/pub/'+c['file'], 'PNG')
    print(c['file'], im.size)
