package com.gyee.wisdom.alarm.sharding.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.gyee.wisdom.alarm.sharding.RSA.RSAService;
import com.gyee.wisdom.alarm.sharding.entity.*;
import com.gyee.wisdom.alarm.sharding.mapper.*;
import com.gyee.wisdom.alarm.sharding.service.DeviceService;
import com.gyee.wisdom.alarm.sharding.util.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @descrition: 页面基本信息
 * @author:Wanghs
 * @date:2019-12-06
 */
@Slf4j
@RestController
@RequestMapping("/info")
@CrossOrigin
@Api(tags = {"获取基础数据，如风场，风机，测点等"})
public class InfoController {

    @Autowired
    private WindPowerStationMapper windPowerStationMapper;

    @Autowired
    private WindturbineMapper windturbineMapper;

    @Autowired
    private TestingPointAIMapper testingPointAIMapper;

    @Autowired
    private TestingPointDIMapper testingPointDIMapper;

    @Autowired
    private WindturbinePartsMapper windturbinePartsMapper;

    @Autowired
    private ElectricalTestingPointAIMapper electricalTestingPointAIMapper;

    @Autowired
    private ElectricalTestingPointDIMapper electricalTestingPointDIMapper;

    @Autowired
    private WarningTypeMapper warningTypeMapper;

    @Autowired
    private WarningClassifyMapper warningClassifyMapper;

    @Autowired
    private RSAService rsaService;

    @Autowired
    private DeviceService deviceService;

    @RequestMapping(value = "/station")
    @ResponseBody
    @ApiOperation("获取风场")
    public List<WindPowerStation> getAllWindStation(@RequestParam(value = "id", required = false) String id) {

        if (StringUtils.isBlank(id)) {
            id = "_FDC";
        }
        List<WindPowerStation> lst = windPowerStationMapper.getStationAll(id);
        return lst;
    }

    @RequestMapping(value = "/station/all")
    @ResponseBody
    @ApiOperation("获取风场和广电场")
    public List<WindPowerStation> getAllStation(@RequestParam(value = "id", required = false) String id) {
        List<WindPowerStation> lst = windPowerStationMapper.getStationAll(id);
        return lst;
    }


    @RequestMapping(value = "/windturbine")
    @ResponseBody
    public List<Windturbine> getWindturbineByStation(@RequestParam(value = "stationId", required = false) String stationId) {
        List<Windturbine> lst = windturbineMapper.selectWindturbineByCondition2(stationId);
        return lst;
    }

    @RequestMapping(value = "/testing_point_ai")
    @ResponseBody
    public List<TestingPointAI> getTestingPointAIByStationIdAndModelId(
            @RequestParam(value = "stationId") String stationId,
            @RequestParam(value = "modelId") String modelId
    ) {

        List<TestingPointAI> lst = new ArrayList<>();
        if (StringUtils.isNotBlank(stationId) && StringUtils.isNotBlank(modelId)) {
            String stationStr = stationId.replace("_FDC", "").replace("_GDC", "").trim();
            lst = testingPointAIMapper.selectByStationAndModel(stationStr, modelId);
        }
        return lst;
    }

    @RequestMapping(value = "/testing_point_di")
    @ResponseBody
    public List<TestingPointDI> getTestingPointDIByStationIdAndModelId(
            @RequestParam(value = "stationId", required = false) String stationId,
            @RequestParam(value = "modelId", required = false) String modelId
    ) {
        List<TestingPointDI> lst = new ArrayList<>();
        if (StringUtils.isNotBlank(stationId) || StringUtils.isNotBlank(modelId)) {
            String stationStr = "";
            if (StringUtils.isNotBlank(stationId))
                stationStr = stationId.replace("_FDC", "").replace("_GDC", "").trim();
            lst = testingPointDIMapper.selectByStationAndModel(stationStr, modelId);
        }
        return lst;

    }

