/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.youchain.businessdata.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.businessdata.service.dto.AgvTaskDto;
import com.youchain.businessdata.service.dto.AgvTaskQueryCriteria;
import com.youchain.exception.handler.ApiResult;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author baobinglin
* @date 2023-08-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "agvTask管理")
@RequestMapping("/api/agvTask")
public class AgvTaskController {

    private final AgvTaskService agvTaskService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('agvTask:list')")
    public void exportAgvTask(HttpServletResponse response, AgvTaskQueryCriteria criteria) throws IOException {
        agvTaskService.download(agvTaskService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询agvTask")
    @ApiOperation("查询agvTask")
    @PreAuthorize("@el.check('agvTask:list')")
    public ResponseEntity<Object> queryAgvTask(AgvTaskQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(agvTaskService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增agvTask")
    @ApiOperation("新增agvTask")
    @PreAuthorize("@el.check('agvTask:add')")
    public ResponseEntity<Object> createAgvTask(@Validated @RequestBody AgvTask resources){
        return new ResponseEntity<>(agvTaskService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改agvTask")
    @ApiOperation("修改agvTask")
    @PreAuthorize("@el.check('agvTask:edit')")
    public ResponseEntity<Object> updateAgvTask(@Validated @RequestBody AgvTask resources){
        agvTaskService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除agvTask")
    @ApiOperation("删除agvTask")
    @PreAuthorize("@el.check('agvTask:del')")
    public ResponseEntity<Object> deleteAgvTask(@RequestBody Integer[] ids) {
        agvTaskService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 料箱任务接口回调
     * @param json
     */
    @PostMapping("/agvTaskCallback")
    @Log("AGV搬运任务回调接口")
    @ApiOperation("AGV搬运任务回调接口")
    @AnonymousAccess
    public ResponseEntity<Object> agvTaskCallback(@RequestBody String json){
        System.out.println("json:"+json);
        JSONObject jsonObject = JSON.parseObject(json);
        String taskCode = jsonObject.getString("taskCode")==null?"":jsonObject.getString("taskCode");//任务编号
        String podCode = jsonObject.getString("podCode")==null?"":jsonObject.getString("podCode");//容器编号
        String currentNodeCode = jsonObject.getString("currentNodeCode")==null?"":jsonObject.getString("currentNodeCode");//当前位置
        String missionStatus = jsonObject.getString("missionStatus")==null?"":jsonObject.getString("missionStatus");//作业当前状态
        agvTaskService.agvTaskCallback(taskCode,missionStatus);
        return new ResponseEntity<>(ApiResult.success("",""),HttpStatus.OK);
        //查询对应BOX
        /*try {
            AgvTaskDto dto= agvTaskService.findById(Integer.parseInt(taskCode));
            AgvTask agvTask=agvTaskService.toEntity(dto);
            agvTaskService.agvTaskCallback(agvTask,missionStatus,"");

        } catch (Exception e) {
            return new ResponseEntity<>(ApiResult.fail(400,e.getMessage(),""),HttpStatus.OK);
        }*/
    }

    /**
     * 料箱机器人申请取货/放货/取放完成回调
     * @param json
     */
    @PostMapping("/NotifyConveyor")
    @Log("料箱机器人回调")
    @ApiOperation("料箱机器人回调")
    @AnonymousAccess
    public ResponseEntity<Object> NotifyConveyor(@RequestBody String json){
        JSONObject jsonObject = JSON.parseObject(json);
        String conveyorId = jsonObject.getString("conveyorId")==null?"":jsonObject.getString("conveyorId");//输送线、光电编号
        /**
         * type
         * 1：申请取货
         * 2：申请放货
         * 3、取货完成
         * 4、放货完成
         */
        String type = jsonObject.getString("type")==null?"":jsonObject.getString("type");//类型

        return new ResponseEntity<>(ApiResult.success("",""),HttpStatus.OK);
    }


    /**
     * 输送线申请取货/放货/完成回报
     * @param json
     */
    @PostMapping("/lineApply")
    @Log("输送线回调")
    @ApiOperation("输送线回调")
    @AnonymousAccess
    public ResponseEntity<Object> lineApply(@RequestBody String json){
        JSONObject jsonObject = JSON.parseObject(json);
        String lineCode = jsonObject.getString("lineCode")==null?"":jsonObject.getString("lineCode");//输送线、光电编号
        /**
         * type
         * 1：申请取货
         * 2：申请放货
         * 3、取货完成
         * 4、放货完成
         */
        String type = jsonObject.getString("type")==null?"":jsonObject.getString("type");//类型

        return new ResponseEntity<>(ApiResult.success("",""),HttpStatus.OK);
    }


    @PostMapping("/test")
    @Log("测试料箱任务下发")
    @ApiOperation("测试料箱任务下发")
    @AnonymousAccess
    public ResponseEntity<Object>  test(){

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/getConveyorState")
    @Log("读取输送线、流利式货架状态")
    @ApiOperation("读取输送线、流利式货架状态")
    @AnonymousAccess
    public ResponseEntity<Object>  getConveyorState(@RequestParam String conveyorId){
        agvTaskService.getConveyorState(conveyorId);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}