package com.sagacloud.endpoints.mng;

import com.sagacloud.dict.*;
import com.sagacloud.endpoints.Dictionary;
import com.sagacloud.json.JSONObject;
import com.sagacloud.json.JSONValue;
import com.sagacloud.utils.InfoCodeUtil;
import com.sagacloud.utils.OssUtils;
import com.sagacloud.utils.ZillionAgentProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

/**
 * Created by wushengzong on 5/9/17.
 */
@Path("mng")
public class SuperMng {
    private static final Logger log = LoggerFactory.getLogger(SuperMng.class);

    @Path("/dontdoit")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String execute(@Context HttpHeaders headers, String jsonStr) {
//        String ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko)
//        Chrome/56.0.2924.87 Safari/537.36";
//        if (!headers.getHeaderString(HttpHeaders.USER_AGENT).equals(ua)) {
//            return "{}";
//        }
        JSONObject json = (JSONObject) JSONValue.parse(jsonStr);
//        json.put("Database", OssUtils.getProperty("database.name"));
        log.info(json.toString());
        try {
            return ZillionAgentProvider.GetAgent().Query(json).toString();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

    @Path("/clear_cache")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String clearCache() {

        Dictionary.clearCache();
        InfoCodeUtil.clearCache();
        InfosUtil.clearCache();
        TableMap.clearCache();
        EqStaticInfo.init();
        RelStaticInfo.init();
        GeoStaticInfo.init();
        log.info("dictionary and infoCode cache cleared");
        JSONObject json = new JSONObject();
        json.put("Result", "success");
        json.put("ResultMsg", "dictionary and infoCode cache cleared");
        return json.toString();

    }
}

