package _VisualDVM.Passes.All;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.Passes.PassCode;
import _VisualDVM.Passes.SSH.ConnectionPass;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
import _VisualDVM.TestingSystem.DVM.PackageCredentials.DVMPackageCredentials;

import java.util.Date;
public class PauseDVMPackage extends ConnectionPass<DVMPackage> {
    @Override
    public String getIconPath() {
        return "/icons/Pause.png";
    }
    @Override
    public String getButtonText() {
        return "";
    }
    @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 (!Global.mainModule.getAccount().CheckAuthorship(target.sender_address, Log))
            return false;
        //--
        switch (target.state) {
            case CompilationExecution:
            case RunningExecution:
                break;
            default:
                Log.Writeln_("Возможно поставить на паузу только пакет в состоянии [компиляция] или [запуск]");
                return false;
        }
        GetDVMPackageCredetials pass_ = (GetDVMPackageCredetials) Global.mainModule.getPass(PassCode.GetDVMPackageCredetials);
        if (pass_.Do(target)) {
            DVMPackageCredentials credentials = pass_.target;
            machine = target.getMachine();
            user = target.getUser();
            user.password = credentials.userPassword;
            return true;
        }
        return false;
    }
    @Override
    protected void ServerAction() throws Exception {
        RemoteFile packageRemoteWorkspace = new RemoteFile(
                target.user_workspace + "/tests", String.valueOf(target.id), true);
        if (user.connection.Exists(packageRemoteWorkspace)) {
            try {
                RemoteFile pauseFile = new RemoteFile(packageRemoteWorkspace, Constants.pause);
                user.connection.writeToFile("pause at " + new Date(), pauseFile);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
