package _VisualDVM.Passes.All;
import Common.MainModule_;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Constants;
import _VisualDVM.Global;
import _VisualDVM.GlobalData.Machine.Machine;
import _VisualDVM.GlobalData.RemoteFile.RemoteFile;
import _VisualDVM.GlobalData.User.User;
import _VisualDVM.Passes.SSH.ConnectionPass;

import java.util.Vector;
public class RemoteInitialiseUser extends ConnectionPass<RemoteFile> {
    RemoteFile modulesDirectory;
    @Override
    protected boolean needsAnimation() {
        return true;
    }
    @Override
    protected boolean canStart(Object... args) {
        return MainModule_.instance.getDb().getTable(User.class).getUI().CheckCurrent(Log);
    }
    @Override
    protected void Connect() throws Exception {
        machine = MainModule_.instance.getDb().getTable(Machine.class).getUI().getCurrent();
        user = MainModule_.instance.getDb().getTable(User.class).getUI().getCurrent();
        super.Connect();
    }
    @Override
    protected void ServerAction() throws Exception {
        String workspace_name = Utils_.getDateName("visual_sapfor_workspace");
        ShowMessage1("Создание рабочего пространства...");
        target = new RemoteFile(user.connection.sftpChannel.getHome(), workspace_name);
        user.connection.sftpChannel.mkdir(target.full_name);
        Vector<RemoteFile> subdirectories = new Vector<>();
        subdirectories.add(new RemoteFile(target, "projects"));
        subdirectories.add(modulesDirectory = new RemoteFile(target, "modules"));
        subdirectories.add(new RemoteFile(target, "tests"));
        //-------------------------------------
        for (RemoteFile remoteFile : subdirectories)
            user.connection.sftpChannel.mkdir(remoteFile.full_name);
        //----------------------------------
        ShowMessage1("Закачка кода модулей...");
        for (String resource_name : Constants.resourses_names) {
            ShowMessage2(resource_name);
            user.connection.putResource(modulesDirectory, resource_name);
        }
        //-------------------------------------
        ShowMessage1("Сборка модулей...");
        String modules_log = user.connection.compileModules(modulesDirectory);
        if (!modules_log.isEmpty())
            throw new PassException(modules_log);
        //--------------------------------------
        RemoteFile info = new RemoteFile(target, Global.mainModule.getAccount().email);
        user.connection.writeToFile("", info);
    }
}
