package _VisualDVM.Visual.Windows;
import Common.MainModule_;
import Common.Visual.Editor.BaseEditor;
import Common.Visual.FormWithSplitters;
import Common.Visual.TextField.StyledTextField;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Compiler.Compiler;
import _VisualDVM.GlobalData.Compiler.CompilerType;
import _VisualDVM.TestingSystem.Common.TestingServer;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
import _VisualDVM.TestingSystem.DVM.DVMTasks.DVMRunTask;
import _VisualDVM.TestingSystem.DVM.DVMTasks.UI.DVMRunTasksComparisonForm;
import _VisualDVM.TestingSystem.SAPFOR.SapforPackage.SapforPackage;
import _VisualDVM.Visual.Interface.TestingWindow;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class TestingForm implements FormWithSplitters, TestingWindow {
    //-
    private final BaseEditor coText;
    private final BaseEditor ceText;
    private final BaseEditor roText;
    private final BaseEditor reText;
    private final BaseEditor stsText;
    private final DVMRunTasksComparisonForm dvmTestingRunMaster; //сравнение тестов двм системы.
    private final DVMRunTasksComparisonForm dvmTestingRunSlave;
    //-
    private final SapforPackagesComparisonForm sapforPackageTreeMaster; //сравнение деревьев пакетов SAPFOR.
    private final SapforPackagesComparisonForm sapforPackageTreeSlave;
    //--
    private final SapforVersionsComparisonForm sapforVersionMaster; //сравнение версий тестов SAPFOR.
    private final SapforVersionsComparisonForm sapforVersionSlave;
    public JSplitPane SC65;
    public JSplitPane SC66;
    public JSplitPane SC64;
    public JSplitPane SC70;
    public JSplitPane SC63;
    public JSplitPane SC74;
    public JSplitPane SC80;
    public JSplitPane SC83;
    public JSplitPane SC72;
    public JSplitPane SC85;
    public JSplitPane SC50;
    public JSplitPane SC90;
    private JPanel content;
    //---
    private JTabbedPane testingTabs;
    private JPanel dvmPackagesTab;
    private JPanel dvmComparisonTab;
    private JPanel sapforPackagesTab;
    private JPanel sapforComparisonTab;
    private JPanel dvmRunTasksPanel;
    private JToolBar testsResultsTools;
    private JButton bChangeKernels;
    private JTextField filterName;
    private JPanel configurationsPanel;
    private JPanel dvmPackagesPanel;
    private JButton bCompilationFilter;
    private JPanel packageVersionsPanel;
    private JPanel sapforConfigurationsPanel;
    private JLabel sapforConfigurationsLabel;
    private JLabel sapforConfigurationsCommandsLabel;
    private JPanel sapforScenariosPanel;
    private JPanel serverSapforsPanel;
    private JPanel sapforPackagesPanel;
    private JPanel compilersPanel;
    private JPanel machinesPanel;
    private JPanel usersPanel;
    private JPanel credentialsPanel;
    private JPanel sapforSettingsPanel;
    private JPanel sapforSettingsCommandsPanel;
    private JPanel dvmSettingsPanel;
    private JTabbedPane taskResultsTabs;
    private JPanel compilationOutPanel;
    private JPanel compilationErrPanel;
    private JPanel runOutPanel;
    private JPanel runErrPanel;
    private JPanel runStsPanel;
    private JTabbedPane compilationTabs;
    private JTabbedPane runTabs;
    private JPanel testsRunTasksBackground;
    private JCheckBox filterFinished;
    private JButton bTest;
    private JButton bDownloadTestProject;
    private boolean events_on = true;
    private final PropertyChangeListener sc65Listener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (events_on) {
                events_on = false;
                SC66.setDividerLocation(SC65.getDividerLocation());
                events_on = true;
            }
        }
    };
    private final PropertyChangeListener sc66Listener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (events_on) {
                events_on = false;
                SC65.setDividerLocation(SC66.getDividerLocation());
                events_on = true;
            }
        }
    };
    //--
    public TestingForm() {
        content.add(Global.mainModule.getUI().getTestingMenuBar(), BorderLayout.NORTH);
        LoadSplitters();
        //-
        dvmTestingRunMaster = new DVMRunTasksComparisonForm(dvmTestingRunSlave = new DVMRunTasksComparisonForm(null));
        sapforPackageTreeMaster = new SapforPackagesComparisonForm(sapforPackageTreeSlave = new SapforPackagesComparisonForm(null));
        sapforVersionMaster = new SapforVersionsComparisonForm(
                sapforVersionSlave = new SapforVersionsComparisonForm(null, Current.SapforVersion),
                Current.SapforEtalonVersion
        );
        //--->>>
        RefreshTabsNames();
        SC90.setLeftComponent(dvmTestingRunMaster.getContent());
        SC90.setRightComponent(dvmTestingRunSlave.getContent());
        //-
        compilationOutPanel.add(new JScrollPane(coText = new BaseEditor()));
        compilationErrPanel.add(new JScrollPane(ceText = new BaseEditor()));
        runOutPanel.add(new JScrollPane(roText = new BaseEditor()));
        runErrPanel.add(new JScrollPane(reText = new BaseEditor()));
        runStsPanel.add(new JScrollPane(stsText = new BaseEditor()));
        //-
        SC65.setLeftComponent(sapforPackageTreeMaster.getContent());
        SC66.setLeftComponent(sapforPackageTreeSlave.getContent());
        //-
        SC65.setRightComponent(sapforVersionMaster.getContent());
        SC66.setRightComponent(sapforVersionSlave.getContent());
        //--
        SC65.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, sc65Listener);
        SC66.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, sc66Listener);
        //-
        Global.testingServer.db.dvmConfigurations.mountUI(configurationsPanel);
        Global.testingServer.db.dvmPackages.mountUI(dvmPackagesPanel);
        Global.testingServer.db.dvmRunTasks.mountUI(dvmRunTasksPanel);
        Global.testingServer.db.sapforConfigurations.mountUI(sapforConfigurationsPanel);
        Global.testingServer.db.serverSapfors.mountUI(serverSapforsPanel);
        Global.testingServer.db.sapforPackages.mountUI(sapforPackagesPanel);
        Global.testingServer.db.sapforSettings.mountUI(sapforSettingsPanel);
        Global.testingServer.db.sapforSettingsCommands.mountUI(sapforSettingsCommandsPanel);
        Global.testingServer.db.dvmSettings.mountUI(dvmSettingsPanel);
        //--
    }
    @Override
    public JPanel getContent() {
        return content;
    }
    //-
    @Override
    public void ShowAll() {
        Global.testingServer.db.dvmConfigurations.ShowUI();
        Global.testingServer.db.dvmPackages.ShowUI();
        Global.testingServer.db.sapforPackages.ShowUI();
        //--
        Global.testingServer.db.serverSapfors.ShowUI();
        Global.testingServer.db.sapforConfigurations.ShowUI();
        Global.testingServer.db.dvmRunTasks.ShowUI();
        Global.testingServer.db.dvmSettings.ShowUI();
        Global.testingServer.db.sapforSettings.ShowUI();
        //---
        Global.mainModule.getDb().RestoreCredentials();
        //---
        if (Global.normalProperties.AutoCheckTesting)
            TestingServer.TimerOn();
    }
    //-
    @Override
    public void ShowCurrentTestsPackage() {
        ShowCurrentTestRunTask();
    }
    @Override
    public void DropTestRunTasksComparison() {
        dvmTestingRunMaster.RemoveObject();
        dvmTestingRunSlave.RemoveObject();
    }
    @Override
    public void DropSapforComparison() {
        sapforPackageTreeMaster.RemoveObject();
        sapforPackageTreeSlave.RemoveObject();
        sapforVersionMaster.RemoveObject();
        sapforVersionSlave.RemoveObject();
    }
    @Override
    public void ShowCurrentTestRunTask() {
    }
    @Override
    public void ShowNoTestRunTask() {
    }
    @Override
    public void RefreshTabsNames() {
    }
    private void createUIComponents() {
        // TODO: place custom component creation code here
        filterName = new StyledTextField();
    }
    @Override
    public void FocusTestingTasks() {
    }
    @Override
    public void ShowCurrentSapforPackageVersionEtalon() {
        sapforVersionMaster.ApplyObject();
    }
    @Override
    public void ShowCurrentSapforPackageVersion() {
        sapforVersionSlave.ApplyObject();
    }
    @Override
    public void ShowNoSapforPackageVersionEtalon() {
        sapforVersionMaster.RemoveObject();
    }
    @Override
    public void ShowNoSapforPackageVersion() {
        sapforVersionSlave.RemoveObject();
    }
    @Override
    public void FocusSapforTesting() {
        testingTabs.setSelectedIndex(1);
    }
    @Override
    public void ShowCurrentCompiler() {
        Compiler compiler = (MainModule_.instance.getDb().getTable(Compiler.class).getUI().getCurrent());
        String title = (compiler != null) && (compiler.type.equals(CompilerType.dvm)) ?
                "DVM система: " + compiler.description : "DVM система: ?";
        testingTabs.setTitleAt(0, title);
    }
    @Override
    public void ShowCurrentServerSapfor() {
        testingTabs.setTitleAt(2, "SAPFOR: " + Global.testingServer.db.serverSapfors.getUI().getCurrent().version);
    }
    @Override
    public void ShowNoServerSapfor() {
        testingTabs.setTitleAt(2, "SAPFOR: ?");
    }
    @Override
    public void ShowDVMRunTask(DVMRunTask master) {
        //  dvmTestingRunMaster.ApplyObject(master);
        testingTabs.setSelectedIndex(1);
    }
    @Override
    public void ShowSapforPackage(SapforPackage master) {
        sapforPackageTreeMaster.ApplyObject(master);
        testingTabs.setSelectedIndex(3);
    }
    public void ShowDVMPackage(DVMPackage master) {
        testingTabs.setSelectedIndex(1);
    }
    @Override
    public void CompareSapforPackages(SapforPackage master, SapforPackage slave) {
        sapforPackageTreeMaster.ApplyObject(master);
        sapforPackageTreeSlave.ApplyObject(slave);
        sapforPackageTreeMaster.DoComparePass();
        testingTabs.setSelectedIndex(3);
    }
    @Override
    public void ShowCurrentDVMTask() {
        DVMRunTask object = Global.testingServer.db.dvmRunTasks.getUI().getCurrent();
        coText.setText(object.getCompilationOutput());
        ceText.setText(object.getCompilationErrors());
        roText.setText(object.getOutput());
        reText.setText(object.getErrors());
        stsText.setText(object.getStatistic());
        //-
        Global.testingServer.db.groups.getUI().SetCurrentByPK(object.group_id);
        Global.testingServer.db.tests.getUI().SetCurrentByPK(object.test_id);
    }
    @Override
    public void ShowNoCurrentDVMTask() {
        coText.setText("");
        ceText.setText("");
        roText.setText("");
        reText.setText("");
        stsText.setText("");
    }
    @Override
    public void FocusTestingTasksComparison() {
        taskResultsTabs.setSelectedIndex(1);
    }
    @Override
    public void CompareCurrentDVMTaskToEthalon(DVMRunTask ethalon, DVMRunTask task) throws Exception {
        dvmTestingRunMaster.ApplyObject(ethalon);
        dvmTestingRunSlave.ApplyObject(task);
        dvmTestingRunMaster.ChangeState(TasksComparisonState.RunOutput);
        dvmTestingRunSlave.ChangeState(TasksComparisonState.RunOutput);
        dvmTestingRunMaster.Compare();
    }
}
