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

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.agv.server.AgvService;
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.AbstractOpcDeviceDriver;
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.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.system.service.param.ISysParamService;
import org.nl.config.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 插拔轴工位
 */
@Slf4j
@Data
@RequiredArgsConstructor
public class PlugPullDeviceSiteDeviceDriver extends AbstractOpcDeviceDriver implements DeviceDriver, ExecutableDeviceDriver, RouteableDeviceDriver, DeviceStageMonitor {
    protected ItemProtocol itemProtocol = new ItemProtocol(this);
    @Autowired
    DeviceAppService deviceAppservice = SpringContextHolder.getBean(DeviceAppService.class);
    @Autowired
    InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class);
    @Autowired
    DeviceService deviceservice = SpringContextHolder.getBean(DeviceService.class);
    @Autowired
    TaskService taskserver = SpringContextHolder.getBean(TaskService.class);
    @Autowired
    RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class);
    @Autowired
    AcsToWmsService acsToWmsService = SpringContextHolder.getBean(AcsToWmsServiceImpl.class);
    @Autowired
    ISysParamService paramService = SpringContextHolder.getBean(ISysParamService.class);
    @Autowired
    DeviceExecuteLogService logServer = SpringContextHolder.getBean(DeviceExecuteLogService.class);
    @Autowired
    AgvService agvService = SpringContextHolder.getBean(AgvService.class);

    private Date instruction_require_time = new Date();
    private Date instruction_finished_time = new Date();
    private Date instruction_apply_time = new Date();
    private int instruction_require_time_out = 3000;

    public int mode = 0;
    public int move = 0;
    public int action = 0;
    public int status = 0;
    public int control = 0;
    public int plug_finish = 0;
    public int pull_finish = 0;
    public int plug3_times = 0;
    public int plug6_times = 0;
    public int pull3_times = 0;
    public int pull6_times = 0;
    public int plug_alltimes = 0;
    public int pull_alltimes = 0;

    public int last_mode = 0;
    public int last_move = 0;
    public int last_action = 0;
    public int last_status = 0;
    public int last_control = 0;
    public int last_plug_finish = 0;
    public int last_pull_finish = 0;


    Boolean isonline = true;

    Boolean iserror = false;

    //1-执行任务；2-取货完成；3-放货完成；
    int flag;

    String device_code;

    @Override
    public Device getDevice() {
        return this.device;
    }

    //请求成功标记
    Boolean requireSucess = false;

    @Override
    public void execute() {
        String message = null;

        device_code = this.getDeviceCode();
        mode = this.itemProtocol.getItem_mode();
        move = this.itemProtocol.getItem_move();
        action = this.itemProtocol.getItem_action();
        status = this.itemProtocol.getItem_status();
        control = this.itemProtocol.getItem_control();
        plug_finish = this.itemProtocol.getItem_plug_finish();
        pull_finish = this.itemProtocol.getItem_pull_finish();
        plug3_times = this.itemProtocol.getItem_3plug_times();
        plug6_times = this.itemProtocol.getItem_6plug_times();
        pull3_times = this.itemProtocol.getItem_3pull_times();
        pull6_times = this.itemProtocol.getItem_6pull_times();
        plug_alltimes = this.itemProtocol.getItem_plug_alltimes();
        pull_alltimes = this.itemProtocol.getItem_pull_alltimes();




        if (status != 1) {
            this.setIsonline(false);
            this.setIserror(true);
            message = "有报警";
            //无报警
        } else {


        }

        last_mode = mode;
        last_move = move;
        last_action = action;
    }


    protected void thingToNothing() {
        //this.setRequireSucess(false);
    }

    public void writing(List list) {

        //String opcservcerid = this.getDevice().getOpc_server_id();
//Server server = ReadUtil.getServer(opcservcerid);
        Map<String, Object> itemMap = new HashMap<String, Object>();
        for (int i = 0; i < list.size(); i++) {
            Object ob = list.get(i);
            JSONObject json = (JSONObject) JSONObject.toJSON(ob);
            if (!StrUtil.isEmpty(json.getString("value"))) {
                String to_param = this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code()
                        + "." + json.getString("code");
                itemMap.put(to_param, json.getString("value"));
            }
        }
        logServer.deviceExecuteLog(device_code, "", "", "下发电气信号:" + itemMap);
        this.control(itemMap);
    }

    //将扩展表中的字符串数据转换成集合
    public List<String> getExtraDeviceCodes(String extraName) {
        String extraValue = (String) this.getDevice().getExtraValue().get(extraName);
        if (StrUtil.isEmpty(extraValue)) {
            return new ArrayList<>();
        }
        String devicesString = extraValue.substring(1, extraValue.length() - 1);
        List<String> devicesList = new ArrayList<>();
        String[] devices = devicesString.split(",");
        for (int i = 0; i < devices.length; i++) {
            String s = devices[i].replace("\"", "").replace("\"", "");
            devicesList.add(s);
        }
        return devicesList;
    }



    @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", this.getIsonline());
        jo.put("isError", this.getIserror());
        return jo;
    }

    @Override
    public void setDeviceStatus(JSONObject data) {

    }

    public void writing(String param, String value) {

        String to_param = this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code()
                + "." + param;
        //String opcservcerid = this.getDevice().getOpc_server_id();
//Server server = ReadUtil.getServer(opcservcerid);
        Map<String, Object> itemMap = new HashMap<String, Object>();

        itemMap.put(to_param, value);
//            itemMap.put(to_param, Integer.parseInt(value));
        this.control(itemMap);
    }


}
