package com.ozs.controller.upload;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.common.core.domain.Result;
import com.ozs.common.utils.ApiTokenUtils;
import com.ozs.common.utils.AppTokenUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.service.entity.BaseAccountManagement;
import com.ozs.service.entity.BaseDynamicManagement;
import com.ozs.service.service.BaseAccountManagementService;
import com.ozs.service.service.BaseDynamicManagementService;
import com.ozs.vo.ReqDeviceVo;
import com.ozs.vo.ReqMsgAlarmVo;
import com.ozs.vo.RespGeoHazardMonitorVo;
import com.ozs.vo.RespMsgAlarmVo;
import org.springframework.util.ObjectUtils;
import org.springframework.util.SimpleIdGenerator;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 1. 获取身份认证控制层
 *
 * @author Administrator
 */
@RestController
public class GeoHazardMonitorTokenController {

    @Resource
    private ApiTokenUtils apiTokenUtils;
    @Resource
    private AppTokenUtils appTokenUtils;
    @Resource
    BaseDynamicManagementService baseDynamicManagementService;

    /**
     * 获取web访问令牌
     *
     * @param grantType    授权类型，客户端模式（client_credentials）
     * @param clientId     客户端编号
     * @param clientSecret 客户端密钥
     * @return
     */
    @PostMapping("/IPAddress/token")
    public Result getWebToken(@RequestParam("grantType") String grantType,
                           @RequestParam("clientId") String clientId,
                           @RequestParam("clientSecret") String clientSecret) {
        // 生成令牌
        ArrayList<String> objects = apiTokenUtils.createGeoHazardMonitorToken();
        if (objects.size() > 0) {
            RespGeoHazardMonitorVo respGeoHazardMonitorVo = new RespGeoHazardMonitorVo();
            respGeoHazardMonitorVo.setAccessToken(objects.get(1));
            respGeoHazardMonitorVo.setExpiresIn(Long.valueOf(objects.get(0)));
            respGeoHazardMonitorVo.setTokenType("令牌类型");
            return new Result(1, "成功", respGeoHazardMonitorVo);
        } else {
            return new Result(2, "失败", "生成token失败！！");
        }
    }

    /**
     * 获取app访问令牌
     *
     * @param grantType    授权类型，客户端模式（client_credentials）
     * @param clientId     客户端编号
     * @param clientSecret 客户端密钥
     * @return
     */
    @PostMapping("/App/token")
    public Result getAppToken(@RequestParam("grantType") String grantType,
                           @RequestParam("clientId") String clientId,
                           @RequestParam("clientSecret") String clientSecret) {
        // 生成令牌
        ArrayList<String> objects = appTokenUtils.createGeoHazardMonitorToken();
        if (objects.size() > 0) {
            RespGeoHazardMonitorVo respGeoHazardMonitorVo = new RespGeoHazardMonitorVo();
            respGeoHazardMonitorVo.setAccessToken(objects.get(1));
            respGeoHazardMonitorVo.setExpiresIn(Long.valueOf(objects.get(0)));
            respGeoHazardMonitorVo.setTokenType("令牌类型");
            return new Result(1, "成功", respGeoHazardMonitorVo);
        } else {
            return new Result(2, "失败", "生成token失败！！");
        }
    }

    /**
     * 报警信息数据传输
     *
     * @param reqMsgAlarmVo
     * @return
     */
    @PostMapping("/IPAddress/alarm")
    public Result alarm(@RequestBody ReqMsgAlarmVo reqMsgAlarmVo,
                        HttpServletRequest request) {
        // 生成令牌
        String token = apiTokenUtils.getGeoHazardMonitorToken(request);
        if (StringUtils.isNotEmpty(token)) {
            RespMsgAlarmVo respMsgAlarmVo = new RespMsgAlarmVo();
            respMsgAlarmVo.setAlarmId(reqMsgAlarmVo.getAlarmId());
            respMsgAlarmVo.setAlarmCamera(reqMsgAlarmVo.getAlarmCamera());
            respMsgAlarmVo.setCancelTime("");
            return new Result(1, "成功", respMsgAlarmVo);
        } else {
            return new Result(2, "失败", "生成token失败！！");
        }
    }


    /**
     * 设备状态数据传输
     *
     * @param reqDeviceVo
     * @return
     */
    @PostMapping("/IPAddress /device/state")
    public Result deviceState(@RequestBody ReqDeviceVo reqDeviceVo) throws ParseException {
        LambdaQueryWrapper<BaseDynamicManagement> lw = new LambdaQueryWrapper<BaseDynamicManagement>();
        if (!ObjectUtils.isEmpty(reqDeviceVo.getCameraCode())) {
            lw.eq(BaseDynamicManagement::getCameraCode, reqDeviceVo.getCameraCode());
        }
        BaseDynamicManagement baseDynamicManagement = baseDynamicManagementService.getOne(lw);
        baseDynamicManagement.setElectricity(Integer.valueOf(reqDeviceVo.getElectricity()));
        baseDynamicManagement.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(reqDeviceVo.getTime()));
        if (baseDynamicManagementService.updateById(baseDynamicManagement)) {
            return new Result(1, "成功");
        }
        return new Result(2, "失败");
    }
}
