package Visual_DVM_2021.Passes.All;
import Common.Constants;
import Common.Global;
import Common.Utils.Utils;
import GlobalData.RemoteFile.RemoteFile;
import ProjectData.LanguageName;
import Repository.Server.ServerCode;
import Repository.Server.ServerExchangeUnit_2021;
import SapforTestingSystem.ServerSapfor.ServerSapfor;
import Visual_DVM_2021.Passes.PassCode_2021;
import Visual_DVM_2021.Passes.SSH.ConnectionPass;
import Visual_DVM_2021.Passes.TestingSystemPass;

import java.util.Date;
public class InstallServerSapfor extends ConnectionPass<Object> {
    boolean result;
    ServerSapfor serverSapfor;
    String version_text;
    @Override
    public String getIconPath() {
        return "/icons/DownloadAll.png";
    }
    @Override
    public String getButtonText() {
        return "";
    }
    @Override
    public void Connect() throws Exception {
        machine = Constants.repository_machine;
        user = Constants.repository_user;
        super.Connect();
    }
    @Override
    protected boolean canStart(Object... args) {
        result = false;
        version_text = "";
        serverSapfor = null;
        return true;
    }
    @Override
    protected boolean needsInitialize() {
        return false;
    }
    @Override
    protected boolean needsAnimation() {
        return true;
    }
    @Override
    protected void ServerAction() throws Exception {
        RemoteFile testingSystemHome = new RemoteFile(sftpChannel.pwd(), "testing_system", true);
        RemoteFile repo = new RemoteFile(testingSystemHome.full_name, "Repo", true);
        RemoteFile repoSapforHome = new RemoteFile(repo.full_name + "/sapfor/experts/Sapfor_2017/_bin", true);
        RemoteFile old_bin = new RemoteFile(repoSapforHome.full_name, "Sapfor_F");
        if (Exists(repoSapforHome.full_name, "Sapfor_F"))
            sftpChannel.rm(old_bin.full_name);
        ShowMessage2("Вывод в консоли визуализатора");
        //-->>
        ShowMessage1("Синхронизация ветви DVM...");
        ShellCommand("cd " + Utils.DQuotes(repo.full_name), "svn checkout " + Constants.REPOSITORY_AUTHENTICATION + " " + Constants.DVM_REPOSITORY + "\n");
        ShowMessage1("Синхронизация ветви SAPFOR...");
        ShellCommand("cd " + Utils.DQuotes(repo.full_name), "svn checkout " + Constants.REPOSITORY_AUTHENTICATION + " " + Constants.SAPFOR_REPOSITORY + "\n");
        ShowMessage1("Сборка SAPFOR...");
        ShellCommand("cd " + Utils.DQuotes(repoSapforHome.full_name), "cmake ../", "make -j 4");
        result = Exists(repoSapforHome.full_name, "Sapfor_F");
        if (result) {
            RemoteFile sapforsDirectory = new RemoteFile(testingSystemHome.full_name, "Sapfors", true);
            //создать папку. Для того чтобы скопировать из репозитория.
            RemoteFile sapforHome = new RemoteFile(sapforsDirectory.full_name, Utils.getDateName("sapfor"));
            tryMKDir(sapforHome);
            RemoteFile repoSapforBin = new RemoteFile(repoSapforHome, "Sapfor_F");
            RemoteFile sapforBin = new RemoteFile(sapforHome, "Sapfor_F");
            copy(repoSapforBin, sapforBin);
            //-->>>
            serverSapfor = new ServerSapfor();
            serverSapfor.home_path = sapforHome.full_name;
            serverSapfor.call_command = sapforBin.full_name;
            serverSapfor.languageName = LanguageName.fortran;
            serverSapfor.buildDate = new Date().getTime();
            ///--->>
            String raw = ShellCommand(serverSapfor.getVersionCommand());
            String[] data = raw.split(" ");
            if (data.length >= 4) serverSapfor.version = data[3].replace(",", "");
            //---
            result = new TestingSystemPass() {
                @Override
                public String getDescription() {
                    return "Публикация тестовой версии";
                }
                @Override
                protected void ServerAction() throws Exception {
                    Command(new ServerExchangeUnit_2021(ServerCode.PublishObject, "", serverSapfor));
                }
            }.Do();
        }
    }
    @Override
    protected boolean validate() {
        return result;
    }
    @Override
    protected void performDone() throws Exception {
        super.performDone();
        passes.get(PassCode_2021.SynchronizeTests).Do();
    }
    @Override
    protected void showFail() throws Exception {
        Log.Writeln_("Файл [SAPFOR_F] не найден!");
    }
    @Override
    protected void showDone() throws Exception {
        super.showDone();
        Global.testingServer.db.serverSapfors.ui_.Show(serverSapfor.getPK());
    }
}

