from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from pathlib import Path

OUTDIR = Path('/home/agent/family/Lernen/Pruefungen_Emilia_4_Klasse/Mathe/_Neu_erstellte_Uebungsblaetter')
OUTDIR.mkdir(parents=True, exist_ok=True)
W,H=A4; M=42

def header(c, code, title):
    c.setFont('Helvetica-Bold',16); c.drawString(M,H-50,f'Lösungsblatt – {code}')
    c.setFont('Helvetica',11); c.drawString(M,H-68,title)
    c.setStrokeColor(colors.black); c.line(M,H-78,W-M,H-78)

def footer(c,page,total=2):
    c.setFont('Helvetica',8); c.setFillColor(colors.grey)
    c.drawRightString(W-M,24,f'Lösungsblatt Seite {page}/{total}')
    c.setFillColor(colors.black)

def section(c,y,n,title):
    c.setFont('Helvetica-Bold',12); c.drawString(M,y,f'{n}. {title}')
    return y-20

def lines(c,y,items,leading=17,font=10):
    c.setFont('Helvetica',font)
    for item in items:
        c.drawString(M+16,y,item); y-=leading
    return y

def two_col(c,y,left,right,leading=18):
    c.setFont('Helvetica',10.5)
    maxn=max(len(left),len(right))
    for i in range(maxn):
        if i<len(left): c.drawString(M+16,y,left[i])
        if i<len(right): c.drawString(M+285,y,right[i])
        y-=leading
    return y

def mult_solution(path):
    c=canvas.Canvas(str(path),pagesize=A4)
    header(c,'LZK 4.12','Textaufgaben / Schriftliche Multiplikation – neu erstelltes Übungsblatt')
    y=H-105
    y=section(c,y,1,'Rechenprotokoll ergänzen')
    y=two_col(c,y,[
        'a) 4 · 3508 = 14 032',
        '   4 · 3000 = 12 000',
        '   4 · 500 = 2 000',
        '   4 · 8 = 32',
        '   Summe = 14 032',
    ],[
        'b) 7 · 8179 = 57 253',
        '   7 · 8000 = 56 000',
        '   7 · 100 = 700',
        '   7 · 70 = 490',
        '   7 · 9 = 63',
        '   Summe = 57 253',
    ])
    y-=14
    y=section(c,y,2,'Geeignete Strategie wählen und ausrechnen')
    y=two_col(c,y,[
        'a) 4 · 20 500 = 82 000',
        'b) 9 · 65 201 = 586 809',
        'c) 6 · 27 083 = 162 498',
    ],[
        'd) 5 · 30 007 = 150 035',
        'e) 2 · 321 111 = 642 222',
        'f) 6 · 39 999 = 239 994',
    ])
    y-=14
    y=section(c,y,3,'Schriftlich multiplizieren')
    y=two_col(c,y,[
        'a) 3 · 718 = 2 154',
        'b) 4 · 6672 = 26 688',
    ],[
        'c) 6 · 35 490 = 212 940',
        'd) 5 · 81 296 = 406 480',
    ])
    y-=14
    y=section(c,y,4,'Fehlende Ziffern')
    y=lines(c,y,[
        'a) 2 · 738 = 1476  ODER  2 · 788 = 1576',
        '   Hinweis: Diese Aufgabe hat in der neu erstellten Fassung zwei gültige Lösungen.',
        'b) 7 · 2807 = 19 649',
        'c) 4 · 7726 = 30 904',
        'd) 9 · 873 = 7857',
    ])
    footer(c,1,1); c.save()

def div_solution(path):
    c=canvas.Canvas(str(path),pagesize=A4)
    header(c,'LZK 4.13','Rechenstrategien Division / Schriftliche Division – neu erstelltes Übungsblatt')
    y=H-105
    y=section(c,y,1,'Rechenprotokoll ergänzen')
    y=two_col(c,y,[
        'a) 3840 : 6 = 640',
        '   3600 : 6 = 600',
        '   240 : 6 = 40',
        '   Summe = 640',
    ],[
        'b) 16 192 : 8 = 2024',
        '   16 000 : 8 = 2000',
        '   192 : 8 = 24',
        '   Summe = 2024',
    ])
    y-=10
    y=section(c,y,2,'Geeignetes Vorgehen wählen')
    y=two_col(c,y,[
        'a) 39 996 : 4 = 9999',
        'b) 180 000 : 6 = 30 000',
        'c) 99 666 : 3 = 33 222',
    ],[
        'd) 116 945 : 5 = 23 389',
        'e) 42 256 : 8 = 5282',
        'f) 140 140 : 7 = 20 020',
    ])
    y-=10
    y=section(c,y,3,'Schriftlich dividieren')
    y=two_col(c,y,[
        'a) 6608 : 4 = 1652',
    ],[
        'b) 7535 : 5 = 1507',
    ])
    y-=10
    y=section(c,y,4,'Rechne aus')
    y=lines(c,y,[
        'a) 1000 + 25 + 25 + 25 + 25 + 50 + 50 + 100 = 1300',
        'b) 700 - 30 - 30 - 30 - 30 - 600 = -20',
        'c) 800 - 35 + 15 - 35 + 15 - 35 + 15 = 740',
    ],leading=16)
    y-=6
    y=section(c,y,5,'Rechne aus')
    y=two_col(c,y,[
        'a) 3 · 40 · 5 = 600',
        'b) 2 · 24 · 50 = 2400',
    ],[
        'c) 2 · 8 · 20 = 320',
        'd) 5 · 10 · 4 · 3 = 600',
    ],leading=16)
    y-=8
    y=section(c,y,6,'Dividiere mit und ohne Rest')
    y=two_col(c,y,[
        'a) 238 360 : 7 = 34 051 Rest 3',
    ],[
        'b) 406 376 : 8 = 50 797',
    ],leading=16)
    y-=8
    y=section(c,y,7,'Monsterzahl')
    y=lines(c,y,['574 689 521 : 7 = 82 098 503'],leading=16)
    y-=8
    y=section(c,y,8,'Gesuchte Zahl')
    y=lines(c,y,[
        'Gesuchte Zahl = 1250',
        'Kontrolle: 1250 · 4 = 5000; 5000 : 5 = 1000; 1000 : 2 = 500',
    ],leading=16)
    footer(c,1,1); c.save()

mult_path=OUTDIR/'Mathe_Valerie_Loesungsblatt_LZK_4_12_Textaufgaben_Schriftliche_Multiplikation_neu.pdf'
div_path=OUTDIR/'Mathe_Valerie_Loesungsblatt_LZK_4_13_Schriftliche_Division_Monsterzahl_neu.pdf'
mult_solution(mult_path)
div_solution(div_path)
print(mult_path)
print(div_path)
