import asyncio
import dataclasses
import os
import re
from pathlib import Path
from typing import Literal

import aiofiles
from folkugat_web.model.lilypond import (LyricsLine, LyricsParagraph,
                                         LyricsText, RenderError)
from folkugat_web.model.temes import Tema
from folkugat_web.services import files as files_service
from folkugat_web.templates import templates
from folkugat_web.utils import batched

SCORE_BEGINNING = "% --- SCORE BEGINNING --- %"

RenderFormat = Literal["png"] | Literal["pdf"]


def source_to_single_score(source: str, tema: Tema) -> str:
    if tema.lyrics:
        lyrics_text = build_lyrics(tema.lyrics[0].content)
    else:
        lyrics_text = None
    print("AAAAAA")
    print(lyrics_text)
    print("AAAAAA")
    return templates.get_template("lilypond/single_score.ly").render(
        score_beginning=SCORE_BEGINNING,
        score_source=source,
        tema=tema,
        lyrics_text=lyrics_text,
    )


async def render(source: str, fmt: RenderFormat, output_filename: Path | None = None) -> tuple[Path | None, list[RenderError]]:
    input_filename = files_service.create_tmp_filename(extension=".ly")
    async with aiofiles.open(input_filename, "w") as f:
        _ = await f.write(source)

    output_filename = output_filename or files_service.create_tmp_filename()

    if fmt == "png":
        proc = await asyncio.create_subprocess_exec(
            "lilypond", "--png", "-o", output_filename, input_filename,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )
    else:
        proc = await asyncio.create_subprocess_exec(
            "lilypond", "-o", output_filename, input_filename,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
        )

    _stdout, stderr = await proc.communicate()

    if proc.returncode:
        esc_input_filename = re.escape(str(input_filename))
        error_re = re.compile(rf"{esc_input_filename}:(?P<line>\d+):(?P<pos>\d+): error: (?P<error>.*)$")
        error_matches = filter(None, map(error_re.match, stderr.decode("utf-8").splitlines()))
        errors = map(lambda m: RenderError.from_dict(m.groupdict()), error_matches)
        line_offset = source.splitlines().index(SCORE_BEGINNING) + 1
        errors_offset = [dataclasses.replace(e, line=e.line - line_offset) for e in errors if e.line > line_offset]
        return None, errors_offset

    if fmt == "png":
        # Check if the output generated multiple pages
        pages = sorted(output_filename.parent.rglob(f"{output_filename.name}*.png"))
        if len(pages) > 1:
            output_filename = pages[0]
            for page in pages[1:]:
                os.remove(page)
        else:
            output_filename = output_filename.with_suffix(".png")
    elif fmt == "pdf":
        output_filename = output_filename.with_suffix(".pdf")

    return output_filename, []


def build_lyrics(text: str, max_cols: int = 3) -> LyricsText:
    paragraphs = [LyricsParagraph(lines=par_str.splitlines()) for par_str in text.split("\n\n") if par_str]
    print(list(batched(paragraphs, max_cols)))
    return LyricsText(lines=[
        LyricsLine(paragraphs=list(line_pars))
        for line_pars in batched(paragraphs, max_cols)
    ])
