"""
Vykreslí Kakuro puzzle do PDF z dat v MySQL tabulce puzzles.
"""

import json
import os
import sys
from pathlib import Path

sys.stdout.reconfigure(encoding="utf-8")
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "Knihovny"))

from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen.canvas import Canvas

from mysql_db import connect_mysql

_fonts_dir = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts")
pdfmetrics.registerFont(TTFont("Arial", os.path.join(_fonts_dir, "arial.ttf")))
pdfmetrics.registerFont(TTFont("ArialBold", os.path.join(_fonts_dir, "arialbd.ttf")))

OUTPUT = Path(__file__).parent / "test_kakuro.pdf"


def parse_grid(data_str: str) -> list[str]:
    rows = ["0" + r for r in data_str.split(",")]
    rows.insert(0, "0" * len(rows[0]))
    return rows


def sum_hor(grid, x, y):
    s = 0
    w = len(grid[0])
    for i in range(x + 1, w):
        if grid[y][i] == "0":
            break
        s += int(grid[y][i])
    return s if s > 0 else 0


def sum_vert(grid, x, y):
    s = 0
    h = len(grid)
    for i in range(y + 1, h):
        if grid[i][x] == "0":
            break
        s += int(grid[i][x])
    return s if s > 0 else 0


def draw_kakuro(c: Canvas, x0: float, y0: float, cell: float,
                data_str: str, title: str = "", show_solution: bool = False):
    grid = parse_grid(data_str)
    h = len(grid)
    w = len(grid[0])

    clue_font = max(cell * 0.3, 5)
    num_font = max(cell * 0.5, 6)

    if title:
        c.setFillColor(colors.black)
        c.setFont("ArialBold", 12)
        c.drawString(x0, y0 + 5, title)

    for gy in range(h):
        for gx in range(w):
            cx = x0 + gx * cell
            cy = y0 - (gy + 1) * cell
            ch = grid[gy][gx]

            if ch == "0":
                # Černá buňka
                c.setFillColor(colors.Color(0.15, 0.15, 0.15))
                c.rect(cx, cy, cell, cell, fill=1, stroke=0)

                sv = sum_vert(grid, gx, gy)
                sh = sum_hor(grid, gx, gy)

                if sv or sh:
                    # Diagonála
                    c.setStrokeColor(colors.white)
                    c.setLineWidth(0.5)
                    c.line(cx, cy + cell, cx + cell, cy)
                    c.setStrokeColor(colors.black)

                    c.setFillColor(colors.white)
                    c.setFont("ArialBold", clue_font)

                    if sh:
                        # Součet doprava — horní pravý trojúhelník
                        c.drawString(cx + cell * 0.52, cy + cell * 0.55, str(sh))

                    if sv:
                        # Součet dolů — dolní levý trojúhelník
                        c.drawString(cx + cell * 0.08, cy + cell * 0.08, str(sv))

            else:
                # Bílá buňka
                if show_solution:
                    c.setFillColor(colors.black)
                    c.setFont("ArialBold", num_font)
                    c.drawCentredString(cx + cell / 2, cy + cell * 0.3, ch)

    # Mřížka
    c.setStrokeColor(colors.black)
    for i in range(h + 1):
        c.setLineWidth(0.8)
        c.line(x0, y0 - i * cell, x0 + w * cell, y0 - i * cell)
    for i in range(w + 1):
        c.setLineWidth(0.8)
        c.line(x0 + i * cell, y0, x0 + i * cell, y0 - h * cell)


def generate_pdf(puzzles: list[dict], output_path: Path):
    """puzzles: difficulty, puzzle_str, puzzle_date"""
    BOARD_CM = 11
    SOL_CM = 6
    GAP = 1.5 * cm
    page_w, page_h = A4

    prepped = []
    for p in puzzles:
        grid = parse_grid(p["puzzle_str"])
        h, w = len(grid), len(grid[0])
        cell = BOARD_CM * cm / max(h, w)
        prepped.append((p, h, w, cell))

    c = Canvas(str(output_path), pagesize=A4)

    for i in range(0, len(prepped), 2):
        for j, (p, h, w, cell) in enumerate(prepped[i:i + 2]):
            x0 = (page_w - w * cell) / 2
            y0 = page_h - 2 * cm - j * (BOARD_CM * cm + 3 * cm)
            draw_kakuro(c, x0, y0, cell, p["puzzle_str"],
                        f"Kakuro {p['difficulty'].capitalize()} — {p['puzzle_date']}")
        c.showPage()

    c.setFont("ArialBold", 14)
    c.drawCentredString(page_w / 2, page_h - 2 * cm, "Řešení")
    y_cursor = page_h - 3.5 * cm
    for p, h, w, _ in prepped:
        sol_cell = SOL_CM * cm / max(h, w)
        x0 = (page_w - w * sol_cell) / 2
        draw_kakuro(c, x0, y_cursor, sol_cell, p["puzzle_str"],
                    p["difficulty"].capitalize(), show_solution=True)
        y_cursor -= h * sol_cell + GAP
    c.showPage()
    c.save()


def main():
    conn = connect_mysql(database="puzzle")
    cur = conn.cursor()
    cur.execute(
        "SELECT difficulty, puzzle, extra FROM puzzles "
        "WHERE game_type='kakuro' AND puzzle_date='2026-05-08' "
        "ORDER BY FIELD(difficulty, 'easy', 'medium', 'hard', 'expert') "
        "LIMIT 1"
    )
    row = cur.fetchone()
    cur.close()
    conn.close()

    if not row:
        print("Žádná data.")
        return

    difficulty, data_str, extra = row
    grid = parse_grid(data_str)
    grid_size = len(grid[0])

    page_w, page_h = A4
    board_cm = 11
    cell = board_cm * cm / grid_size
    board = grid_size * cell

    c = Canvas(str(OUTPUT), pagesize=A4)

    # Zadání (bez čísel)
    x0 = (page_w - board) / 2
    y0 = page_h - 2 * cm
    draw_kakuro(c, x0, y0, cell, data_str,
                f"Kakuro {difficulty.capitalize()} — 2026-05-08", show_solution=False)

    # Řešení (s čísly)
    y0_sol = y0 - board - 3 * cm
    draw_kakuro(c, x0, y0_sol, cell, data_str,
                f"Řešení", show_solution=True)

    c.save()
    print(f"PDF uloženo: {OUTPUT}")


if __name__ == "__main__":
    main()
