package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.BackBusiOrderAsynService;
import com.chinacreator.video.restful.service.ChannelSyncService;
import com.chinacreator.video.restful.service.SmsCheckService;
import com.chinacreator.video.restful.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 视频审核管理中台搭建需求
 * zhengrong.yan
 * 20210628
 */
@RestController
public class SmsCheckController {

    private static Logger logger = Logger.getLogger(String.valueOf(SmsCheckController.class));

    private static Logger log = Logger.getLogger("smscheck");
    private static Logger log_img = Logger.getLogger("imgcheck");
    private static Logger log_audio = Logger.getLogger("audiocheck");
    private static Logger log_video = Logger.getLogger("videocheck");
    private static Logger log_callback = Logger.getLogger("callback");
    private static Logger log_health = Logger.getLogger("health");


    @Autowired
    private SmsCheckService service;

    @Autowired
    private BackBusiOrderAsynService backBusiOrderAsynService;

    @Autowired
    private ChannelSyncService syncService;


    /**
     * POST方式办理业务
     *短信检测
     * @param orderInfo
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/smscheck", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> order(@RequestBody(required = false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "smscheck");
        logmap.put("reqData", orderInfo);
        orderInfo.setInvokeface("smscheck");
        String resultcode = "";
        String errorinfo = "";
        net.sf.json.JSONObject data = null;
        try {
            orderInfo.setTimes(System.currentTimeMillis() + "");
            service.busiCheck(orderInfo,0);
            data = service.busiOper_sms(orderInfo);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(data);
            return outpipe;
        }
    }

    /**
     * POST方式办理业务
     *图片检测
     * @param orderInfo
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/imgcheck", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> imgcheck(@RequestBody(required = false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "imgcheck");
        logmap.put("reqData", orderInfo);
        orderInfo.setInvokeface("imgcheck");
        String resultcode = "";
        String errorinfo = "";
        net.sf.json.JSONObject data = null;
        try {
            orderInfo.setTimes(System.currentTimeMillis() + "");
            service.busiCheck(orderInfo,1);
            data = service.busiOper_img(orderInfo);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log_img.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(data);
            return outpipe;
        }
    }

    /**
     * POST方式办理业务
     *音频检测
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/audiocheck", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> audiocheck(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "audiocheck");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        net.sf.json.JSONObject data = null;
        try {
            JSONObject inputObj = new JSONObject().parseObject(params);
            OrderInfo orderInfo = service.setBean(inputObj,1);
            orderInfo.setInvokeface("audiocheck");
            orderInfo.setTimes(System.currentTimeMillis() + "");
            service.busiCheck(orderInfo, 1);
            data = service.busiOper_audio(inputObj);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log_audio.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(data);
            return outpipe;
        }
    }

    /**
     * POST方式办理业务
     *视频检测
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/videocheck", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> videocheck(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "videocheck");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        net.sf.json.JSONObject data = null;
        try {
            JSONObject inputObj = new JSONObject().parseObject(params);
            OrderInfo orderInfo = service.setBean(inputObj,1);
            orderInfo.setTimes(System.currentTimeMillis() + "");
            orderInfo.setInvokeface("videocheck");
            service.busiCheck(orderInfo,1);
            data = service.busiOper_video(inputObj);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log_video.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(data);
            return outpipe;
        }
    }

    /**
     * POST方式办理业务
     *音频回调 接口
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/audioCallback", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> audioCallback(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "audioCallback");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        String data = "";
        log_callback.info("----callback-----audioCallback");
        try {
            JSONObject inputObj = new JSONObject().parseObject(params);
            service.busiOper_callback(inputObj,0);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log_callback.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(data);
            return outpipe;
        }
    }

    /**
     * POST方式办理业务
     *视频回调接口
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/videoCallback", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> videoCallback(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "videoCallback");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        log_callback.info("----callback-----videoCallback");
        try {
            JSONObject inputObj = new JSONObject().parseObject(params);
            service.busiOper_callback(inputObj,1);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log_callback.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            return outpipe;
        }
    }

    /**
     * POST方式办理业务
     *心跳接口
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/health", method = RequestMethod.GET)
    public JSONObject heath(HttpServletRequest request) throws Exception {

        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        OrderInfo orderInfo = new OrderInfo();
        logmap.put("forward", "health");
        logmap.put("reqData", orderInfo);
        System.out.println(request.getParameter("channel"));
        System.out.println(request.getParameter("signature"));
        orderInfo.setTimestamp("1627541210");
        orderInfo.setInvokeface("health");
        orderInfo.setChannel(request.getParameter("channel"));
        orderInfo.setSignature(request.getParameter("signature"));

        String resultcode = "";
        String errorinfo = "";
        log_health.info("-------心跳接口开始-----");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status","UP");
        try {
            service.busiCheck(orderInfo,1);
            resultcode = "0";
            errorinfo = "";
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.getMessage();
            }
            jsonObject.put("status","ERROR");
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log_health.info(JSON.toJSONString(logmap));
            log_health.info("-------心跳接口end-----");
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            outpipe.setData(jsonObject);
            return jsonObject;
        }
    }
}
