package com.tcl.ah.module.serverinfo.data;

public class ServerInfo {
    /***
     * {
     *     "httpinfo":{
     *         "apihost":"https://192.168.5.202:8080"
     *     },
     *     "mqttinfo":{
     *         "host":"192.168.5.202",
     *         "port":"1883",
     *         "user":"username",
     *         "pwd":"password",
     *         "hblive":"60",
     *         "expire":"3600"
     *     }
     * }
     */

    public HttpInfo httpInfo = new HttpInfo();
    public MqttInfo mqttInfo = new MqttInfo();


    public boolean isQualified() {
        return isHttpQualified() && isMqttQualified();

    }

    public boolean isHttpQualified() {
        if (httpInfo == null) {
            return false;
        }
        return httpInfo.isQualified();
    }

    public boolean isMqttQualified() {
        if (mqttInfo == null) {
            return false;
        }
        return mqttInfo.isQualified();
    }

    @Override
    public String toString() {
        return "{" +
                "httpInfo=" + httpInfo +
                ", mqttInfo=" + mqttInfo +
                '}';
    }

    public static class HttpInfo {
        public String apiHost;

        public boolean isQualified() {
            if (!isEmpty(apiHost)) {
                return true;
            }
            return false;
        }

        @Override
        public String toString() {
            return "{" +
                    "apiHost='" + apiHost + '\'' +
                    '}';
        }
    }

    public static class MqttInfo {
        public String host;
        public int port = ServerInfoIni.MQTT_PORT;
        public String user;
        public String password;
        public int heartbeatLive = ServerInfoIni.HEARTBEAT_LIVE;
        public int expire = ServerInfoIni.EXPIRE;

        public boolean isQualified() {
            if (isEmpty(host) || isEmpty(user) || isEmpty(password)) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "{" +
                    "host='" + host + '\'' +
                    ", port=" + port +
                    ", user='" + user + '\'' +
                    ", password='" + password + '\'' +
                    ", heartbeatLive=" + heartbeatLive +
                    ", expire=" + expire +
                    '}';
        }
    }

    private static boolean isEmpty(String val) {
        if (val == null || val.trim().length() == 0) {
            return true;
        }
        return false;
    }
}
