package com.yfyy.iotx.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yfyy.iotx.InsectCons;
import com.yfyy.iotx.util.DataUtil;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.Map;

public class RestHandle extends AbstractHandler {

    private final String resIp1 = "http://8.136.98.49:8003";
    private final String desIp1 = "https://bigdata-image.oss-cn-hangzhou.aliyuncs.com";

    private final String resIp2 = "http://120.27.222.26:8003";
    private final String resIp3 = "http://cbd.yfpyx.com";
    private final String desIp2 = "https://cbdpic.oss-cn-hangzhou.aliyuncs.com";


    @Override
    public void handle(String url, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
        if (url.endsWith("/uploadPic")) {
            JSONObject json = new JSONObject();
            json.put("code", 200);
            json.put("msg", "操作成功");
            json.put("result","操作成功");

            String imei;
            String result_image;
            StringBuilder resultSB = new StringBuilder();
            String image;

            String body = getPostData(request);
            try {
                body = body.replace(" ","");
                Map<String, String> maps = (Map) JSON.parse(body);
                imei = maps.get("imei");
                result_image = maps.get("Result_image");
                image = maps.get("Image");
                System.out.println( "字符编码"+Charset.defaultCharset());
                if(!maps.get("Result").equals("0")){
                    String[] resultArr = maps.get("Result").split("#");
                    for (int i = 0; i < resultArr.length; i++) {
                        String oneStr = resultArr[i];
                        String[] codeNumArr = oneStr.split(",");
                        String oneResult = codeNumArr[0] + "," + InsectCons.CODE_NAME_MAP.get(Integer.parseInt(codeNumArr[0])) + "," + codeNumArr[1];
                        if(i == resultArr.length -1){
                            resultSB.append(oneResult);
                        }else {
                            resultSB.append(oneResult).append("#");
                        }
                    }

                    // 替换IP
                    image = image.replace(resIp1,desIp1);
                    result_image = result_image.replace(resIp1,desIp1);

                    // 替换IP
                    image = image.replace(resIp2,desIp2);
                    result_image = result_image.replace(resIp2,desIp2);

                    // 替换IP
                    image = image.replace(resIp3,desIp2);
                    result_image = result_image.replace(resIp3,desIp2);

                    // 上报图片
                    DataUtil.onlineAndReportPic(imei, image, result_image, resultSB.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            httpServletResponse.setCharacterEncoding("utf-8");
            httpServletResponse.setContentType("application/json; charset=utf-8");
            PrintWriter writer = httpServletResponse.getWriter();
            writer.write(json.toString());
            writer.flush();
            writer.close();
        }

    }


    public static String getPostData(HttpServletRequest request) {
        StringBuilder data = new StringBuilder();
        String line;
        BufferedReader reader;
        try {
            reader = request.getReader();
            while (null != (line = reader.readLine())) {
                data.append(line);
            }
        } catch (IOException e) {
            return null;
        }
        return data.toString();
    }



}