package _VisualDVM.Passes.All;
import Common.MainModule_;
import Common.Passes.Pass;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.ComponentsServer.SubscriberWorkspace.SubscriberWorkspace;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.GlobalData.User.User;
import _VisualDVM.GlobalData.User.UserState;
import _VisualDVM.Passes.PassCode;
public class InitialiseUser extends Pass {
    @Override
    public String getIconPath() {
        return "/icons/InitializeUser.png";
    }
    @Override
    public String getButtonText() {
        return "";
    }
    @Override
    protected boolean canStart(Object... args) throws Exception {
        return MainModule_.instance.getDb().CheckCurrent(Log, Machine.class, User.class);
    }
    @Override
    protected void body() throws Exception {
        Machine machine = MainModule_.instance.getDb().getTable(Machine.class).getUI().getCurrent();
        User user = MainModule_.instance.getDb().getTable(User.class).getUI().getCurrent();
        switch (machine.type) {
            case Local:
                Global.mainModule.getPass(PassCode.LocalInitaliseUser).Do();
                break;
            case Server:
            case MVS_cluster:
                if (Global.mainModule.getPass(PassCode.CheckRemoteWorkspace).Do()) {
                    SubscriberWorkspace workspace = (SubscriberWorkspace) Global.mainModule.getPass(PassCode.CheckRemoteWorkspace).target;
                    if (workspace == null) {
                        if (Global.mainModule.getPass(PassCode.RemoteInitialiseUser).Do()) {
                            workspace = new SubscriberWorkspace();
                            workspace.email = Global.mainModule.getAccount().email;
                            workspace.URL = machine.getURL();
                            workspace.login = user.login;
                            workspace.path = ((RemoteFile) Global.mainModule.getPass(PassCode.RemoteInitialiseUser).target).full_name;
                            //---
                            System.out.println("==");
                            System.out.println(Utils_.Brackets(workspace.email));
                            System.out.println(Utils_.Brackets(workspace.URL));
                            System.out.println(Utils_.Brackets(workspace.login));
                            System.out.println(Utils_.Brackets(workspace.path));
                            System.out.println("==");
                            if (Global.mainModule.getPass(PassCode.PublishRemoteWorkspace).Do(workspace)) {
                                user.workspace = workspace.path;
                                user.state = UserState.ready_to_work;
                                Global.mainModule.getDb().Update(user);
                            }
                        }
                    } else {
                        //рега была. просто заносим то что там пользователю
                        user.workspace = workspace.path;
                        user.state = UserState.ready_to_work;
                        Global.mainModule.getDb().Update(user);
                    }
                }
                break;
            default:
                throw new PassException("Неопределенный тип пользователя");
        }
    }
    @Override
    protected void showFinish() throws Exception {
        Global.mainModule.getDb().users.getUI().RedrawControl();
    }
}
