package _VisualDVM.Passes.All;
import Common.CommonConstants;
import Common.Utils.Pair;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.Server.TestingServerPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;

import java.io.File;
public class RefreshDVMPackageResults extends TestingServerPass<DVMPackage> {
    @Override
    public String getIconPath() {
        return "/icons/Menu/Undo.png";
    }
    @Override
    protected boolean needsAnimation() {
        return true;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (!Global.testingServer.db.dvmPackages.getUI().CheckCurrent(Log))
            return false;
        target = Global.testingServer.db.dvmPackages.getUI().getCurrent();
        if (!new File(target.getLocalWorkspace(), CommonConstants.LOADED).exists()) {
            Log.Writeln_("Пакет не загружен!");
            return false;
        }
        return true;
    }
    @Override
    protected void body() throws Exception {
        target.checkFinishState();
    }
    @Override
    protected void performDone() throws Exception {
        SendRequest(ServerCode.ReplaceDVMPackageResults, "",
                new Pair<>(
                        target,
                        Utils_.fileToBytes(target.getJsonFile())
                )
        );
        Global.mainModule.getPass(PassCode.SynchronizeTests).Do();
    }
}
