package Visual_DVM_2021.Passes.All;
import Common.Current;
import Common.Global;
import Common.Utils.Utils;
import Repository.Server.ServerCode;
import Repository.Server.ServerExchangeUnit_2021;
import SapforTestingSystem.SapforTasksPackage.SapforTasksPackage;
import TestingSystem.TasksPackage.TasksPackageState;
import Visual_DVM_2021.Passes.PassCode_2021;
import Visual_DVM_2021.Passes.TestingSystemPass;

import java.io.File;
public class DownloadSapforTasksPackage extends TestingSystemPass<SapforTasksPackage> {
    @Override
    public String getIconPath() {
        return "/icons/DownloadBugReport.png";
    }
    @Override
    public String getButtonText() {
        return "";
    }
    File tmpArchive = null;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (Current.Check(Log, Current.SapforTasksPackage)) {
            target = Current.getSapforTasksPackage();
            if (target.state.equals(TasksPackageState.Done)) return true;
            else Log.Writeln_("Пакет не завершен.");
        }
        return false;
    }
    @Override
    protected void ServerAction() throws Exception {
        Command(new ServerExchangeUnit_2021(ServerCode.DownloadSapforTasksPackage, Current.getAccount().email, target.id));
        response.Unpack(tmpArchive = Utils.getTempFileName(target.id));
    }
    @Override
    protected boolean validate() {
        return tmpArchive != null && tmpArchive.exists();
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        File packageWorkspace = new File(Global.SapforPackagesDirectory, target.id);
        Utils.forceDeleteWithCheck(packageWorkspace);
        if (passes.get(PassCode_2021.UnzipFolderPass).Do(
                tmpArchive.getAbsolutePath(),
                Global.SapforPackagesDirectory.getAbsolutePath(), false
        )) ;
    }
}

