package com.zcxk.track.controller;

import com.zcxk.common.ConvertXY;
import com.zcxk.common.MyPoint;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.track.dto.GpsDataHisDto;
import com.zcxk.track.dto.XYDto;
import com.zcxk.track.entity.GpsDataHis;
import com.zcxk.track.entity.TaskTrack;
import com.zcxk.track.service.GpsDataHisService;
import com.zcxk.track.service.TaskTrackService;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/track/TaskTrack")
@Api(tags = "任务轨迹")
public class TaskTrackController {

    @Autowired
    TaskTrackService taskTrackService;
    @Autowired
    GpsDataHisService gpsDataHisService;

    /**
     * 新增一条数据
     *
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "startTrack", method = RequestMethod.GET)
    @ApiOperation(value = "开始轨迹")
    public AjaxMessage<Integer> startTrack(@ApiParam(value = "任务id", required = true) @RequestParam Integer taskId) {
        TaskTrack taskTrack=new TaskTrack();
        LoginUser currentUser = UserUtil.getCurrentUser();
        taskTrack.setUserId(currentUser.getId());
        taskTrack.setTaskId(taskId);
        taskTrack.setTaskStartDate(new Date());
        TaskTrack track=taskTrackService.selectTaskTrack(taskId,currentUser.getId());
        if(track == null) {
            taskTrackService.insert(taskTrack);
        }else{
            taskTrack.setId(track.getId());
            taskTrack.setTaskEndDate(null);
            taskTrackService.updateTrack(taskTrack);
        }
        return new AjaxMessage<>(ResultStatus.OK,taskTrack.getId());
    }
    /**
     * 修改一条数据
     *
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "endTrack", method = RequestMethod.GET)
    @ApiOperation(value = "结束轨迹")
    public AjaxMessage<Boolean> endTrack( @ApiParam(value = "任务id", required = true) @RequestParam Integer taskId) {
        TaskTrack taskTrack=new TaskTrack();
        LoginUser currentUser = UserUtil.getCurrentUser();
        taskTrack.setTaskEndDate(new Date());
        TaskTrack track=taskTrackService.selectTaskTrack(taskId,currentUser.getId());
        if(track != null) {
            taskTrack.setId(track.getId());
            taskTrackService.updateTrack(taskTrack);
        }
        return new AjaxMessage<>(ResultStatus.OK);
    }
    /**
     * 查询任务轨迹
     *
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "selectTaskTrack", method = RequestMethod.GET)
    @ApiOperation(value = "查询任务轨迹")
    public AjaxMessage<List<GpsDataHisDto>> selectTaskTrack(
            @ApiParam(value = "任务id", required = true) @RequestParam Integer taskId,
            @ApiParam(value = "用户id", required = false) @RequestParam(required = false) Integer userId,
            @ApiParam(value = "wkid", required = false) @RequestParam(required = false) String wkid,
            @ApiParam(value = "偏移量(x,y)", required = false) @RequestParam(required = false) String moveXY) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        userId=userId==null?currentUser.getId():userId;
        TaskTrack track=taskTrackService.selectTaskTrack(taskId,userId);
        List<GpsDataHis> gpsList=gpsDataHisService.selectGpsDataHis(track);
        List<GpsDataHisDto> result=new ArrayList<>();
        //转换坐标
        for (GpsDataHis gpsDataHis:gpsList) {
            GpsDataHisDto gpsDataHisDto=new GpsDataHisDto();
            gpsDataHisDto.setId(gpsDataHis.getId());
            gpsDataHisDto.setUserId(gpsDataHis.getUserId());
            gpsDataHisDto.setUploadDate(gpsDataHis.getUploadDate());
            gpsDataHisDto.setLatitude(gpsDataHis.getLatitude());
            gpsDataHisDto.setLongtitude(gpsDataHis.getLongtitude());
            if(!StringUtils.isEmpty(wkid) && wkid.equals("2360")){
                MyPoint myPoint= ConvertXY.ConvertXYToXian80(gpsDataHis.getLongtitude().toString(),
                        gpsDataHis.getLatitude().toString(),moveXY);
                gpsDataHisDto.setConvertX(myPoint.getX());
                gpsDataHisDto.setConvertY(myPoint.getY());
            }else if(!StringUtils.isEmpty(wkid) && wkid.equals("3857")){
                MyPoint myPoint= ConvertXY.ConvertXYToMercato(gpsDataHis.getLongtitude().toString(),
                        gpsDataHis.getLatitude().toString(),moveXY);
                gpsDataHisDto.setConvertX(myPoint.getX());
                gpsDataHisDto.setConvertY(myPoint.getY());
            }
            result.add(gpsDataHisDto);
        }

        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 转换坐标
     *
     * @param
     * @return Response对象
     */
    @RequestMapping(value = "convertXY", method = RequestMethod.POST)
    @ApiOperation(value = "转换坐标")
    public AjaxMessage<XYDto> convertXY(@ApiParam(value = "GPS数据", required = true) @RequestBody XYDto xyDto){
        List<String> convertXY=new ArrayList<>();
        //经纬度转其他坐标系
        if(xyDto.getXY()!=null&&xyDto.getXY().size()>0){
            for (String xy:xyDto.getXY()) {
                if(!StringUtils.isEmpty(xyDto.getWkid()) && xyDto.getWkid().equals("2360")){
                    MyPoint myPoint= ConvertXY.ConvertXYToXian80(xy.split(",")[0], xy.split(",")[1]
                            ,xyDto.getMoveXY());
                    convertXY.add(myPoint.getX()+","+myPoint.getY());
                }else if(!StringUtils.isEmpty(xyDto.getWkid()) && xyDto.getWkid().equals("3857")){
                    MyPoint myPoint= ConvertXY.ConvertXYToMercato(xy.split(",")[0], xy.split(",")[1]
                            ,xyDto.getMoveXY());
                    convertXY.add(myPoint.getX()+","+myPoint.getY());
                }
            }
            xyDto.setConvertXY(convertXY);
        }else{
            //其他坐标系转经纬度
            for (String xy:xyDto.getConvertXY()) {
                if(!StringUtils.isEmpty(xyDto.getWkid()) && xyDto.getWkid().equals("2360")){
                    MyPoint myPoint= ConvertXY.ConvertXian80ToXY(xy.split(",")[0], xy.split(",")[1]
                            ,xyDto.getMoveXY());
                    convertXY.add(myPoint.getX()+","+myPoint.getY());
                }else if(!StringUtils.isEmpty(xyDto.getWkid()) && xyDto.getWkid().equals("3857")){
                    MyPoint myPoint= ConvertXY.MercatorToXY(xy.split(",")[0], xy.split(",")[1]
                            ,xyDto.getMoveXY());
                    convertXY.add(myPoint.getX()+","+myPoint.getY());
                }
            }
            xyDto.setXY(convertXY);
        }
        return new AjaxMessage<>(ResultStatus.OK, xyDto);
    }
}