"""Test that files are encoded correctly.

Run with `pytest tests/test_encoding.py`, or `pytest` to run all tests.
"""

from pathlib import Path
import pytest

from textual_paint.ansi_art_document import AnsiArtDocument

ROUND_TRIP_EXCLUSIONS = [
    # These files are generated by a script, not the Textual Paint app, so they naturally change.
    "4x4_font_template.ans",
    "gradient_test.ans",
    # This one was free-handed with Inkscape, so naturally changes a lot.
    "pathological_character_grid.svg",
    # The `0x0.ans` file saves as 1x1, due to the minimum size.
    "0x0.ans",
    # This is a color palette file, meant to be loaded with Get Colors, not Open.
    "pipe_strip_palette.gpl",
]
ROUND_TRIP_XFAIL = [
    # THESE ONES MAYBE SHOULD IDEALLY WORK? But I need a better way of viewing the diff...
    "cp437_as_utf8.txt",
    "kitty_logo_v1.ans",
    "kitty_logo_v2.ans",
    "kitty_logo_v2.1_claws.ans",
    "kitty_logo_v2.2_cropped_whiskers_swapped.ans",
    "pipe_strip_sequel_micro_v1.ans",
    "pipe_strip_sequel_micro_v2.ans",
    "pipe_strip_sequel_mini_v1.ans",
    "pipe_strip_sequel_v1.ans",
    "pipe_strip_sequel_v2.ans",
    "pipe_strip_sequel_v3.ans",
    "pipe_strip_sequel_v4.ans",
    "pipe_strip_sequel_v5.ans",
    "pipe_strip_sequel_v6.ans",
    "pipe_strip_sequel_v7.ans",
    "pipe_strip_v12_tiling_v2.ans",
    "pipe_strip_v12_tiling_v3.ans",
    "text_tilt_tall.ans",
    "text_tilt_v0.ans",
    "text_tilt_v1.ans",
    "text_tilt_v2.ans",
    "text_tilt_v3.ans",
    "text_tilt_v4.ans",
    "text_tilt_v5_crazy_serifs.ans",
    "text_tilt_v6.ans",
    "text_tilt_v7.ans",
    "text_tilt_v8.ans",
    "text_tilt_v9.ans",
    "textual_paint_logo_ascii_v3.ans",
    "textual_paint_logo_v1.ans",
    "textual_paint_logo_v2.ans",
    "textual_paint_logo_v3.ans",
    "textual_paint_logo_v4.ans",
    "textual_paint_logo_v5.ans",
    "textual_paint_logo_v6.ans",
    "tool_options_v2.ans",
    "tool_options_v3.ans",
    "tool_options_v4.ans",
    "tool_options_v5_parens.ans",
    "tool_options_v6_red_ellipse_thing.ans",
    "tool_options_v7_red_over_top_border_confusing.ans",
    "tool_options_v8_paren_ornament.ans",
    "tool_options_v9_paren_ornaments_not_as_good.ans",
    "tool_options_v10_cropped_10x8.ans",
    "tool_options_v10_inverting_is_interesting.ans",
    "tool_options_v11_tweaked_braille_border_10x8.ans",
    "tool_options_v12_tweaked_right_border_to_be_braille_10x8.ans",
    "tool_options_v13_tweaked_right_border_corners_back_10x8.ans",
]
SAMPLES_DIR = Path(__file__).parent.parent / "samples"
SAMPLES = [f for f in SAMPLES_DIR.rglob("**/*") if f.is_file() and "~" not in f.name]
ROUND_TRIP_SAMPLES = [f for f in SAMPLES if f.name not in ROUND_TRIP_EXCLUSIONS]

@pytest.mark.parametrize("file_path", [pytest.param(f, id=f.name, marks=[pytest.mark.xfail] if f.name in ROUND_TRIP_XFAIL else []) for f in ROUND_TRIP_SAMPLES])
def test_round_trip(file_path: Path) -> None:
    """Test that files are re-encoded identically when opened and saved."""
    with open(file_path, "rb") as f:
        file_content = f.read()
        image = AnsiArtDocument.decode_based_on_file_extension(file_content, str(file_path))
        assert image.encode_based_on_file_extension(str(file_path)) == file_content
