package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import Common.Visual.Controls.PassButton;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.TestingSystemPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;
import _VisualDVM.Utils;
import _VisualDVM.Visual.UI;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.io.File;
public class SynchronizeTests extends TestingSystemPass<Object> {
    File new_db_file;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        return Global.mainModule.getAccount().CheckRegistered(Log);
    }
    @Override
    public String getIconPath() {
        return "/icons/ComponentsActual.png";
    }
    @Override
    public String getButtonText() {
        return "";
    }
    @Override
    public JButton createButton() {
        return new PassButton(this);
    }
    @Override
    protected void showPreparation() throws Exception {
        server.db.SaveLastSelections();
        //--
        // server.db.machines _testing_system
        //--
        server.db.dvm_configurations.ClearUI();
        server.db.groups.ClearUI();
        server.db.sapforConfigurations.ClearUI();
        server.db.serverSapfors.ClearUI();
        server.db.dvmPackages.ClearUI();
        server.db.sapforPackages.ClearUI();
        server.db.sapforSettings.ClearUI();
        server.db.dvmSettings.ClearUI();
    }
    @Override
    protected void ServerAction() throws Exception {
        Command(new ServerExchangeUnit_2021(ServerCode.ReceiveTestsDatabase));
        response.Unpack(new_db_file = Utils.getTempFileName("tdb"));
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        server.db.Disconnect();
        Utils_.forceDeleteWithCheck(server.db.getFile());
        FileUtils.moveFile(new_db_file, server.db.getFile());
        server.db.Connect();
        server.db.prepareTablesStatements();
        server.db.Synchronize();
    }
    @Override
    protected void showDone() throws Exception {
        if (UI.HasMainWindow()) {
            //server.db.machines
            server.db.dvm_configurations.ShowUI();
            server.db.groups.ShowUI();
            server.db.sapforConfigurations.ShowUI();
            server.db.serverSapfors.ShowUI();
            server.db.dvmPackages.ShowUI();
            server.db.sapforPackages.ShowUI();
            server.db.sapforSettings.ShowUI();
            server.db.dvmSettings.ShowUI();
            server.db.RestoreLastSelections(); //---- ? объединить с RestoreCredentials
            UI.getMainWindow().ShowCheckedTestsCount();
        }
    }
}
