package com.dapr;


import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone;

public class CommonController {


    protected static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    private final static String saveBodyTmpl="{\"content\": \"%s\"}";

    protected String setBody(byte[] body, Map<String, String> headers) {

        String message = body == null ? "" : new String(body, StandardCharsets.UTF_8);

        Calendar utcNow = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        String utcNowAsString = DATE_FORMAT.format(utcNow.getTime());


        // Handles the request by printing message.
        System.out.println("Server: " + message);
        return message;
    }

    public static String doPost(String sql) throws IOException {
        String  httpPort = "3500";
        String resultUrl = "http://192.168.133.129:" + httpPort + "/v1.0/publish/talents-database-rabbitmq-pubsub/deathStarStatus";

        // 通过httpclient发送post请求
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(resultUrl);// 创建httpPost

        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");

        StringEntity entity = new StringEntity(sql, "utf-8");

        //System.out.println("resultUrl: " + resultUrl);

        httpPost.setEntity(entity);

        CloseableHttpResponse response = null;

        try {
            response = httpclient.execute(httpPost);


        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) throws InterruptedException {

        Thread sendThread = new Thread(new Runnable() {
            public void run() {
                int i=0;
                while(!Thread.interrupted()){
                    try {
                        Thread.sleep(2000);
                        i++;
                        String str = "第1个线程发布--"+i;
                        String body=String.format(saveBodyTmpl,str);
                        doPost(body);
                        System.out.println(str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        Thread sendThread1 = new Thread(new Runnable() {
            public void run() {
                int i=0;
                while(!Thread.interrupted()){
                    try {
                        Thread.sleep(2000);
                        i++;
                        String str = "第2个线程发布--"+i;
                        String body=String.format(saveBodyTmpl,str);
                        doPost(body);
                        System.out.println(str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        sendThread.start();
        sendThread1.start();
        //Thread.sleep(5*60*60*1000);
    }
}
