package Visual_DVM_2021.Passes.SSH;
import Common.Utils.Utils_;
import _VisualDVM.Constants;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.Machine.MachineType;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.GlobalData.User.User;

import java.util.Date;
public abstract class RepositoryServerSSHPass extends ConnectionPass {
    protected abstract String getServerFileName();
    protected abstract String getServerHomeName();
    @Override
    public String getButtonText() {
        return "";
    }
    //--
    protected RemoteFile userHome;
    protected RemoteFile serverHome;
    @Override
    protected boolean needsAnimation() {
        return true;
    }
    @Override
    public void Connect() throws Exception {
        userHome = null;
        serverHome = null;
        //--
        machine = new Machine("alex", Global.properties.ServerAddress, Global.properties.ServerUserSHHPort, MachineType.Server);
        user = new User(Global.properties.ServerUserName, "mprit_2011");
        super.Connect();
        //--
        userHome = new RemoteFile(user.connection.sftpChannel.getHome(), true);
        serverHome = new RemoteFile(userHome, getServerHomeName());
        //-
    }
    //--
    protected void StartServer() throws Exception {
        user.connection.startShellProcess(serverHome,"server_out.txt", "java -jar " + getServerFileName());
        //--
        RemoteFile StartLog = new RemoteFile(serverHome, Constants.STARTED +
                "_by_" +
                Global.mainModule.getAccount().email +
                "_" +
                new Date().toString()
        );
        user.connection.writeToFile("", StartLog);
        //--
        RemoteFile[] files = new RemoteFile[]{
                new RemoteFile(serverHome, Constants.script),
                new RemoteFile(serverHome, Constants.out_file),
                new RemoteFile(serverHome, Constants.err_file)
        };
        //-
        for (RemoteFile file : files) {
            user.connection.tryRM(file);
        }
    }
    protected void ShutdownServer() throws Exception {
        RemoteFile INTERRUPT = new RemoteFile(serverHome, Constants.INTERRUPT);
        RemoteFile ABORTED = new RemoteFile(serverHome, Constants.ABORTED);
        //--
        ShowMessage1("Создание файла INTERRUPT..");
        user.connection.writeToFile("", INTERRUPT);
        ShowMessage1("Ожидание остановки сервера тестирования");
        do {
            Utils_.sleep(1000);
            ShowMessage2("Проверка признака остановки..");
        } while (!user.connection.Exists(ABORTED));
        //--
        ShowMessage2("");
        ShowMessage1("Журнализация");
        RemoteFile AbortLog = new RemoteFile(serverHome, Constants.ABORTED +
                "_by_" +
                Global.mainModule.getAccount().email +
                "_" +
                new Date().toString()
        );
        user.connection.sftpChannel.rename(ABORTED.full_name, AbortLog.full_name);
    }
}
