package com.engine.htsc.image.web;


import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.image.service.ImageBase64Service;
import com.engine.htsc.image.service.impl.ImageBase64ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.MediaType;
import java.util.HashMap;
import java.util.Map;

public class ImageBase64Action {


    public ImageBase64Service getService(HttpServletRequest request, HttpServletResponse response){
        return  (ImageBase64Service) ServiceUtil.getService(ImageBase64ServiceImpl.class);
    }

    @POST
    @Path("/getbase64toimage")
    @Produces({MediaType.TEXT_PLAIN})
    public String getBase64ToImage(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            Map<String,Object> param = ParamUtil.request2Map(request);

            apidatas = getService(request,response).generate2Image(param);

            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @GET
    @Path("/gettest")
    @Produces({MediaType.TEXT_PLAIN})
    public String getTest(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String,Object> apidatas = new HashMap<String,Object>();
        try {
            apidatas.put("api_status", true);
            apidatas.put("api_errormsg", "");
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

}
