package com.yihu.hos.broker.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.hos.broker.configurations.ApplicationConfiguration;
import com.yihu.hos.broker.configurations.ArbiterConfiguration;
import com.yihu.hos.core.http.HTTPResponse;
import com.yihu.hos.core.http.HttpClientKit;
import com.yihu.hos.core.net.IPChoiceUtils;
import org.apache.camel.util.InetAddressUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @created Airhead 2016/8/1.
 */
@Component("brokerServerService")
public class BrokerServerService implements DisposableBean {
    private static boolean registered = false;
    @Autowired
    private ApplicationConfiguration applicationConfiguration;
    @Autowired
    private ArbiterConfiguration arbiterConfiguration;
    @Value("${hos.tenant.name}")
    private String tenant;

    private String hostName;
    private String hostAddress;
    private int port;

    public void brokerServerOnline() {
        try {
            hostName = InetAddressUtil.getLocalHostName();
            hostAddress = IPChoiceUtils.getSourceIP();
            port = applicationConfiguration.getPort();
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("tenant", tenant);
            objectNode.put("hostName", hostName);
            objectNode.put("hostAddress", hostAddress);
            objectNode.put("port", port);
            objectNode.put("enable", true);
            objectNode.put("registered", registered);
            String brokerServer = objectMapper.writeValueAsString(objectNode);

            Map<String, String> params = new HashMap<>();
            params.put("brokerServer", brokerServer);
            HTTPResponse httpResponse = HttpClientKit.post(arbiterConfiguration.getServer() + "/brokerServer", params);
            if (httpResponse.getStatusCode() == 200) {
                registered = true;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * brokerServer下线时通知
     */
    public void brokerServerOffline() {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("hostName", hostName);
            objectNode.put("hostAddress", hostAddress);
            objectNode.put("port", port);
            objectNode.put("enable", false);
            String brokerServer = objectMapper.writeValueAsString(objectNode);

            URI uri = new URIBuilder(arbiterConfiguration.getServer() + "/brokerServer")
                    .addParameter("brokerServer", brokerServer)
                    .build();

            HttpDelete httpDelete = new HttpDelete(uri);
            CloseableHttpClient httpclient = HttpClients.createDefault();
            CloseableHttpResponse response = httpclient.execute(httpDelete);
            response.close();

        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void destroy() throws Exception {
//        this.brokerServerOffline();
    }
}
