package com.demo.mongodb;

import com.alibaba.fastjson.JSONObject;
import com.demo.constant.ResultStatus;
import com.demo.entity.po.BusinData;
import com.demo.entity.po.ExceptionRecord;
import com.demo.entity.vo.DeviceConfVo;
import com.demo.entity.vo.DeviceIdVo;
import com.demo.entity.vo.SensorVo;
import com.demo.service.DeviceService;
import com.demo.util.FileEncryptor;
import com.demo.util.ResultObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/")
public class DevicecheatController {

    private final Logger logger = LoggerFactory.getLogger(DevicecheatController.class);

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private DeviceService deviceService;


    /**
     *该接口主要负责在SDK所在的APP启动时，由服务器想SDK下发配置信息，
     * 包括风险软件列表、热更新、白名单等信息。
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deviceconf", method = RequestMethod.GET)
    @ResponseBody
    public ResultObject deviceconf(@ModelAttribute DeviceConfVo deviceConf) throws Exception {
        String result = deviceService.getDeviceConf(deviceConf);
        if(result == null){
            return new ResultObject(ResultStatus.FAIL);
        }
        return new ResultObject(result);
    }


    /**
     * 该接口主要负责在SDK所在的APP启动时，采集设备的硬件信息，
     * 同时由服务器生成唯一ID并返回。
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deviceid",method = RequestMethod.POST)
    @ResponseBody
    public ResultObject deviceid(@RequestBody DeviceIdVo deviceIdVo) throws Exception {
        String result = deviceService.getDeviceId(deviceIdVo);
        if(result == null){
            return new ResultObject(ResultStatus.FAIL);
        }
        return new ResultObject(result);
    }


    /**
     * 传感器信息上报接口
     *     该接口主要负责在APP运行期间，定时的上报设备传感器或者状态信息
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/sensor", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject sensor(@RequestBody SensorVo sensorVo) throws Exception {
        boolean result = deviceService.reciveSensorInfo(sensorVo);
        if(!result){
            return new ResultObject(ResultStatus.FAIL);
        }
        return new ResultObject();
    }


    /**
     * 异常日志上报接口
     *
     * 该接口主要功能是接收异常日志，帮助SDK开发者定位异常代码
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/exception", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject exception(@RequestBody ExceptionRecord exceptionRecord) throws Exception {

        this.mongoTemplate.insert(exceptionRecord);
        return new ResultObject(ResultStatus.SUCCESS);
    }


    @RequestMapping(value = "/encryptorFile", method = RequestMethod.GET)
    @ResponseBody
    public ResultObject deviceconf() throws Exception {
        String sourceFilePath = "/data/application/deviceCheck/updateJar/update.jar";
        String destFilePath = "/data/application/deviceCheck/updateJar/encryptorFile/update.jar";
//        String sourceFilePath = "C:\\Users\\reyun\\Desktop\\update.jar";
//        String destFilePath = "C:\\Users\\reyun\\Desktop\\update_2.jar";
        String key = "5662e7c3d61fd5e0e67acd8bace6d1b0";
        FileEncryptor.decryptFile(key,sourceFilePath,destFilePath);
        return new ResultObject(ResultStatus.SUCCESS);
    }

    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    @ResponseBody
    public ResultObject downloadFile(HttpServletResponse response, String fileName) throws Exception {
        String sourceFilePath = "/data/application/deviceCheck/updateJar/encryptorFile/update.jar";
//        String sourceFilePath = "C:\\Users\\reyun\\Desktop\\update_2.jar";
        FileEncryptor.downloadFileToPath(sourceFilePath,fileName, response);
        return new ResultObject(ResultStatus.SUCCESS);
    }

    @RequestMapping(value = "/getIp", method = RequestMethod.GET)
    @ResponseBody
    public ResultObject getIp(HttpServletRequest request) throws Exception {
        String ip = deviceService.getIpAddr(request);
        Map<String, Object> result = new HashMap<>();
        result.put("ip",ip);
        return new ResultObject(result);
    }


    /**
     *
     * 接收上传的数据：
     *   "stBusinData":{
     * 	        "app_id":	"11111111111", 		//String 创建产品时获得的32位字符长度的AppID
     * 	        "user_id":	"22222222222", 		//String 账号标识识，支持英文、数字、下划线
     * 	         "user_name":	"33333333333"		//String 账号名称，该值不存在时请传user_id
     *    }
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/businData", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject businData(@RequestBody BusinData data) throws Exception {
        logger.info("/businData : {}" ,  data.toString());
        if(data == null || StringUtils.isEmpty(data.getStBusinData())){
            return new ResultObject(ResultStatus.FAIL);
        }
        deviceService.insertBusinData(data);

        return new ResultObject(ResultStatus.SUCCESS);
    }




}
