package com.tuoheng.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rabbitmq.tools.json.JSONUtil;
import com.tuoheng.admin.entity.domain.ThInspection;
import com.tuoheng.admin.entity.domain.ThMission;
import com.tuoheng.admin.entity.request.InspectionRequest;
import com.tuoheng.admin.entity.request.MissionStatusRequest;
import com.tuoheng.admin.entity.request.PushAndPullURLRequest;
import com.tuoheng.admin.entity.request.airport.ExecuteTaskStatusRequest;
import com.tuoheng.admin.entity.vo.InspectionVO;
import com.tuoheng.admin.service.IMissionService;
import com.tuoheng.admin.service.IThInspectionService;
import com.tuoheng.common.common.BaseQuery;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.system.utils.ShiroUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @User qiujinyang
 * @Description
 * @Date Created by 2022/7/28 9:28
 */
@RestController
@RequestMapping("/inspection")
@ApiOperation(value = "巡检坐标")
@Slf4j
public class InspectionController {


    @Autowired
    private IThInspectionService inspectionService;

    @Autowired
    private IMissionService missionService;

    /**
     * 获取巡检机场
     */
    @GetMapping("/airport")
    @ApiOperation(value = "获取巡检机场", notes = "获取巡检机场")
    public JsonResult airport(BaseQuery baseQuery) throws UnsupportedEncodingException {
        return JsonResult.success(inspectionService.airport());
    }


    /**
     * 获取巡检线路
     */
    @GetMapping("/airport/line/{droneId}")
    @ApiOperation(value = "获取巡检线路", notes = "获取巡检线路")
    @ApiImplicitParam(name="droneId",value = "机场列表里面的droneId")
    public JsonResult airLine(@PathVariable("droneId") Integer droneId) {
        return JsonResult.success(inspectionService.airLine(droneId));
    }

    /**
     * 获取飞行轨迹
     */
    @GetMapping("/track/{id}")
    @ApiOperation(value = "获取飞行轨迹", notes = "获取飞行轨迹")
    @ApiImplicitParam(name="id",value = "任务的Id，missionId")
    public JsonResult<List<InspectionVO>> track(@PathVariable("id") Integer id) {
        return JsonResult.success(inspectionService.track(id));
    }

    /**
     * 返回标准的航线轨迹
     */
    @GetMapping("/line/track/{missionId}")
    @ApiOperation(value = "获取航线轨迹", notes = "获取飞行轨迹")
    @ApiImplicitParam(name="id",value = "任务的Id，missionId")
    public JsonResult lineTrack(@PathVariable("missionId") Integer missionId) {
        return inspectionService.lineTrack(missionId);
    }


    /**
     * 立即执行
     */
    @PutMapping("/mission/{id}")
    @ApiOperation(value = "立即执行", notes = "立即执行")
    @ApiImplicitParam(name="id",value = "巡检的Id，飞行任务里面的id")
    public JsonResult executeTask(@PathVariable("id") String id) {
        return inspectionService.executeTask(id,null);
    }

    @PutMapping("/mission/url/{id}")
    @ApiOperation(value = "立即执行", notes = "立即执行")
    @ApiImplicitParam(name="id",value = "巡检的Id，飞行任务里面的id")
    public JsonResult executeTask(@PathVariable("id") String id,@RequestBody PushAndPullURLRequest pushAndPull) {
        return inspectionService.executeTask(id,pushAndPull);
    }

    /**
     * 获取天气信息
     */
    @GetMapping("/airport/weather/{airportId}")
    @ApiOperation(value = "获取天气信息", notes = "获取天气信息")
    @ApiImplicitParam(name="airportId",value = "机场列表里面的Id")
    public JsonResult getWeather(@PathVariable("airportId")Integer airportId) {
        return JsonResult.success(inspectionService.getWeather(airportId));
    }

    /**
     * 被硬件调用，执行任务后，回调执行状态
     */
    @PostMapping("/executeTaskStatus")
    public JsonResult updateExecuteTaskStatus(@RequestBody @Valid ExecuteTaskStatusRequest executeTaskStatusRequest) {
        log.info("被硬件调用，执行任务后，回调执行状态：{}", JSONObject.toJSONString(executeTaskStatusRequest));
        return missionService.updateExecuteTaskStatus(executeTaskStatusRequest);
    }

    /**
     * 被硬件调用，存任务状态
     */
    @PutMapping("/status")
    @ApiOperation(value = "被硬件调用，存任务状态", notes = "被硬件调用，存任务状态")
    public JsonResult<Boolean> track(@RequestBody @Valid MissionStatusRequest missionStatusRequest) {
        log.info("被硬件调用，存任务状态：{}", JSONObject.toJSONString(missionStatusRequest));
        return JsonResult.success(missionService.updateStatus(missionStatusRequest));
    }


    /**
     * 被硬件调用，存飞行轨迹
     */
    @PostMapping("/track")
    @ApiOperation(value = "被硬件调用，存飞行轨迹", notes = "被硬件调用，存飞行轨迹")
    public JsonResult<Integer> track(@RequestBody @Valid InspectionRequest inspectionRequest) {
        log.info("被硬件调用，存飞行轨迹：{}", JSONObject.toJSONString(inspectionRequest));
        return JsonResult.success(inspectionService.track(inspectionRequest));
    }

    /**
     * 获取飞行轨迹
     * @param id
     * @return
     */
    @GetMapping("/fightData/{id}")
    public JsonResult fightData(@PathVariable("id") Integer id){
        return inspectionService.fightData(id);
    }



}
