from ewoksid02.tasks.secondaryscatteringtask import SecondaryScatteringTask

from .utils import check_h5groups_common, check_h5groups_equivalent, execute_ewoks


def test_eiger2_2scat_numpy(
    inputs_task_generic,
    inputs_task_2scat,
    tmp_path,
    dataset_signal_norm,
    dataset_sigma_norm,
    filename_processed_2scat_full,
):
    processing_filename_numpy = str(tmp_path / "id02test_eiger2_2scat_numpy.h5")
    inputs_2scat_numpy = {
        **inputs_task_generic,
        **inputs_task_2scat,
        "processing_filename": processing_filename_numpy,
        "dataset_signal": dataset_signal_norm,
        "dataset_sigma": dataset_sigma_norm,
    }
    task_2scat_numpy = SecondaryScatteringTask(inputs_2scat_numpy)
    task_2scat_numpy.run()

    check_h5groups_equivalent(
        url_reference=f"silx://{processing_filename_numpy}?path=/entry_0000/PyFAI/result_2scat/",
        url_test=f"silx://{processing_filename_numpy}?path=/entry_0000/PyFAI/result_2scat/",
    )

    check_h5groups_common(
        filename_reference=filename_processed_2scat_full,
        filename_test=processing_filename_numpy,
    )


def test_eiger2_2scat_cupy(
    cupy_available,
    inputs_task_generic,
    inputs_task_2scat,
    tmp_path,
    dataset_signal_norm,
    dataset_sigma_norm,
    filename_processed_2scat_full,
):
    if not cupy_available:
        return

    processing_filename_cupy = str(tmp_path / "id02test_eiger2_2scat_numpy.h5")
    inputs_2scat_cupy = {
        **inputs_task_generic,
        **inputs_task_2scat,
        "processing_filename": processing_filename_cupy,
        "dataset_signal": dataset_signal_norm,
        "dataset_sigma": dataset_sigma_norm,
    }
    inputs_2scat_cupy["algorithm"] = "cupy"
    task_2scat_cupy = SecondaryScatteringTask(inputs_2scat_cupy)
    task_2scat_cupy.run()

    check_h5groups_equivalent(
        url_reference=f"silx://{processing_filename_cupy}?path=/entry_0000/PyFAI/result_2scat/",
        url_test=f"silx://{processing_filename_cupy}?path=/entry_0000/PyFAI/result_2scat/",
    )

    check_h5groups_common(
        filename_reference=filename_processed_2scat_full,
        filename_test=processing_filename_cupy,
    )


def test_eiger2_2scat_workflow(
    workflow_norm_2scat,
    inputs_task_generic,
    inputs_task_norm,
    inputs_task_2scat,
    tmp_path,
    filename_processed_norm_reference,
    filename_processed_2scat_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"})
    filename_processing_norm = str(tmp_path / "id02test_eiger2_norm.h5")
    filename_processing_2scat = str(tmp_path / "id02test_eiger2_2scat.h5")
    inputs.append(
        {"name": "processing_filename", "value": filename_processing_norm, "id": "norm"}
    )
    inputs.append(
        {
            "name": "processing_filename",
            "value": filename_processing_2scat,
            "id": "2scat",
        }
    )
    _ = execute_ewoks(
        graph=workflow_norm_2scat,
        inputs=inputs,
    )

    check_h5groups_equivalent(
        url_reference=f"silx://{filename_processed_norm_reference}?path=/entry_0000/PyFAI/result_norm/",
        url_test=f"silx://{filename_processing_norm}?path=/entry_0000/PyFAI/result_norm/",
    )
    check_h5groups_common(
        filename_reference=filename_processed_norm_reference,
        filename_test=filename_processing_norm,
    )
    check_h5groups_equivalent(
        url_reference=f"silx://{filename_processed_2scat_full}?path=/entry_0000/PyFAI/result_2scat/",
        url_test=f"silx://{filename_processing_2scat}?path=/entry_0000/PyFAI/result_2scat/",
    )
    check_h5groups_common(
        filename_reference=filename_processed_2scat_full,
        filename_test=filename_processing_2scat,
    )
