package org.nl.acs.ext.wms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.address.service.AddressService;
import org.nl.acs.address.service.AddressService;
import org.nl.acs.device.domain.Device;
import org.nl.acs.device_driver.basedriver.siemens_conveyor.SiemensConveyorDeviceDriver;
import org.nl.acs.ext.wms.RespUtil;
import org.nl.acs.ext.wms.liKuData.*;
import org.nl.acs.ext.wms.service.AcsToLiKuService;
import org.nl.acs.ext.wms.service.AcsToWmsService;
import org.nl.acs.ext.wms.service.LiKuToAcsService;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.opc.DeviceAppServiceImpl;
import org.nl.acs.task.service.TaskService;
import org.nl.acs.task.service.dto.TaskDto;
import org.nl.config.SpringContextHolder;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

/**
 * 立库WCS接口
 *
 * @author: wangs
 * @createDate: 2022/11/24
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LiKuToAcsServiceImpl implements LiKuToAcsService {

    private final AddressService addressService;

    private final TaskService taskService;

    private final InstructionService instructionService;

    private final AcsToWmsService acsToWmsService;

    private final AcsToLiKuService acsToLiKuService;


    private String log_file_type="log_file_type";
    private String log_type="立库请求ACS";

    //入库任务状态反馈
    @Override
    public Resp<InStoreReportResponse> inStoreReport(InStoreReportRequest requestParam) throws Exception {

        log.info("入库任务状态反馈-----输入参数{}", JSON.toJSONString(requestParam));
        String inst_code = requestParam.getOrderId();
        String status = requestParam.getState();
        String carNo = requestParam.getRobotId();
        Instruction inst = instructionService.findByCodeFromCache(inst_code);
        if (ObjectUtil.isEmpty(inst)) {
            InStoreReportResponse inStoreReportResponse = new InStoreReportResponse();
            inStoreReportResponse.setOrderId(inst_code);
            JSONObject result = new JSONObject();
            result.put("result", "false");
            result.put("code", "0");
            result.put("comment", "未找到对应指令");
            result.put("data", inStoreReportResponse );
            log.info("入库任务状态反馈-----输出参数{}", result);
            return RespUtil.getResp(result.toString(), new InStoreReportResponse());
        }
        TaskDto task = taskService.findByCodeFromCache(inst.getTask_code());
        if (ObjectUtil.isEmpty(task)) {
            InStoreReportResponse inStoreReportResponse = new InStoreReportResponse();
            inStoreReportResponse.setOrderId(inst_code);
            JSONObject result = new JSONObject();
            result.put("result", "false");
            result.put("code", "0");
            result.put("comment", "未找到对应任务");
            result.put("data", inStoreReportResponse );
            log.info("入库任务状态反馈-----输出参数{}", result);
            return RespUtil.getResp(result.toString(), new InStoreReportResponse());
        }
        // 1 已接收 2 开始执行 3执行完成 4  5 取消
        if(StrUtil.equals(status,"1") || StrUtil.equals(status,"2") ){
            task.setTask_status("1");
            taskService.update(task);
            inst.setInstruction_status("1");
            inst.setExecute_device_code(carNo);
            inst.setCarno(carNo);
            instructionService.update(inst);
        } else if(StrUtil.equals(status,"3")){
            inst.setInstruction_status("2");
            instructionService.finish(inst.getInstruction_id());
        } else if(StrUtil.equals(status,"4")) {

            // 5 取消指令
        } else if(StrUtil.equals(status,"5")) {

            instructionService.cancel(inst.getInstruction_id());
        }
        InStoreReportResponse inStoreReportResponse = new InStoreReportResponse();
        inStoreReportResponse.setOrderId(inst_code);
        JSONObject result = new JSONObject();
        result.put("result", "true");
        result.put("code", "0");
        result.put("comment", "");
        result.put("data", inStoreReportResponse );
        log.info("入库任务状态反馈-----输出参数{}", result);
        return RespUtil.getResp(result.toString(), new InStoreReportResponse());

    }

    @Override
    public Resp<OutStoreReportResponse> outStoreReport(OutStoreReportRequest requestParam) throws Exception {
        try {
            MDC.put(log_file_type, log_type);
            log.info("出库任务状态反馈-----输入参数{}", JSON.toJSONString(requestParam));
            String inst_code = requestParam.getOrderId();
            String status = requestParam.getState();
            String carNo = requestParam.getRobotId();
            Instruction inst = instructionService.findByCodeFromCache(inst_code);
            if (ObjectUtil.isEmpty(inst)) {
                OutStoreReportResponse outStoreReportResponse = new OutStoreReportResponse();
                outStoreReportResponse.setOrderId(inst_code);
                JSONObject result = new JSONObject();
                result.put("result", "false");
                result.put("code", "0");
                result.put("comment", "未找到对应指令");
                result.put("data", outStoreReportResponse );
                log.info("出库任务状态反馈-----输出参数{}", result);
                return RespUtil.getResp(result.toString(), new OutStoreReportResponse());
            }
            TaskDto task = taskService.findByCodeFromCache(inst.getTask_code());
            if (ObjectUtil.isEmpty(task)) {
                OutStoreReportResponse outStoreReportResponse = new OutStoreReportResponse();
                outStoreReportResponse.setOrderId(inst_code);
                JSONObject result = new JSONObject();
                result.put("result", "false");
                result.put("code", "0");
                result.put("comment", "未找到对应任务");
                result.put("data", outStoreReportResponse );
                log.info("出库任务状态反馈-----输出参数{}", result);
                return RespUtil.getResp(result.toString(), new OutStoreReportResponse());
            }
            // 1 已接收 2 开始执行 3执行完成 4 阻挡 5 空洞
            if(StrUtil.equals(status,"1") || StrUtil.equals(status,"2") ){
                task.setTask_status("1");
                taskService.update(task);
                inst.setInstruction_status("1");
                inst.setExecute_device_code(carNo);
                inst.setCarno(carNo);
                instructionService.update(inst);
            } else if(StrUtil.equals(status,"3")){
//                inst.setInstruction_status("1");
//                instructionService.finish(inst.getInstruction_id());

                if(StrUtil.equals(task.getTask_type(),"7") && StrUtil.equals(task.getStorage_task_type(),"4")){
                    inst.setInstruction_status("2");
                    instructionService.finish(inst.getInstruction_id());
                } else {
                    inst.setRemark("立库已反馈完成，待输送上报任务号");
                    instructionService.update(inst);
                }

            } else if(StrUtil.equals(status,"4")){
                //5 取消指令
            } else if(StrUtil.equals(status,"5")){
                instructionService.cancel(inst.getInstruction_id());

            }
            OutStoreReportResponse outStoreReportResponse = new OutStoreReportResponse();
            outStoreReportResponse.setOrderId(inst_code);
            JSONObject result = new JSONObject();
            result.put("result", "true");
            result.put("code", "0");
            result.put("comment", "");
            result.put("data", outStoreReportResponse );
            log.info("出库任务状态反馈-----输出参数{}", result);

            return RespUtil.getResp(result.toString(), new OutStoreReportResponse());
        } finally {
            MDC.remove(log_file_type);
        }




    }

    //移库任务上报
    @Override
    public Resp<MoveStoreReportResponse> moveStoreReport(MoveStoreReportRequest requestParam) throws Exception {
        try {
            MDC.put(log_file_type, log_type);
            log.info("移库任务上报-----输入参数{}", JSON.toJSONString(requestParam));
            String inst_code = requestParam.getOrderId();
            String status = requestParam.getState();
            String carNo = requestParam.getRobotId();
            Instruction inst = instructionService.findByCodeFromCache(inst_code);
            if (ObjectUtil.isEmpty(inst)) {
                MoveStoreReportResponse moveStoreReportResponse = new MoveStoreReportResponse();
                moveStoreReportResponse.setOrderId(inst_code);
                JSONObject result = new JSONObject();
                result.put("result", "false");
                result.put("code", "0");
                result.put("comment", "未找到对应指令");
                result.put("data", moveStoreReportResponse );
                log.info("移库任务状态反馈-----输出参数{}", result);
                return RespUtil.getResp(result.toString(), new MoveStoreReportResponse());
            }
            TaskDto task = taskService.findByCodeFromCache(inst.getTask_code());
            if (ObjectUtil.isEmpty(task)) {
                MoveStoreReportResponse moveStoreReportResponse = new MoveStoreReportResponse();
                moveStoreReportResponse.setOrderId(inst_code);
                JSONObject result = new JSONObject();
                result.put("result", "false");
                result.put("code", "0");
                result.put("comment", "未找到对应任务");
                result.put("data", moveStoreReportResponse );
                log.info("移库任务状态反馈-----输出参数{}", result);
                return RespUtil.getResp(result.toString(), new MoveStoreReportResponse());
            }            // 1 已接收 2 开始执行 3执行完成 4 阻挡 5 空洞
            if(StrUtil.equals(status,"1") || StrUtil.equals(status,"2") ){
                task.setTask_status("1");
                taskService.update(task);
                inst.setInstruction_status("1");
                inst.setExecute_device_code(carNo);
                inst.setCarno(carNo);
                instructionService.update(inst);

            } else if(StrUtil.equals(status,"3")){
                inst.setInstruction_status("2");
                instructionService.finish(inst.getInstruction_id());
            } else if(StrUtil.equals(status,"4")){


            } else if(StrUtil.equals(status,"5")){
                instructionService.cancel(inst.getInstruction_id());
            }
            MoveStoreReportResponse moveStoreReportResponse = new MoveStoreReportResponse();
            moveStoreReportResponse.setOrderId(inst_code);
            JSONObject result = new JSONObject();
            result.put("result", "true");
            result.put("code", "0");
            result.put("comment", "");
            result.put("data", moveStoreReportResponse );
            log.info("移库任务上报-----输出参数{}", result);

            return RespUtil.getResp(result.toString(), new MoveStoreReportResponse());
        } finally {
            MDC.remove(log_file_type);
        }

    }




    //请求放空盘
    @Override
    public Resp<DeviceStatusResponse> putEmptyPallet(putEmptyPalletRequest requestParam) throws Exception {
        try {
            MDC.put(log_file_type, log_type);
            log.info("请求放空盘-----输入参数{}", JSON.toJSONString(requestParam));
            String device_code = requestParam.getDeviceId();
            String type = requestParam.getType();
            DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class);
            Device device = appService.findDeviceByCode(device_code);
            SiemensConveyorDeviceDriver siemensConveyorDeviceDriver;
            JSONObject result = new JSONObject();
            result.put("code", "0");
            if (device.getDeviceDriver() instanceof SiemensConveyorDeviceDriver) {
                siemensConveyorDeviceDriver = (SiemensConveyorDeviceDriver) device.getDeviceDriver();
                if(StrUtil.equals(type,"1")){
                    if((siemensConveyorDeviceDriver.getMode() ==2 || siemensConveyorDeviceDriver.getMode() ==7 )&& siemensConveyorDeviceDriver.getMove() ==0){
                        result.put("result", "true");
                        result.put("comment", "");
                    } else {
                        result.put("result", "false");
                        result.put("comment", "当前设备光电信号:"+siemensConveyorDeviceDriver.getMove()+",工作模式:"+siemensConveyorDeviceDriver.getMode());
                    }
                } else if(StrUtil.equals(type,"2")){
                    siemensConveyorDeviceDriver.writing("to_command","3");
                    siemensConveyorDeviceDriver.writing("to_command","3");

                    result.put("result", "true");
                    result.put("comment", "");
                }

            }
            log.info("请求放空盘-----输出参数{}", result);
            return RespUtil.getResp(result.toString(), new DeviceStatusResponse());
        } finally {
            MDC.remove(log_file_type);
        }

    }

}
