#!/usr/bin/env python

'''
test_mrscontrolpanelmini.py - Tests methods of MRSControlPanelMini class in controls.py

Authors: Vasilis Karlaftis    <vasilis.karlaftis@ndcn.ox.ac.uk>

Elements taken from fsleyes/fsleyes/tests/controls/test_controls.py and
    test_plotlistpanel.py by Paul McCarthy <pauldmccarthy@gmail.com>

Copyright (C) 2026 University of Oxford
'''

import os.path as op
from pathlib import Path
import fsl.data.image as fslimage
from tests import run_with_viewpanel, realYield

from fsleyes_plugin_mrs.controls    import MRSControlPanelMini
from fsleyes_plugin_mrs.views       import MRSView

datadir = Path(__file__).parent / 'testdata' / 'svs'

# Test #1: check if the title matches the expected value
def test_title():
    assert isinstance(MRSControlPanelMini.title(), str)
    assert MRSControlPanelMini.title() == "MRS control shortcut"

# Test #2: check if supportedViews include the MRSView
def test_supportedViews():
    assert isinstance(MRSControlPanelMini.supportedViews(), list)
    assert MRSView in MRSControlPanelMini.supportedViews()

# Test #3: check MRSControlPanelMini basic functionality on a single metabolite file
def test_toggle():
    run_with_viewpanel(_test_toggle, MRSView)

def _test_toggle(view, overlayList, displayCtx):

    img = fslimage.Image(op.join(datadir, 'metab'))
    overlayList.append(img)
    realYield(25)

    # toggle the panel off and on
    view.togglePanel(MRSControlPanelMini)
    realYield(25)

    view.togglePanel(MRSControlPanelMini)
    realYield(25)

# Test #4: check MRSControlPanelMini 'GeneralPlotSettings' widgets
def test_GeneralPlotSettings():
    run_with_viewpanel(_test_GeneralPlotSettings, MRSView)

def _test_GeneralPlotSettings(view, overlayList, displayCtx):
    img = fslimage.Image(op.join(datadir, 'metab'))
    overlayList.append(img)
    realYield(25)

    # toggle the panel on
    if not view.isPanelOpen(MRSControlPanelMini):
        view.togglePanel(MRSControlPanelMini)
    panel = view.getPanel(MRSControlPanelMini)
    realYield(25)
    
    groupName = 'GeneralPlotSettings'
    widgetList = panel.getWidgetList()
    assert groupName in widgetList.GetGroups()
    my_widgets = widgetList._WidgetList__groups[groupName].widgets
    # there should be only one FlexGrid widget
    assert len(my_widgets) == 1
    my_widgets = list(my_widgets.values())[0]
    # check that the FlexGrid dimensions match the expected size
    assert my_widgets.widget.GetRows() == 3
    assert my_widgets.widget.GetCols() == 8
    # within the FlexGrid, check that the following widgets are present
    disp_names = ['Plot real', 'Auto scale Y',
                  'Plot imaginary', 'Link phase',
                  'Plot magnitude', 'Link apodization']
    for name in disp_names:
        assert any(w.Label == name for w in my_widgets.panel.Children), \
            f"No widget found with displayName == '{name}'"

# Test #5: check MRSControlPanelMini 'DataseriesPlotSettings' widgets
def test_DataSeriesPlotSettings():
    run_with_viewpanel(_test_DataSeriesPlotSettings, MRSView)

def _test_DataSeriesPlotSettings(view, overlayList, displayCtx):
    img = fslimage.Image(op.join(datadir, 'metab'))
    overlayList.append(img)
    realYield(25)

    # toggle the panel on
    if not view.isPanelOpen(MRSControlPanelMini):
        view.togglePanel(MRSControlPanelMini)
    panel = view.getPanel(MRSControlPanelMini)
    realYield(25)
    
    groupName = 'DataseriesPlotSettings'
    widgetList = panel.getWidgetList()
    assert groupName in widgetList.GetGroups()
    # check that the following widgets are present
    disp_names = ['Apodize (in Hz)',
                  'Zero order phase : ',
                  'First order phase : ']
    my_widgets = widgetList._WidgetList__groups[groupName].widgets
    for name in disp_names:
        assert any(w.displayName == name for w in my_widgets.values()), \
            f"No widget found with displayName == '{name}'"
