package org.nl.acs.device_driver.basedriver.standard_ordinary_site;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.agv.server.NDCAgvService;
import org.nl.acs.device.domain.Device;
import org.nl.acs.device.service.DeviceService;
import org.nl.acs.device_driver.DeviceDriver;
import org.nl.acs.device_driver.RouteableDeviceDriver;
import org.nl.acs.device_driver.driver.AbstractDeviceDriver;
import org.nl.acs.device_driver.driver.ExecutableDeviceDriver;
import org.nl.acs.ext.wms.service.AcsToWmsService;
import org.nl.acs.ext.wms.service.impl.AcsToWmsServiceImpl;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.log.service.DeviceExecuteLogService;
import org.nl.acs.monitor.DeviceStageMonitor;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.route.service.RouteLineService;
import org.nl.acs.task.service.TaskService;
import org.nl.config.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;


/**
 * 普通站点
 */
@Slf4j
@Data
@RequiredArgsConstructor
public class StandardOrdinarySiteDeviceDriver extends AbstractDeviceDriver implements DeviceDriver, ExecutableDeviceDriver, RouteableDeviceDriver , DeviceStageMonitor {
    @Autowired
    DeviceAppService deviceAppservice = SpringContextHolder.getBean(DeviceAppService.class);
    @Autowired
    InstructionService instructionService = SpringContextHolder.getBean("instructionServiceImpl");
    @Autowired
    DeviceService deviceservice = SpringContextHolder.getBean("deviceServiceImpl");
    @Autowired
    RouteLineService routelineserver = SpringContextHolder.getBean("routeLineServiceImpl");
    @Autowired
    TaskService taskserver = SpringContextHolder.getBean("taskServiceImpl");
    @Autowired
    RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class);
    @Autowired
    AcsToWmsService acsToWmsService = SpringContextHolder.getBean(AcsToWmsServiceImpl.class);
    @Autowired
    DeviceExecuteLogService logServer = SpringContextHolder.getBean(DeviceExecuteLogService.class);
    @Autowired
    NDCAgvService agvService = SpringContextHolder.getBean(NDCAgvService.class);


    Integer hasGoods = 0;
    int error = 0;
    Boolean iserror = false;
    Boolean islock = false;

    int branchProtocol = 0;
    int last_branchProtocol = 0;
    //是否需要输入物料
    String input_material = "0";
    //备注
    String remark = "";
    //数量
    String qty = "";
    //批次
    String batch = "";
    //物料
    String material = "";
    //目标点位
    String purpose = "";
    //当前指令
    Instruction inst = null;
    //上次指令
    Instruction last_inst = null;

    boolean requireSucess = false;

    //触摸屏手动触发任务
    private Boolean is_has_task = false;

    //申请搬运任务
    private Boolean apply_handling = false;
    //申请物料
    private Boolean apply_material = false;

    // 1取货完成 2放货完成 3进入区域 4离开区域
    private int flag;

    //人工确认信号 默认0 agv到达后请求置1 等人工确认后变为2 反馈agv后继续为0
    private int manua_confirm = 0;

    String device_code = null;
    String container;
    String container_type_desc;
    String last_container_type_desc;
    String last_container;
    private Date instruction_require_time = new Date();
    private Date instruction_finished_time = new Date();

    private int instruction_require_time_out;

    String message;

    // 1 上位系统允许进入 2 上位系统允许离开
    int status = 0;

    int agvphase = 0;
    int index = 0;

    int mode = 2;

    int move;

    @Override
    public void execute() {
        hasGoods = this.getDevice().getHas_goods();
        batch = this.getDevice().getBatch();
        device_code = this.getDeviceCode();

        if (agvphase == 0x03) {
            if (ObjectUtil.isNotEmpty(inst)) {
                inst.setExecute_status("1");
                instructionService.update(inst);
                log.info("agvphase:" + agvphase + "反馈成功,到达普通站点。");
                byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
                agvphase = 0;
                index = 0;
                inst = null;
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "反馈成功");
            } else {
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "等待反馈");

            }
        }

        if (agvphase == 0x05) {
            if (ObjectUtil.isNotEmpty(inst)) {
                inst.setExecute_status("2");
                instructionService.update(inst);
                log.info("agvphase:" + agvphase + "反馈成功,取货完成离开普通站点。");
                byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
//                OneNDCSocketConnectionAutoRun.write(data);
                agvphase = 0;
                index = 0;
                inst = null;
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "反馈成功");
            } else {
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "等待反馈");

            }
        }

        if (agvphase == 0x07) {
            if (ObjectUtil.isNotEmpty(inst)) {
                inst.setExecute_status("5");
                instructionService.update(inst);
                log.info("agvphase:" + agvphase + "反馈成功,放货完成。");
                byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
//                OneNDCSocketConnectionAutoRun.write(data);
                agvphase = 0;
                index = 0;
                inst = null;
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "反馈成功");
            } else {
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "等待反馈");
            }
        }

        if (agvphase == 0x09) {
            if (ObjectUtil.isNotEmpty(inst)) {
                inst.setExecute_status("6");
                instructionService.update(inst);
                log.info("agvphase:" + agvphase + "反馈成功,取货完成请求离开普通站点。");
                byte[] data = agvService.sendAgvOneModeInst(agvphase, index, 0,0,0,0,0);
//                OneNDCSocketConnectionAutoRun.write(data);
                agvphase = 0;
                index = 0;
                inst = null;
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "反馈成功");
            } else {
                logServer.deviceExecuteLog(this.device_code, "", "", "agvphase:" + agvphase + "等待反馈");

            }
        }

    }

    @Override
    public JSONObject getDeviceStatusName() {
        JSONObject jo = new JSONObject();
        String mode = "";
        String action = "";
        String move = "";
        if (this.getMode() == 0) {
            mode = "未联机";
        } else if (this.getMode() == 1) {
            mode = "单机";
        } else if (this.getMode() == 2) {
            mode = "联机";
        } else if (this.getMode() == 3) {
            mode = "运行中";
        }

        if (this.getMove() == 0) {
            move = "无货";
            jo.put("hasGoods", false);
        } else if (this.getMove() == 1) {
            move = "有货";
            jo.put("hasGoods", true);
        } else if (this.getMove() == 2) {
            move = "有托盘有货";
            jo.put("hasGoods", true);
        }
        jo.put("device_name", this.getDevice().getDevice_name());
        jo.put("mode", mode);
        jo.put("move", move);
        jo.put("action", action);
        jo.put("isOnline", true);
        jo.put("error", this.getError());
        jo.put("isError", this.getIserror());
        return jo;
    }


    @Override
    public void setDeviceStatus(JSONObject data) {

    }
}

