package _VisualDVM.Passes.All;
import Common.Visual.Controls.PassButton;
import _VisualDVM.ComponentsServer.UserAccount.AccountRole;
import _VisualDVM.ComponentsServer.UserAccount.UserAccount;
import _VisualDVM.Global;
import _VisualDVM.Passes.Server.TestingServerPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Utils;

import javax.swing.*;
import java.io.File;
public class SynchronizeTests extends TestingServerPass<Object> {
    File new_db_file;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        UserAccount account = Global.mainModule.getAccount();
        return (account.role.equals(AccountRole.Admin) || account.role.equals(AccountRole.Developer))
                && SendRequest(ServerCode.ReceiveTestsDatabase);
    }
    @Override
    public String getIconPath() {
        return "/icons/ComponentsActual.png";
    }
    @Override
    public JButton createButton() {
        return new PassButton(this);
    }
    @Override
    protected void showPreparation() throws Exception {
        getServer().db.DropUI();
    }
    @Override
    protected void body() throws Exception {
        request.server_response.Unpack(new_db_file = Utils.getTempFileName("tdb"));
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        getServer().db.ReplaceFileAndReconnect(new_db_file);
    }
    @Override
    protected void showDone() throws Exception {
        if (Global.mainModule.getUI().hasMainWindow()) {
            getServer().db.ResetUI();
            Global.mainModule.getUI().getMainWindow().ShowCheckedTestsCount();
        }
    }
}
