package CH.ifa.draw.standard;

import java.util.Collection;
import javax.swing.JMenuItem;

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import CH.ifa.draw.application.DrawApplication;
import CH.ifa.draw.util.CommandMenu;
import de.renew.plugin.PluginProperties;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

public class AlignmentMenuCreatorTest {

    @Test
    public void testCreateMenus() {
        //given
        PluginProperties mockPluginCfg = mock(PluginProperties.class);
        PluginProperties mockUserProps = mock(PluginProperties.class);

        when(mockPluginCfg.getHotkeyProperty(anyString())).thenReturn(null);
        when(mockPluginCfg.getHotkeyProperty(anyString(), any())).thenReturn(null);

        when(mockUserProps.getHotkeyProperty(anyString())).thenReturn(null);
        when(mockUserProps.getHotkeyProperty(anyString(), any())).thenReturn(null);

        CommandMenu mockCommandMenu = mock(CommandMenu.class);

        try (MockedStatic<PluginProperties> mockedProps = mockStatic(PluginProperties.class);
            MockedStatic<DrawApplication> mockedApp = mockStatic(DrawApplication.class)) {

            mockedProps.when(PluginProperties::getUserProperties).thenReturn(mockUserProps);

            mockedApp.when(() -> DrawApplication.createCommandMenu(anyString()))
                .thenReturn(mockCommandMenu);

            AlignmentMenuCreator creator = new AlignmentMenuCreator();

            //when
            Collection<JMenuItem> menus = creator.createMenus(mockPluginCfg);

            //then
            assertNotNull(menus, "Menu collection should not be null");
            assertFalse(menus.isEmpty(), "Menu collection should not be empty");

            boolean toggleGridPresent = menus.stream()
                .anyMatch(item -> item != null && "Toggle Snap to Grid".equals(item.getText()));
            assertTrue(toggleGridPresent, "Item 'Toggle Snap to Grid' should be present");
        }
    }
}