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

import json
import os
import re
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_calcudoku.pdf"

OP_DISPLAY = {"+": "+", "-": "−", "*": "×", "/": "÷"}


def parse_cages(cages_str: str) -> list[dict]:
    cages = []
    for part in cages_str.split("|"):
        target, op, cells_str = part.split(",", 2)
        cells = [(ord(m[0]) - ord("a"), int(m[1]) - 1)
                 for m in re.findall(r"([a-h])(\d)", cells_str)]
        cages.append({"target": int(target), "op": op, "cells": cells})
    return cages


def parse_solution(solution_str: str, grid_size: int) -> list[list[int]]:
    return [[int(solution_str[r * grid_size + c])
             for c in range(grid_size)]
            for r in range(grid_size)]


def build_cage_map(cages: list[dict], grid_size: int) -> list[list[int]]:
    cage_map = [[-1] * grid_size for _ in range(grid_size)]
    for i, cage in enumerate(cages):
        for col, row in cage["cells"]:
            cage_map[row][col] = i
    return cage_map


def cage_label(cage: dict) -> str:
    if len(cage["cells"]) == 1:
        return str(cage["target"])
    return f"{cage['target']}{OP_DISPLAY.get(cage['op'], cage['op'])}"


def top_left_cell(cage: dict) -> tuple[int, int]:
    return min(cage["cells"], key=lambda c: (c[1], c[0]))


def draw_calcudoku(c: Canvas, x0: float, y0: float, cell: float,
                   cages: list[dict], cage_map: list[list[int]],
                   grid_size: int, title: str = "",
                   solution: list[list[int]] | None = None):
    label_font = max(cell * 0.28, 6)
    num_font = max(cell * 0.45, 7)
    thin = 0.4
    thick = 2.2

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

    # Bílé pozadí
    c.setFillColor(colors.white)
    c.rect(x0, y0 - grid_size * cell, grid_size * cell, grid_size * cell, fill=1, stroke=0)

    # Řešení
    if solution:
        c.setFillColor(colors.black)
        c.setFont("ArialBold", num_font)
        for row in range(grid_size):
            for col in range(grid_size):
                cx = x0 + col * cell + cell / 2
                cy = y0 - (row + 1) * cell + cell * 0.3
                c.drawCentredString(cx, cy, str(solution[row][col]))

    # Popisky klecí
    c.setFillColor(colors.Color(0.15, 0.15, 0.15))
    c.setFont("ArialBold", label_font)
    for cage in cages:
        col, row = top_left_cell(cage)
        label = cage_label(cage)
        lx = x0 + col * cell + cell * 0.06
        ly = y0 - row * cell - label_font * 1.1
        c.drawString(lx, ly, label)

    # Tenké vnitřní čáry
    c.setStrokeColor(colors.Color(0.75, 0.75, 0.75))
    c.setLineWidth(thin)
    for i in range(1, grid_size):
        c.line(x0, y0 - i * cell, x0 + grid_size * cell, y0 - i * cell)
        c.line(x0 + i * cell, y0, x0 + i * cell, y0 - grid_size * cell)

    # Tlusté hrany mezi různými klecemi
    c.setStrokeColor(colors.black)
    c.setLineWidth(thick)

    # Vnější okraj
    bx = x0
    by = y0 - grid_size * cell
    bw = grid_size * cell
    bh = grid_size * cell
    c.rect(bx, by, bw, bh, fill=0, stroke=1)

    # Horizontální hrany (mezi řádky row a row+1)
    for row in range(grid_size - 1):
        col_start = None
        for col in range(grid_size):
            border = cage_map[row][col] != cage_map[row + 1][col]
            if border and col_start is None:
                col_start = col
            elif not border and col_start is not None:
                lx1 = x0 + col_start * cell
                lx2 = x0 + col * cell
                ly = y0 - (row + 1) * cell
                c.line(lx1, ly, lx2, ly)
                col_start = None
        if col_start is not None:
            lx1 = x0 + col_start * cell
            lx2 = x0 + grid_size * cell
            ly = y0 - (row + 1) * cell
            c.line(lx1, ly, lx2, ly)

    # Vertikální hrany (mezi sloupci col a col+1)
    for col in range(grid_size - 1):
        row_start = None
        for row in range(grid_size):
            border = cage_map[row][col] != cage_map[row][col + 1]
            if border and row_start is None:
                row_start = row
            elif not border and row_start is not None:
                lx = x0 + (col + 1) * cell
                ly1 = y0 - row_start * cell
                ly2 = y0 - row * cell
                c.line(lx, ly1, lx, ly2)
                row_start = None
        if row_start is not None:
            lx = x0 + (col + 1) * cell
            ly1 = y0 - row_start * cell
            ly2 = y0 - grid_size * cell
            c.line(lx, ly1, lx, ly2)


def generate_pdf(puzzles: list[dict], output_path: Path):
    """
    Vygeneruje PDF ze seznamu puzzle.
    Každý dict musí mít: difficulty, cages_str, solution_str, grid_size, puzzle_date.
    """
    BOARD_CM = 11
    SOL_CM = 6
    GAP = 1.5 * cm
    page_w, page_h = A4

    prepped = []
    for p in puzzles:
        gs = p["grid_size"]
        cell = BOARD_CM * cm / gs
        cages = parse_cages(p["cages_str"])
        cage_map = build_cage_map(cages, gs)
        solution = parse_solution(p["solution_str"], gs)
        prepped.append((p, gs, cell, cages, cage_map, solution))

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

    # Zadání — 2 puzzle nad sebou na stránku
    for i in range(0, len(prepped), 2):
        for j, (p, gs, cell, cages, cage_map, _) in enumerate(prepped[i:i + 2]):
            board = gs * cell
            x0 = (page_w - board) / 2
            y0 = page_h - 2 * cm - j * (BOARD_CM * cm + 3 * cm)
            draw_calcudoku(c, x0, y0, cell, cages, cage_map, gs,
                           f"Calcudoku {p['difficulty']} — {p['puzzle_date']}")
        c.showPage()

    # Řešení
    c.setFont("ArialBold", 14)
    c.drawCentredString(page_w / 2, page_h - 2 * cm, "Řešení")
    y_cursor = page_h - 3.5 * cm
    for p, gs, _, cages, cage_map, solution in prepped:
        sol_cell = SOL_CM * cm / gs
        sol_board = gs * sol_cell
        x0 = (page_w - sol_board) / 2
        draw_calcudoku(c, x0, y_cursor, sol_cell, cages, cage_map, gs,
                       p["difficulty"], solution=solution)
        y_cursor -= sol_board + GAP
    c.showPage()
    c.save()


def main():
    conn = connect_mysql(database="puzzle")
    cur = conn.cursor()
    cur.execute(
        "SELECT difficulty, puzzle, solution, extra FROM puzzles "
        "WHERE game_type='calcudoku' AND puzzle_date='2026-05-08' "
        "ORDER BY FIELD(difficulty, '4x4', '5x5', '6x6', '8x8') "
        "LIMIT 1"
    )
    row = cur.fetchone()
    cur.close()
    conn.close()

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

    difficulty, cages_str, solution_str, extra_json = row
    extra = json.loads(extra_json)
    puzzles = [{
        "difficulty": difficulty,
        "cages_str": cages_str,
        "solution_str": solution_str,
        "grid_size": extra["grid_size"],
        "puzzle_date": "2026-05-08",
    }]
    generate_pdf(puzzles, OUTPUT)
    print(f"PDF uloženo: {OUTPUT}")


if __name__ == "__main__":
    main()
