from ewoksid02.tasks.azimuthaltask import AzimuthalTask

from .utils import check_h5groups_common, check_h5groups_equivalent, execute_ewoks


def test_eiger2_azim_full(
    inputs_task_generic,
    inputs_task_azim,
    tmp_path,
    dataset_signal_cave_new,
    dataset_sigma_cave_new,
    filename_processed_azim_full,
):
    processing_filename = str(tmp_path / "id02test_eiger2_azim.h5")
    inputs = {
        **inputs_task_generic,
        **inputs_task_azim,
        "processing_filename": processing_filename,
        "dataset_signal": dataset_signal_cave_new,
        "dataset_sigma": dataset_sigma_cave_new,
    }
    task_azim = AzimuthalTask(inputs)
    task_azim.run()

    check_h5groups_equivalent(
        url_reference=f"silx://{filename_processed_azim_full}?path=/entry_0000/PyFAI/result_azim/",
        url_test=f"silx://{processing_filename}?path=/entry_0000/PyFAI/result_azim/",
    )

    check_h5groups_common(
        filename_reference=filename_processed_azim_full,
        filename_test=processing_filename,
    )


def test_eiger2_azim_workflow(
    workflow_norm_2scat_cave_azim,
    inputs_task_generic,
    inputs_task_norm,
    inputs_task_2scat,
    inputs_task_cave,
    inputs_task_azim,
    tmp_path,
    filename_processed_norm_reference,
    filename_processed_2scat_full,
    filename_processed_cave_full,
    filename_processed_azim_full,
):
    inputs = []
    for key, value in inputs_task_generic.items():
        inputs.append({"name": key, "value": value, "all": True})
    for key, value in inputs_task_norm.items():
        inputs.append({"name": key, "value": value, "id": "norm"})
    for key, value in inputs_task_2scat.items():
        inputs.append({"name": key, "value": value, "id": "2scat"})
    for key, value in inputs_task_cave.items():
        inputs.append({"name": key, "value": value, "id": "cave"})
    for key, value in inputs_task_azim.items():
        inputs.append({"name": key, "value": value, "id": "azim"})
    filename_processing_norm = str(tmp_path / "id02test_eiger2_norm.h5")
    filename_processing_2scat = str(tmp_path / "id02test_eiger2_2scat.h5")
    filename_processing_cave = str(tmp_path / "id02test_eiger2_cave.h5")
    filename_processing_azim = str(tmp_path / "id02test_eiger2_azim.h5")
    for name, value, id_ in [
        ("processing_filename", filename_processing_norm, "norm"),
        ("processing_filename", filename_processing_2scat, "2scat"),
        ("processing_filename", filename_processing_cave, "cave"),
        ("processing_filename", filename_processing_azim, "azim"),
    ]:
        inputs.append({"name": name, "value": value, "id": id_})
    _ = execute_ewoks(
        graph=workflow_norm_2scat_cave_azim,
        inputs=inputs,
    )
    for ref_file, test_file, proc_type in [
        (filename_processed_norm_reference, filename_processing_norm, "norm"),
        (filename_processed_2scat_full, filename_processing_2scat, "2scat"),
        (filename_processed_cave_full, filename_processing_cave, "cave"),
        (filename_processed_azim_full, filename_processing_azim, "azim"),
    ]:
        check_h5groups_equivalent(
            url_reference=f"silx://{ref_file}?path=/entry_0000/PyFAI/result_{proc_type}",
            url_test=f"silx://{test_file}?path=/entry_0000/PyFAI/result_{proc_type}",
        )

        check_h5groups_common(
            filename_reference=ref_file,
            filename_test=test_file,
        )
