package _VisualDVM.TestingSystem.Common.MachineProcess;
import Common.CommonConstants;
import Common.Database.Objects.DBObject;
import Common.Utils.Utils_;
import _VisualDVM.*;
import _VisualDVM.TestingSystem.DVM.DVMPackage.DVMPackage;
import _VisualDVM.TestingSystem.DVM.PackageCredentials.DVMPackageCredentials;

import java.io.File;
import java.net.InetAddress;
import java.nio.file.Files;
import java.util.Vector;
public class MachineProcess extends DBObject {
    public String id = "";
    public String machineAddress = "";
    public int machinePort = CommonConstants.Nan;
    public String userName = "";
    public String userPassword = "";
    public String userWorkspace = "";
    public String testingSystemRoot = "";
    public String serverName = "";
    public MachineProcess() {
    }
    public MachineProcess(MachineProcess p) {
        SynchronizeFields(p);
    }
    public MachineProcess(DVMPackage p, DVMPackageCredentials credentials) {
        machineAddress = p.machine_address;
        machinePort = p.machine_port;
        userName = p.user_name;
        userWorkspace = p.user_workspace;
        testingSystemRoot = Utils_.getHomePath();
        serverName = Global.testingServer.name;
        //-->>
        userPassword = credentials.userPassword;
        //-->>
        id = Utils_.getDateName(machineAddress + "_" + machinePort + "_" + userName);
    }
    @Override
    public Object getPK() {
        return id;
    }
    @Override
    public void SynchronizeFields(DBObject src) {
        super.SynchronizeFields(src);
        MachineProcess p = (MachineProcess) src;
        machineAddress = p.machineAddress;
        machinePort = p.machinePort;
        userName = p.userName;
        userPassword = p.userPassword;
        userWorkspace = p.userWorkspace;
        testingSystemRoot = p.testingSystemRoot;
        serverName = p.serverName;
    }
    public String getUniqueKey() {
        Vector<String> res = new Vector<>();
        res.add(machineAddress);
        res.add(String.valueOf(machinePort));
        res.add(userName);
        res.add(userWorkspace);
        return String.join("_", res);
    }
    public File getWorkspace() {
        return new File(Global.MachinesDirectory, id);
    }
    public File getStartedFile() {
        return new File(getWorkspace(), CommonConstants.STARTED);
    }
    public File getAbortedFile() {
        return new File(getWorkspace(), CommonConstants.ABORTED);
    }
    //---
    public boolean isAborted() {
        File aborted = getAbortedFile();
        return aborted.exists();
    }
    public boolean isStarted() {
        File started = getStartedFile();
        return started.exists();
    }
    public boolean isLocal() {
        boolean local = false;
        try {
            InetAddress address = InetAddress.getByName(machineAddress);
            InetAddress localAddress = InetAddress.getByName(Constants.ServerAddress);
            local = localAddress.getHostAddress().equals(address.getHostAddress());
        } catch (Exception ex) {
            Utils_.MainLog.PrintException(ex);
        }
        return local;
    }
    //--
    public void Start() {
        try {
            File workspace = getWorkspace();
            Utils_.CheckAndCleanDirectory(workspace);
            //копирование визуализатора
            File src = new File(Utils_.getHomeDirectory(), Constants.ApplicationFileName);
            File supervisor = new File(workspace, "VisualSapfor.jar");
            //здесь наоборот дст в начале
            Files.createSymbolicLink(supervisor.toPath(), src.toPath());
            //создание настроек
            VisualDVMProperties properties = new VisualDVMProperties();
            properties.Mode = Mode.MachineQueue;
            Utils_.jsonToFile(properties, new File(workspace, "properties"));
            Vector<String> args = new Vector<>();
            args.add(Utils_.DQuotes(machineAddress));
            args.add(Utils_.DQuotes(machinePort));
            args.add(Utils_.DQuotes(userName));
            args.add(Utils_.DQuotes(userPassword));
            args.add(Utils_.DQuotes(userWorkspace));
            args.add(Utils_.DQuotes(testingSystemRoot));
            args.add(Utils_.DQuotes(Global.testingServer.name));
            //--
            Utils.startScript(workspace, workspace,
                    "start",
                    "java -jar VisualSapfor.jar " +
                            String.join(" ", args) + " 1>out.txt 2>err.txt");
            //---
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
