package com.shanghaichengdi.ghjgitem.controller;

import com.alibaba.fastjson.JSONObject;
import com.shanghaichengdi.ghjgitem.constant.HttpStatus;
import com.shanghaichengdi.ghjgitem.exception.EnumException;
import com.shanghaichengdi.ghjgitem.service.CommonService;
import com.shanghaichengdi.ghjgitem.util.CommonTools;
import com.shanghaichengdi.ghjgitem.vo.*;
import com.shanghaichengdi.ghjgitem.vo.request.BoatAisRealTimeReq;
import com.shanghaichengdi.ghjgitem.vo.request.BoatPathReq;
import com.shanghaichengdi.ghjgitem.vo.request.ReportInfoRequest;
import com.shanghaichengdi.ghjgitem.vo.request.ShipDataRequest;
import com.shanghaichengdi.ghjgitem.vo.request.ShipMmsiReq;
import com.shanghaichengdi.ghjgitem.vo.request.ShipRangeReq;
import com.shanghaichengdi.ghjgitem.vo.response.ResultRes;
import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
import java.text.ParseException;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/boat")
public class BoatController extends CommonController {

  @Resource
  private CommonService commonService;

  @PostMapping("/getAisRealTimeXY")
  public ReturnMsg getAisRealTimeXY(@RequestBody @Validated ObjectBodyVo vo) throws ParseException {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    BoatAisRealTimeReq boatAisRealTimeReq = JSONObject.parseObject(paramJsonData,
        BoatAisRealTimeReq.class);
    List<BoatAISVo> boatAISVoList = commonService.selectAisRealTimeXYList(boatAisRealTimeReq);
    ResultRes resultRes = ResultRes.encapsulationResult(boatAisRealTimeReq.getPageIndex(),
        boatAisRealTimeReq.getPageSize(),
        CommonTools.getTotal(boatAISVoList), boatAISVoList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getInfoList")
  public ReturnMsg getInfoList(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipDataRequest req = JSONObject.parseObject(paramJsonData, ShipDataRequest.class);
    if (StringUtils.isBlank(req.getShipId()) && StringUtils.isBlank(req.getShipNameCn())
        && StringUtils.isBlank(req.getMmsi()) && StringUtils.isBlank(req.getShipTypeName())) {
      throw new EnumException("请至少输入一个查询参数");
    }
    if (!StringUtils.isBlank(req.getMmsi()) && req.getMmsi().length() < 6) {
      throw new EnumException("mmsi不足六位");
    }
    List<ShipDataVo> voList = commonService.selectBoatInfoList(req);
    ResultRes resultRes = ResultRes.encapsulationResult(req.getPageIndex(),
        req.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getInfoListPd")
  public ReturnMsg getInfoListPd(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipDataRequest req = JSONObject.parseObject(paramJsonData, ShipDataRequest.class);
//    if (StringUtils.isBlank(req.getShipId()) && StringUtils.isBlank(req.getShipNameCn())
//        && StringUtils.isBlank(req.getMmsi()) && StringUtils.isBlank(req.getShipTypeName())) {
//      throw new EnumException("请至少输入一个查询参数");
//    }
    if (!StringUtils.isBlank(req.getMmsi()) && req.getMmsi().length() < 6) {
      throw new EnumException("mmsi不足六位");
    }
    // 判断只能查当前在浦东范围内有AIS信号的船
    if(!commonService.checkInportByMmsi(req.getMmsi())){
      throw new EnumException("该船舶不在辖区内");
    }
    List<ShipDataVo> voList = commonService.selectBoatInfoList(req);
    ResultRes resultRes = ResultRes.encapsulationResult(req.getPageIndex(),
        req.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

  @PostMapping("/getInfoListObscure")
  public ReturnMsg obscureGetInfoList(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipDataRequest req = JSONObject.parseObject(paramJsonData, ShipDataRequest.class);
    if (req.getKeyWord().matches("\\d+") && req.getKeyWord().length() < 6) {
      throw new EnumException("mmsi不足六位");
    }
    List<ShipDataVo> voList = commonService.obscureSelectBoatInfoList(req);
    ResultRes resultRes = ResultRes.encapsulationResult(req.getPageIndex(),
        req.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

  @PostMapping("/getReportInfoList")
  public ReturnMsg getReportInfoList(@RequestBody @Validated ObjectBodyVo vo)
      throws ParseException {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ReportInfoRequest req = JSONObject.parseObject(paramJsonData, ReportInfoRequest.class);
    // 判断只能查当前在浦东范围内有AIS信号的船
    if(!commonService.checkInportByShipId(req.getShipId())){
      throw new EnumException("该船舶不在辖区内");
    }
    List<BoatReportInfoVo> voList = commonService.selectBoatReportInfoList(req);
    ResultRes resultRes = ResultRes.encapsulationResult(req.getPageIndex(),
        req.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getRangeCount")
  public ReturnMsg getRangeCount(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipRangeReq req = JSONObject.parseObject(paramJsonData, ShipRangeReq.class);
    CountVo countVo = commonService.getRangeCount(req);
    ResultRes resultRes = ResultRes.encapsulationResult(countVo.getCount());
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

  @PostMapping("/getRangeDetail")
  public ReturnMsg getRangeDetail(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipRangeReq req = JSONObject.parseObject(paramJsonData, ShipRangeReq.class);
    List<ShipDataVo> voList = commonService.getRangeDetail(req);
    ResultRes resultRes = ResultRes.encapsulationResult(req.getPageIndex(),
        req.getPageSize(),
        CommonTools.getTotal(voList), voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }


  @PostMapping("/getMmsiDetail")
  public ReturnMsg getMmsiDetail(@RequestBody @Validated ObjectBodyVo vo) throws ParseException {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipMmsiReq req = JSONObject.parseObject(paramJsonData, ShipMmsiReq.class);
    List<ShipDataVo> voList = commonService.getMmsiDetail(req);
    ResultRes resultRes = ResultRes.encapsulationResult(voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

  /**
   * 获取船舶轨迹(ES版)
   *
   * @param vo
   * @return
   * @throws ParseException
   */
  @PostMapping("/getBoatPath")
  public ReturnMsg getBoatPath(@RequestBody @Validated ObjectBodyVo vo)
      throws ParseException {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    BoatPathReq req = JSONObject.parseObject(paramJsonData, BoatPathReq.class);
    List<BoatPathVo> voList = commonService.selectBoatPathList(req);
    ResultRes resultRes = ResultRes.encapsulationResult(voList);
    String jsonString = JSONObject.toJSONString(resultRes);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }

  /**
   * 获取船舶信息,通过shipId进行精准匹配
   */
  @PostMapping("/getBoatInfoByShipId")
  public ReturnMsg getBoatInfoByShipId(@RequestBody @Validated ObjectBodyVo vo) {
    if (!checkDbKey(vo.getKey())) {
      return ReturnMsg.error(HttpStatus.BAD_REQUEST, "key值有误,请确认您输入的key值正确!");
    }
    String paramJsonData = getParamJsonData(vo.getData());
    ShipDataRequest req = JSONObject.parseObject(paramJsonData, ShipDataRequest.class);
    ShipInfoForShipLockVo voList = commonService.selectBoatInfoByShipId(req.getShipId());
    String jsonString = JSONObject.toJSONString(voList);
    String encodedData = resultEncode(jsonString);
    return ReturnMsg.success("获取成功", encodedData);
  }
}
