package _VisualDVM.Passes.All;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.ComponentsServer.Component.Component;
import _VisualDVM.ComponentsServer.Component.ComponentType;
import _VisualDVM.Global;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.ProcessPass;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class BuildComponent extends ProcessPass<Component> {
    @Override
    protected PassCode necessary() {
        return PassCode.DownloadSapforRepository;
    }
    @Override
    protected boolean resetsNecessary() {
        return true;
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        target = Global.components.getUI().getCurrent();
        return true;
    }
    @Override
    protected void performPreparation() throws Exception {
        Utils_.forceDeleteWithCheck(target.getAssemblyFile());
    }
    @Override
    protected void body() throws Exception {
        ShowMessage1("Сборка " + target.getComponentType().getDescription());
        if (target.getComponentType().equals(ComponentType.Sapfor_F)) {
            File RepoSapfor = new File(Global.RepoDirectory, "SAPFOR");
            File installationDirectory = new File(RepoSapfor, "install");
            Utils_.CheckAndCleanDirectory(installationDirectory);
        }
        PerformScript(target.getAssemblyCommand());
        if (!target.getAssemblyFile().exists())
            throw new PassException("Сборка не найдена");
        Files.copy(target.getAssemblyFile().toPath(),
                target.getNewFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
        if (!target.getNewFile().exists())
            throw new PassException("Не удалось скопировать сборку для установки");
    }
}
