package _VisualDVM.Passes.All;
import Common.Utils.Utils_;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Tasks.RunTask.RunTask;
import _VisualDVM.Passes.Sapfor.SilentSapforPass;

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class SPF_StatisticAnalyzer extends SilentSapforPass {
    public RunTask task;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (super.canStart(args)) {
            task = (RunTask) args[0];
            return true;
        }
        return false;
    }
    @Override
    protected void body() throws Exception {
        sapfor.RunAnalysis(
                getName(),
                -Global.messagesServer.getPort(),
                target.sapforProperties.pack(),
                Utils_.DQuotes(task.getLocalStsFile().getAbsolutePath()) +
                        " "
                        + Utils_.DQuotes(target.getStsFile().getAbsolutePath()));
    }
    @Override
    protected boolean isGoodCode() {
        return sapfor.getErrorCode() == 0;
    }
    @Override
    public boolean needsConfirmations() {
        return false;
    }
    @Override
    protected void performDone() throws Exception {
        if (target.getStsFile().exists()) {
            Files.copy(target.getStsFile().toPath(), task.getLocalStsTextFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
    }
}
