package com.yihu.hos.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.hos.common.configuration.ApplicationConfiguration;
import com.yihu.hos.common.configuration.ArbiterConfiguration;
import com.yihu.hos.core.net.IPChoiceUtils;
import org.apache.camel.util.InetAddressUtil;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

/**
 * @created Airhead 2016/8/1.
 */
@Component("brokerServerService")
public class BrokerServerService implements DisposableBean {
    @Autowired
    private ApplicationConfiguration applicationConfiguration;
    @Autowired
    private ArbiterConfiguration arbiterConfiguration;
    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("hostName", hostName);
            objectNode.put("hostAddress", hostAddress);
            objectNode.put("port", port);
            objectNode.put("enable", true);
            String brokerServer = objectMapper.writeValueAsString(objectNode);

            List<NameValuePair> nameValuePairList = new ArrayList<>();
            nameValuePairList.add(new BasicNameValuePair("brokerServer", brokerServer));

            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(arbiterConfiguration.getServer() + "/brokerServer");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, Consts.UTF_8));
            CloseableHttpResponse response = httpclient.execute(httpPost);
            response.close();

        } 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();
    }
}
