package com.bocloud.ims.service.internal.worker;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.OSCategory;
import com.bocloud.common.model.BocloudResult;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class WorkerHost {
    private Long id;
    private String host;
    private String user;
    private String pasd;
    private Integer port;
    private String task;
    private String rawName;
    private String category;
    private String connection;

    public WorkerHost() {
        this.category = OSCategory.LINUX.name();
    }

    public WorkerHost(String host, String connection) {
        this.category = OSCategory.LINUX.name();
        this.host = host;
        this.connection = connection;
    }

    public WorkerHost(String host, String user, String pasd, Integer port, OSCategory category) {
        this.category = OSCategory.LINUX.name();
        this.host = host;
        this.user = user;
        this.pasd = pasd;
        this.category = category.name();
        if (null != port && 0 < port) {
            this.port = port;
        } else {
            switch (category) {
                case WINDOWS:
                    this.port = 5986;
                    break;
                case LINUX:
                    this.port = 22;
                    break;
                case UNIX:
                    this.port = 22;
                    break;
                default:
                    this.port = 22;
            }
        }

    }

    public WorkerHost(Long id, String host, String user, String pasd, Integer port, OSCategory category) {
        this.category = OSCategory.LINUX.name();
        this.id = id;
        this.host = host;
        this.user = user;
        this.pasd = pasd;
        this.category = category.name();
        if (null != port && 0 < port) {
            this.port = port;
        } else {
            switch (category) {
                case WINDOWS:
                    this.port = 5986;
                    break;
                case LINUX:
                    this.port = 22;
                    break;
                case UNIX:
                    this.port = 22;
                    break;
                default:
                    this.port = 22;
            }
        }

    }

    public static BocloudResult<WorkerHost> parseTarget(String content) {
        BocloudResult<WorkerHost> result = null;
        if (StringUtils.isEmpty(content)) {
            result = new BocloudResult(false, "参数为空！");
        } else {
            try {
                WorkerHost target = (WorkerHost) JSONObject.parseObject(content, WorkerHost.class);
                result = new BocloudResult(true, target, "success");
            } catch (Exception var3) {
                result = new BocloudResult(false, var3.getLocalizedMessage());
            }
        }

        return result;
    }

    public static List<WorkerHost> parseTargets(String content) throws Exception {
        if (StringUtils.isEmpty(content)) {
            throw new Exception("参数为空！");
        } else {
            try {
                List<WorkerHost> targets = JSONArray.parseArray(content, WorkerHost.class);
                return targets;
            } catch (Exception var2) {
                throw new Exception("目标机器参数类型错误！", var2);
            }
        }
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getHost() {
        return this.host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUser() {
        return this.user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPasd() {
        return this.pasd;
    }

    public void setPasd(String pasd) {
        this.pasd = pasd;
    }

    public Integer getPort() {
        return this.port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getCategory() {
        return this.category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getRawName() {
        return this.rawName;
    }

    public void setRawName(String rawName) {
        this.rawName = rawName;
    }

    public String getTask() {
        return this.task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getConnection() {
        return this.connection;
    }

    public void setConnection(String connection) {
        this.connection = connection;
    }
}
