package _VisualDVM.Passes.SSH;
import Common.CommonConstants;
import Common.Utils.Utils_;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.Machine.MachineType;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.GlobalData.User.User;
import _VisualDVM.Repository.Server.RepositoryServer;

import java.util.Date;
public abstract class RepositoryServerSSHPass<S extends RepositoryServer> extends ConnectionPass<S> {
    //--
    protected RemoteFile userHome;
    protected RemoteFile serverHome;
    @Override
    public String getButtonText() {
        return "";
    }
    @Override
    protected boolean needsAnimation() {
        return true;
    }
    protected abstract S getServer();
    @Override
    protected boolean canStart(Object... args) throws Exception {
        if (!Global.mainModule.getAccount().CheckAdmin(Log))
            return false;
        if (Global.normalProperties.ServerUserPassword.isEmpty()) {
            Log.Writeln_("Не задан пароль");
            return false;
        }
        return true;
    }
    @Override
    public void Connect() throws Exception {
        userHome = null;
        serverHome = null;
        //--
        machine = new Machine("alex", Constants.ServerAddress, Constants.ServerUserSHHPort, MachineType.Server);
        user = new User(Constants.ServerUserName, Global.normalProperties.ServerUserPassword); //в пропертиес. пусть админ пользуется.
        super.Connect();
        //--
        userHome = new RemoteFile(user.connection.sftpChannel.getHome(), true);
        serverHome = new RemoteFile(userHome, getServer().getServerHomeName());
        //-
    }
    //--
    protected void StartServer() throws Exception {
        user.connection.startShellProcess(serverHome, "server_out.txt", "java -jar " +
                getServer().getServerFileName());
        //--
        RemoteFile StartLog = new RemoteFile(serverHome, CommonConstants.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, CommonConstants.INTERRUPT);
        RemoteFile ABORTED = new RemoteFile(serverHome, CommonConstants.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, CommonConstants.ABORTED +
                "_by_" +
                Global.mainModule.getAccount().email +
                "_" +
                new Date().toString()
        );
        user.connection.sftpChannel.rename(ABORTED.full_name, AbortLog.full_name);
    }
}
