package _VisualDVM.Passes.Server;
import Common.Passes.Pass;
import _VisualDVM.Repository.Server.RepositoryServer;
import _VisualDVM.Repository.Server.SafeServerExchangeUnit;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;

import java.io.Serializable;
public abstract class ClientPass<S extends RepositoryServer, T> extends Pass<T> {
    protected RepositoryPass request;
    protected boolean isSafe() {
        return true;
    }
    @Override
    protected boolean needsAnimation() {
        return false;
    }
    protected boolean requestNeedsAnimation() {
        return true;
    }
    @Override
    public String getButtonText() {
        return "";
    }
    protected abstract S getServer();
    protected boolean SendRequest(ServerCode code_in, String arg_in, Serializable object_in) {
        request = null;
        request = new RepositoryPass(getServer()) {
            @Override
            protected boolean needsAnimation() {
                return requestNeedsAnimation();
            }
            @Override
            public String getDescription() {
                return code_in.getDescription();
            }
            @Override
            protected int getTimeout() {
                return 0;
            }
            @Override
            protected void ServerAction() throws Exception {
                if (isSafe())
                    Command(new SafeServerExchangeUnit(code_in, arg_in, object_in));
                else
                    unsafe_command(new ServerExchangeUnit_2021(code_in, arg_in, object_in)); //только для отдельных команд.
                target = server_response.object;
            }
            @Override
            protected boolean validate() {
                return Log.isEmpty();
            }
        };
        return request.Do();
    }
    protected boolean SendRequest(ServerCode code_in, String arg_in) {
        return SendRequest(code_in, arg_in, null);
    }
    protected boolean SendRequest(ServerCode code_in) {
        return SendRequest(code_in, "", null);
    }
}
