package com.fri.http.rest.resources;

import com.fri.http.rest.config.YXDefaultServerConfig;
import com.fri.http.rest.connections.HttpClientPool;
import com.fri.http.rest.logging.YXLogging;
import com.fri.http.rest.response.YXJSONResponse;
import com.fri.http.rest.server.YXHttpRestServer;
import net.sf.json.JSONObject;
import org.eclipse.jetty.server.ServerConnector;
import org.omg.CORBA.Any;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Random;
import java.util.logging.Logger;


/**
 * Created by zhangdahui on 4/27/17.
 */
@Path("v1")
public class YXRestResources {

    private String version = "ver1";
    private Logger logger = YXLogging.getLogger(YXDefaultServerConfig.LOGGING_NAME_REQUEST_SERVER);


    @GET
    @Path("status")
    @Produces(MediaType.APPLICATION_JSON)
    //接收带有查询参数的GET的请求
    public String hello(@Context HttpServletRequest req, @Context HttpServletResponse resp, @QueryParam("agent") String agent) throws Exception {
        resp.setCharacterEncoding("UTF-8");
        YXJSONResponse yxResp = new YXJSONResponse();

        HashMap dataMap = new HashMap<String, Any>();

        dataMap.put("ApiVersion", version);
        dataMap.put("ThreadInProcess", YXHttpRestServer.jettyServer.getThreadPool().getThreads());
        dataMap.put("CurrentQueueSize", ((ServerConnector)YXHttpRestServer.jettyServer.getConnectors()[0]).getAcceptQueueSize());
        dataMap.put("AgentCode", agent);
        dataMap.put("BusinessSerialNo", "SAXD1351231512312");
        dataMap.put("RandomNo", String.valueOf(new Random().nextInt(999999999)+5));

        String jsonStr = yxResp.outPutSuccess(dataMap).toJSONString();
        logger.info(req.getRemoteHost() + " => " + jsonStr);

        return jsonStr;

    }


    @POST
    @Path("postBody")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    //接收带有 JSON Body 的POST请求，非Form模式
    public String postWithBody(String jsonStr) throws Exception {
        YXJSONResponse sta = new YXJSONResponse();
        JSONObject json = JSONObject.fromObject(jsonStr);
        return sta.outPutSuccess(json).toJSONString();
    }

    @GET
    @Path("getTest")
    @Produces(MediaType.APPLICATION_JSON)
    //GET请求，同时向其他服务器发送请求
    public String getTest(@Context HttpServletRequest req, @Context HttpServletResponse resp) throws Exception {
        YXJSONResponse sta = HttpClientPool.getRequest("http://www.baidu.com");
        return sta.toJSONString();
    }

    @GET
    @Path("postTest")
    @Produces(MediaType.APPLICATION_JSON)
    //GET请求，同时向远程发送POST请求
    public String postTest(@Context HttpServletRequest req, @Context HttpServletResponse resp) throws Exception {
        String body = "[{\n" +
                "      \"name\": \"archive_file_search_1\",\n" +
                "      \"timestamp\": 1359786400000,\n" +
                "      \"value\": 321,\n" +
                "      \"tags\": {\"host\": \"server3\"}\n" +
                "}]";
        YXJSONResponse sta = HttpClientPool.postRequest(YXDefaultServerConfig.KAIROSDB_REST_ADDRESS, body);
        return sta.toJSONString();
    }

}