package _VisualDVM.Visual.Windows;
import Common.CommonConstants;
import Common.Current_;
import Common.Utils.Utils_;
import Common.Visual.UI_;
import _VisualDVM.Current;
import _VisualDVM.GlobalData.GlobalDatabase;
import _VisualDVM.Global;
import Common.Visual.TextField.StyledTextField;
import _VisualDVM.Visual.UI;
import _VisualDVM.GlobalData.Compiler.CompilerType;
import _VisualDVM.GlobalData.Credentials.Credentials;
import _VisualDVM.TestingSystem.Common.TestingServer;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
import _VisualDVM.TestingSystem.DVM.DVMTasks.DVMRunTask;
import _VisualDVM.TestingSystem.SAPFOR.SapforPackage.SapforPackage;
import _VisualDVM.Visual.Interface.FormWithSplitters;
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 JPanel content;
    @Override
    public JPanel getContent() {
        return content;
    }
    public JSplitPane SC50;
    public JSplitPane SC65;
    public JSplitPane SC66;
    public JSplitPane SC64;
    public JSplitPane SC70;
    public JSplitPane SC63;
    public JSplitPane SC71;
    public JSplitPane SC72;
    public JSplitPane SC74;
    public JSplitPane SC18;
    public JSplitPane SC19;
    public JSplitPane SC80;
    public JSplitPane SC83;
    public JSplitPane SC85;
    //---
    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 JPanel testsRunTasksBackground;
    private JCheckBox filterFinished;
    private JButton bTest;
    private JButton bDownloadTestProject;
    //-
    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;
    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(UI.testingBar, 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();
        if (Global.properties.collapseCredentials)
            CollapseCredentials();
        //-
        SC50.setLeftComponent(dvmTestingRunMaster.getContent());
        SC50.setRightComponent(dvmTestingRunSlave.getContent());
        //-
        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.mainModule.getDb().machines.mountUI(machinesPanel);
        Global.mainModule.getDb().users.mountUI(usersPanel);
        Global.mainModule.getDb().compilers.mountUI(compilersPanel);
        //-
        Global.testingServer.db.dvm_configurations.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);
        //--
        if (Global.properties.collapseCredentials)
            CollapseCredentials();
    }
    //-
    @Override
    public void ShowAll() {
        Global.mainModule.getDb().machines.ShowUI();
        Global.testingServer.db.dvm_configurations.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();
        //---
        RestoreLastCredentials();
        //---
        if (Global.properties.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() {
        // if (dvmTestingRunMaster.isActive()) dvmTestingRunMaster.ShowTask();
        //  else dvmTestingRunSlave.ShowTask();
    }
    @Override
    public void ShowNoTestRunTask() {
        //  dvmTestingRunMaster.Clear();
        // dvmTestingRunSlave.Clear();
    }
    @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() {
        String title = (Global.mainModule.HasCompiler() && Global.mainModule.getCompiler().type.equals(CompilerType.dvm)) ?
                "DVM система: " + Global.mainModule.getCompiler().description : "DVM система: ?";
        testingTabs.setTitleAt(0, title);
    }
    @Override
    public void ShowCurrentServerSapfor() {
        testingTabs.setTitleAt(2, "SAPFOR: " + Global.mainModule.getServerSapfor().version);
    }
    @Override
    public void ShowNoServerSapfor() {
        testingTabs.setTitleAt(2, "SAPFOR: ?");
    }
    @Override
    public void ShowCurrentDVMPackage() {
    }
    @Override
    public void ShowNoCurrentDVMPackage() {
    }
    @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 CompareDVMRunTasks(DVMRunTask master, DVMRunTask slave) {
        dvmTestingRunMaster.ApplyObject(master);
        dvmTestingRunSlave.ApplyObject(slave);
        dvmTestingRunMaster.DoComparePass(true);
        // testingTabs.setSelectedIndex(1);
    }
    @Override
    public void CompareSapforPackages(SapforPackage master, SapforPackage slave) {
        sapforPackageTreeMaster.ApplyObject(master);
        sapforPackageTreeSlave.ApplyObject(slave);
        sapforPackageTreeMaster.DoComparePass();
        testingTabs.setSelectedIndex(3);
    }
    public void RestoreLastCredentials() {
        Credentials credentials = Global.mainModule.getCredentials();
        if (credentials.machine_id != CommonConstants.Nan) {
            if (Global.mainModule.getDb().machines.containsKey(credentials.machine_id)) {
                (Global.mainModule.getDb()).machines.ShowUI(credentials.machine_id);
                if ((Global.mainModule.getDb()).users.containsKey(credentials.user_id)) {
                    (Global.mainModule.getDb()).users.ShowUI(credentials.user_id);
                } else {
                    credentials.user_id = CommonConstants.Nan;
                    (Global.mainModule.getDb()).UpdateCredentials();
                }
                if ((Global.mainModule.getDb()).compilers.containsKey(credentials.compiler_id)) {
                    (Global.mainModule.getDb()).compilers.ShowUI(credentials.compiler_id);
                } else {
                    credentials.compiler_id = CommonConstants.Nan;
                    (Global.mainModule.getDb()).UpdateCredentials();
                }
                //-
                if ((Global.mainModule.getDb()).makefiles.containsKey(credentials.makefile_id)) {
                    (Global.mainModule.getDb()).makefiles.ShowUI(credentials.makefile_id);
                } else {
                    credentials.makefile_id = CommonConstants.Nan;
                    (Global.mainModule.getDb()).UpdateCredentials();
                }
                //-
                if ((Global.mainModule.getDb()).runConfigurations.containsKey(credentials.runconfiguration_id)) {
                    (Global.mainModule.getDb()).runConfigurations.ShowUI(credentials.runconfiguration_id);
                } else {
                    credentials.runconfiguration_id = CommonConstants.Nan;
                    (Global.mainModule.getDb()).UpdateCredentials();
                }
            } else {
                credentials.machine_id = CommonConstants.Nan;
                credentials.user_id = CommonConstants.Nan;
                credentials.compiler_id = CommonConstants.Nan;
                credentials.remotesapfor_id = CommonConstants.Nan;
                credentials.makefile_id = CommonConstants.Nan;
                credentials.runconfiguration_id = CommonConstants.Nan;
                (Global.mainModule.getDb()).UpdateCredentials();
            }
        }
    }
    @Override
    public void CollapseCredentials() {
        Global.mainModule.getDb().splitters.get("SC71").position = SC71.getDividerLocation();
        UI_.Clear(credentialsPanel);
        credentialsPanel.add(UI.credentialsBar);
        SC71.setDividerLocation(30);
        SC71.setDividerSize(0);
        credentialsPanel.revalidate();
        credentialsPanel.repaint();
    }
    @Override
    public void ExpandCredentials() {
        UI_.Clear(credentialsPanel);
        credentialsPanel.add(SC18);
        SC71.setDividerLocation(200);
        SC71.setDividerSize(3);
        credentialsPanel.revalidate();
        credentialsPanel.repaint();
    }
}
