# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for TB-POC dataset."""

import importlib

import pytest
from click.testing import CliRunner


def id_function(val):
    if isinstance(val, dict):
        return str(val)
    return repr(val)


@pytest.mark.parametrize(
    "split,lengths",
    [
        ("fold-0", dict(train=292, validation=74, test=41)),
        ("fold-1", dict(train=292, validation=74, test=41)),
        ("fold-2", dict(train=292, validation=74, test=41)),
        ("fold-3", dict(train=292, validation=74, test=41)),
        ("fold-4", dict(train=292, validation=74, test=41)),
        ("fold-5", dict(train=292, validation=74, test=41)),
        ("fold-6", dict(train=292, validation=74, test=41)),
        ("fold-7", dict(train=293, validation=74, test=40)),
        ("fold-8", dict(train=293, validation=74, test=40)),
        ("fold-9", dict(train=293, validation=74, test=40)),
    ],
    ids=id_function,  # just changes how pytest prints it
)
def test_protocol_consistency(
    database_checkers,
    split: str,
    lengths: dict[str, int],
):
    from mednet.data.split import make_split

    database_checkers.check_split(
        make_split("mednet.config.classify.data.tbpoc", f"{split}.json"),
        lengths=lengths,
        prefixes=(
            "TBPOC_CXR/TBPOC-",
            "TBPOC_CXR/tbpoc-",
        ),
        possible_labels=(0, 1),
    )


@pytest.mark.skip_if_rc_var_not_set("datadir.tbpoc")
def test_database_check():
    from mednet.scripts.database import check

    runner = CliRunner()
    result = runner.invoke(check, ["tbpoc-f0"])
    assert (
        result.exit_code == 0
    ), f"Exit code {result.exit_code} != 0 -- Output:\n{result.output}"


@pytest.mark.skip_if_rc_var_not_set("datadir.tbpoc")
@pytest.mark.parametrize(
    "dataset",
    [
        "train",
        "validation",
        "test",
    ],
)
@pytest.mark.parametrize(
    "name",
    [
        "fold_0",
        "fold_1",
        "fold_2",
        "fold_3",
        "fold_4",
        "fold_5",
        "fold_6",
        "fold_7",
        "fold_8",
        "fold_9",
    ],
)
def test_loading(database_checkers, name: str, dataset: str):
    datamodule = importlib.import_module(
        f".{name}", "mednet.config.classify.data.tbpoc"
    ).datamodule

    datamodule.model_transforms = []  # should be done before setup()
    datamodule.setup("predict")  # sets up all datasets

    loader = datamodule.predict_dataloader()[dataset]

    limit = 3  # limit load checking
    for batch in loader:
        if limit == 0:
            break
        database_checkers.check_loaded_batch(
            batch,
            batch_size=1,
            color_planes=1,
            prefixes=("TBPOC_CXR/TBPOC-", "TBPOC_CXR/tbpoc-"),
            possible_labels=(0, 1),
            expected_num_labels=1,
            expected_meta_size=2,
        )
        limit -= 1


@pytest.mark.skip_if_rc_var_not_set("datadir.tbpoc")
def test_loaded_image_quality(database_checkers, datadir):
    reference_histogram_file = (
        datadir / "histograms/raw_data/histograms_tbpoc_fold_0.json"
    )

    datamodule = importlib.import_module(
        ".fold_0", "mednet.config.classify.data.tbpoc"
    ).datamodule

    datamodule.model_transforms = []
    datamodule.setup("predict")

    database_checkers.check_image_quality(datamodule, reference_histogram_file)
