"""Integration tests for the generic XRPD processor."""

from ...bliss_globals import setup_globals
from .. import testing
from ..processors.xrpd import DemoXrpdProcessor
from .utils import xrpd


def xrpd_demo_1d(nrepeats: int = None, expo=0.2, npoints=10):
    test_xrpd_ct_with_1d_integration(nrepeats=nrepeats, expo=expo)
    test_xrpd_scan_with_1d_integration(nrepeats=nrepeats, expo=expo, npoints=npoints)
    test_xrpd_scan_with_two_1d_integrations(
        nrepeats=nrepeats, expo=expo, npoints=npoints
    )


def xrpd_demo_2d(nrepeats: int = None, expo=0.2, npoints=10):
    test_xrpd_ct_with_2d_integration(nrepeats=nrepeats, expo=expo)
    test_xrpd_scan_with_2d_integration(nrepeats=nrepeats, expo=expo, npoints=npoints)


@testing.integration_fixture
def difflab6_processor_1d(gc_collect):
    xrpd_processor = DemoXrpdProcessor()
    xrpd_processor.enable(setup_globals.difflab6)
    xrpd_processor._plotter.clear_lima_plots("difflab6")
    yield xrpd_processor
    xrpd_processor.disable()


@testing.integration_fixture
def diffcam_processor_1d(gc_collect):
    # Ensure this processor can exist in parallel to difflab6_processor_1d
    config = {
        "name": "diffcam_processor",
        "plugin": "generic",
        "class": "DemoXrpdProcessor",
        "package": "blissoda.demo.processors.xrpd.processor",
        "shared": False,
        "singleton": False,
    }
    xrpd_processor = DemoXrpdProcessor(config=config)
    xrpd_processor.enable(setup_globals.diffcam)
    # TODO: produce different data or fix PONI
    # xrpd_processor.integration_options["mask"] = numpy.zeros((540, 516), dtype=int).tolist()
    xrpd_processor._plotter.clear_lima_plots("diffcam")
    yield xrpd_processor
    xrpd_processor.disable()


@testing.integration_fixture
def difflab6_processor_2d(gc_collect):
    xrpd_processor = DemoXrpdProcessor()
    xrpd_processor.enable(setup_globals.difflab6)
    xrpd_processor.integration_options["nbpt_azim"] = 360
    xrpd_processor._plotter.clear_lima_plots("difflab6")
    yield xrpd_processor
    xrpd_processor.integration_options.pop("nbpt_azim", None)
    xrpd_processor.disable()


@testing.integration_test
def test_xrpd_ct_with_1d_integration(
    debug_flint, difflab6_processor_1d, nrepeats: int = None, expo=0.2
):
    xrpd.test_xrpd_ct_with_1d_integration(
        difflab6_processor_1d,
        [setup_globals.difflab6, setup_globals.diode1, setup_globals.diode2],
        nrepeats=nrepeats,
        expo=expo,
    )


@testing.integration_test
def test_xrpd_ct_with_2d_integration(
    debug_flint, difflab6_processor_2d, nrepeats: int = None, expo=0.2
):
    xrpd.test_xrpd_ct_with_2d_integration(
        difflab6_processor_2d,
        [setup_globals.difflab6, setup_globals.diode1, setup_globals.diode2],
        nrepeats=nrepeats,
        expo=expo,
    )


@testing.integration_test
def test_xrpd_scan_with_1d_integration(
    debug_flint, difflab6_processor_1d, nrepeats: int = None, expo=0.2, npoints=10
):
    xrpd.test_xrpd_scan_with_1d_integration(
        [difflab6_processor_1d],
        [
            setup_globals.difflab6,
            setup_globals.diode1,
            setup_globals.diode2,
        ],
        nrepeats=nrepeats,
        expo=expo,
        npoints=npoints,
    )


@testing.integration_test
def test_xrpd_scan_with_2d_integration(
    debug_flint, difflab6_processor_2d, nrepeats: int = None, expo=0.2, npoints=10
):
    xrpd.test_xrpd_scan_with_2d_integration(
        [difflab6_processor_2d],
        [
            setup_globals.difflab6,
            setup_globals.diode1,
            setup_globals.diode2,
        ],
        nrepeats=nrepeats,
        expo=expo,
        npoints=npoints,
    )


@testing.integration_test
def test_xrpd_scan_with_two_1d_integrations(
    debug_flint,
    difflab6_processor_1d,
    diffcam_processor_1d,
    nrepeats: int = None,
    expo=0.2,
    npoints=10,
):
    xrpd.test_xrpd_scan_with_1d_integration(
        [difflab6_processor_1d, diffcam_processor_1d],
        [
            setup_globals.difflab6,
            setup_globals.diffcam,
            setup_globals.diode1,
            setup_globals.diode2,
        ],
        nrepeats=nrepeats,
        expo=expo,
        npoints=npoints,
    )
