package com.youchain.businessdata.rest;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.appupdate.inputJson.ContainerIn;
import com.youchain.appupdate.inputJson.MissionStateCallback;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.service.PointService;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.domain.Task;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.businessdata.service.TaskService;
import com.youchain.businessdata.service.dto.AgvTaskDto;
import com.youchain.exception.handler.ApiResult;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.BizStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequiredArgsConstructor
@Api(tags = "输送线")
@RequestMapping("/api/Robot/")
@Slf4j
public class LineController {

    @PostMapping("/NotifyConveyor")
    @Log("料箱机器人申请取货/放货/取放完成回调")
    @ApiOperation("料箱机器人申请取货/放货/取放完成回调")
    @AnonymousAccess
    public ResponseEntity<Object> NotifyConveyor(@RequestBody MissionStateCallback missionStateCallback) {

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

    @GetMapping("/getConveyorState")
    @Log("读取输送线、流利式货架状态")
    @ApiOperation("读取输送线、流利式货架状态")
    @AnonymousAccess
    public ResponseEntity<Object> getConveyorState(String conveyorId) {
        //查询点位是否允许取放;conveyorId出入口编号
        boolean success=false;
        String code="400";
        String message="";
        String state="";
        //查询输送线出入口是否有容器；有则，否则
        Stock stock=null;
        if(conveyorId.equals("入")){
            if(stock==null){
                state="3";//不允许取货
            }else{
                state="1";//允许取货
                code="200";
                success=true;
            }
        }else if(conveyorId.equals("出")){
            if(stock==null){
                state="4";//不允许放货
            }else{
                state="2";//允许取货
                code="200";
                success=true;
            }
        }

        JSONObject jsonObject = new JSONObject(new LinkedHashMap<>());
        Map<String, Object> objMap = new LinkedHashMap<>();
        objMap.put("success", success);//
        objMap.put("code", code);//
        objMap.put("message", message);//
        objMap.put("conveyorId", conveyorId);//
        objMap.put("state", state);//
        jsonObject.putAll(objMap);
        return new ResponseEntity<>(ApiResult.success(OK.value(), "", jsonObject), HttpStatus.OK);
    }
}
