package Common.UI.Menus_2023.TestingBar;
import Common.Current;
import Common.UI.Menus_2023.MenuBarButton;
import Common.UI.Menus_2023.VisualiserMenuBar;
import Common.UI.Themes.VisualiserFonts;
import Common.UI.UI;
import Common.Utils.Utils;
import TestingSystem.TestingServer;
import Visual_DVM_2021.Passes.PassCode_2021;

import javax.swing.*;
import java.awt.*;
public class TestingBar extends VisualiserMenuBar {
    JButton autorefreshButton;
    JSpinner sCheckTime;
    JSpinner sKernels;
    public TestingBar() {
        addPasses(PassCode_2021.SynchronizeTestsTasks);
        //--
        add(new JLabel(" ядер ") {
            {
                setFont(Current.getTheme().Fonts.get(VisualiserFonts.TreeItalic));
            }
        });
        add(sKernels = new JSpinner());
        sKernels.setPreferredSize(new Dimension(60, 26));
        sKernels.setMaximumSize(new Dimension(60, 26));
        sKernels.setModel(new SpinnerNumberModel(TestingServer.kernels, 1, 64, 1));
        UI.MakeSpinnerRapid(sKernels, e -> {
            TestingServer.kernels = (int) sKernels.getValue();
        });
        //--
        add(new MenuBarButton() {
            {
                setText("оповещение по email");
                setToolTipText("Оповещение о прогрессе выполнения пакета тестов");
                Mark();
                addActionListener(e -> {
                    TestingServer.email = !TestingServer.email;
                    Mark();
                });
            }
            public void Mark() {
                setIcon(Utils.getIcon(TestingServer.email ? "/icons/Pick.png" : "/icons/NotPick.png"));
            }

        });
        //--
        add(autorefreshButton = new MenuBarButton() {
            {
                setText("проверка раз в");
                setToolTipText("автоматическое обновление состояния пакета задач");
                Mark();
                addActionListener(e -> {
                    TestingServer.checkTasks = !TestingServer.checkTasks;
                    TestingServer.switchTimer(TestingServer.checkTasks);
                    Mark();
                });
            }
            public void Mark() {
                setIcon(Utils.getIcon(TestingServer.checkTasks ? "/icons/Pick.png" : "/icons/NotPick.png"));
            }
        });
        //--
        add(sCheckTime = new JSpinner());
        sCheckTime.setPreferredSize(new Dimension(60, 26));
        sCheckTime.setMaximumSize(new Dimension(60, 26));
        sCheckTime.setModel(new SpinnerNumberModel(TestingServer.checkIntervalSecond, 10, 3600, 1));
        UI.MakeSpinnerRapid(sCheckTime, e -> {
            TestingServer.checkIntervalSecond = (int) sCheckTime.getValue();
            if (TestingServer.checkTasks) TestingServer.ResetTimer();
        });
        add(new JLabel(" сек ") {
            {
                setFont(Current.getTheme().Fonts.get(VisualiserFonts.TreeItalic));
            }
        });
    }
    public void ShowAutorefresh() {
        autorefreshButton.setIcon(Utils.getIcon(TestingServer.checkTasks ? "/icons/Pick.png" : "/icons/NotPick.png"));
    }
}
