#!/usr/bin/env python3
"""Generate README screenshot images from test fixtures."""

import os
import subprocess
import sys


def _get_project_root():
    return os.path.dirname(os.path.abspath(__file__))


def _run_ics_render(argument_list):
    project_root = _get_project_root()
    ics_render_executable = os.path.join(project_root, ".venv", "bin", "ics_render")
    command = [ics_render_executable] + argument_list
    completed = subprocess.run(
        command,
        check=True,
        capture_output=True,
        text=True,
        cwd=project_root,
    )
    return completed.stdout


def _capture_screenshot(screenshots_directory, html_filename, png_filename, window_size):
    project_root = _get_project_root()
    html_path = os.path.join(screenshots_directory, html_filename)
    png_path = os.path.join(screenshots_directory, png_filename)
    file_url = "file://{path}".format(path=html_path)
    width, height = window_size
    command = [
        "brave",
        "--headless=new",
        "--disable-gpu",
        "--hide-scrollbars",
        "--screenshot={png_path}".format(png_path=png_path),
        "--window-size={width},{height}".format(width=width, height=height),
        file_url,
    ]
    subprocess.run(command, check=True, cwd=project_root)


def main():
    project_root = _get_project_root()
    screenshots_directory = os.path.join(project_root, "asset", "screenshots")
    os.makedirs(screenshots_directory, exist_ok=True)

    early_path = os.path.join(project_root, "tests", "fixtures", "early.ics")
    late_path = os.path.join(project_root, "tests", "fixtures", "late.ics")
    filepath_arguments = [
        "--filepath",
        early_path,
        "--filepath",
        late_path,
    ]

    html_grid_text = _run_ics_render(["--html"] + filepath_arguments)
    html_table_text = _run_ics_render(["--html-table"] + filepath_arguments)
    html_list_text = _run_ics_render(["--html-list"] + filepath_arguments)

    with open(
        os.path.join(screenshots_directory, "html-grid.html"),
        "w",
        encoding="utf-8",
    ) as file_handle:
        file_handle.write(html_grid_text)

    with open(
        os.path.join(screenshots_directory, "html-table.html"),
        "w",
        encoding="utf-8",
    ) as file_handle:
        file_handle.write(html_table_text)

    with open(
        os.path.join(screenshots_directory, "html-list.html"),
        "w",
        encoding="utf-8",
    ) as file_handle:
        file_handle.write(html_list_text)

    # Build a static demo page with the event modal open for documentation.
    modal_demo_text = html_grid_text.replace(
        'class="event-modal"',
        'class="event-modal is-open"',
        1,
    )
    modal_demo_text = modal_demo_text.replace(
        '<h2 id="event-modal-title" class="event-modal-title"></h2>',
        '<h2 id="event-modal-title" class="event-modal-title">Morning standup</h2>',
        1,
    )
    modal_demo_text = modal_demo_text.replace(
        '<dd class="event-modal-start"></dd>',
        '<dd class="event-modal-start">2024-06-01T09:00:00+00:00</dd>',
        1,
    )
    modal_demo_text = modal_demo_text.replace(
        '<dd class="event-modal-stop"></dd>',
        '<dd class="event-modal-stop">2024-06-01T09:30:00+00:00</dd>',
        1,
    )
    modal_demo_text = modal_demo_text.replace(
        '<dd class="event-modal-duration"></dd>',
        '<dd class="event-modal-duration">0:30:00</dd>',
        1,
    )
    modal_demo_text = modal_demo_text.replace(
        'class="event-modal-description-block">',
        'class="event-modal-description-block" style="display: block">',
        1,
    )
    modal_demo_text = modal_demo_text.replace(
        '<p class="event-modal-description"></p>',
        '<p class="event-modal-description">Daily team sync</p>',
        1,
    )
    with open(
        os.path.join(screenshots_directory, "html-grid-modal-demo.html"),
        "w",
        encoding="utf-8",
    ) as file_handle:
        file_handle.write(modal_demo_text)

    captures = [
        ("html-grid.html", "html-grid.png", (1100, 720)),
        ("html-table.html", "html-table.png", (1000, 360)),
        ("html-list.html", "html-list.png", (1000, 520)),
        ("html-grid-modal-demo.html", "html-grid-modal.png", (1100, 720)),
    ]

    # Capture each HTML preview as a PNG for the README.
    for html_filename, png_filename, window_size in captures:
        _capture_screenshot(screenshots_directory, html_filename, png_filename, window_size)

    return 0


if __name__ == "__main__":
    sys.exit(main())