    @RequestMapping(value = "/electrical_point_ai")
    @ResponseBody
    public List<ElectricalTestingPointAI> getElectricaTestingPointAIByStationId(@RequestParam(value = "stationId") String stationId) {
        return electricalTestingPointAIMapper.selectPointByStation(stationId);
    }

    @RequestMapping(value = "/electrical_point_di")
    @ResponseBody
    public List<ElectricalTestingPointDI> getElectricaTestingPointDIByStationId(@RequestParam(value = "stationId") String stationId) {
        return electricalTestingPointDIMapper.selectPointByStation(stationId);
    }


    @RequestMapping(value = "/windturbine_parts")
    public List<WindturbineParts> getWindturbineParts() {
        List<WindturbineParts> lst = new ArrayList<>();
        try {
            lst = windturbinePartsMapper.selectAllWindturbineParts();

        } catch (Exception ex) {
            log.error("获取风机部件信息失败" + ex.getMessage() + ex.getStackTrace());
        } finally {
            return lst;
        }
    }

    @RequestMapping(value = "/warning_type")
    public List<WarningType> getWarningType() {
        List<WarningType> lst = new ArrayList<>();
        try {
            lst = warningTypeMapper.getAll();

        } catch (Exception ex) {
            log.error("获取报警种类信息失败" + ex.getMessage() + ex.getStackTrace());
        } finally {
            return lst;
        }
    }

    @RequestMapping(value = "/warning_classify")
    public List<WarningClassify> getWarningClassify() {
        List<WarningClassify> lst = new ArrayList<>();
        try {
            lst = warningClassifyMapper.getAll();

        } catch (Exception ex) {
            log.error("获取报警分类信息失败" + ex.getMessage() + ex.getStackTrace());
        } finally {
            return lst;
        }
    }

    @RequestMapping(value = "/publickey")
    public String getPublicKey() throws Exception {
        //获得密钥对Map
        Map<String, byte[]> keyMap = rsaService.getKeyMap();
        //获得公钥
        String publicKeyStr = rsaService.encryptBASE64(keyMap.get("publicKey"));
        //获得私钥
        String privateKeyStr = rsaService.encryptBASE64(keyMap.get("privateKey"));

        PrivateKey privateKey = rsaService.restorePrivateKey(rsaService.decryptBASE64(privateKeyStr));
        PublicKey publicKey = rsaService.restorePublicKey(rsaService.decryptBASE64(publicKeyStr));
        RSAService.keyMap.put(publicKey, privateKey);
        System.out.println(publicKeyStr);
        return publicKeyStr;
    }


    @PostMapping(value = "/sendkey")
    @ResponseBody
    public ResponseWrapper sendWebPublickey(@RequestBody String webPublicKey) {


        System.out.println(webPublicKey);
        PublicKey publicKey = RSAService.restorePublicKey(webPublicKey.getBytes());
        byte[] data = RSAService.encryptionByPublicKey(publicKey, "wanghs的加密信息".getBytes());
        ResponseWrapper wrapper = ResponseWrapper.success("返回数据", data);

        return wrapper;


    }

    @RequestMapping(value = "/point")
    @ResponseBody
    public JSONObject getPointList(@RequestParam(value = "deviceId", required = true) String deviceid,
                                  @RequestParam(value = "structcode", required = false) String structCode,
                                  @RequestParam(value = "stationId", required = true) String stationId,
                                  @RequestParam(value = "windturbineId", required = true) String windturbineId,
                                  @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                  @RequestParam(value = "pageNum", required = false) Integer pageNum
    ) {

        if (pageSize==null||pageSize == 0)
            pageSize = 100;
        if (pageNum==null||pageNum == 0)
            pageNum = 1;
        JSONObject testingPoint = deviceService.getTestingPointByStructCode(deviceid, structCode, windturbineId, pageSize, pageNum);
        return testingPoint;


    }
}
