package org.nl.acs.agv.server.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.AcsConfig;
import org.nl.acs.agv.server.AgvService;
import org.nl.acs.agv.server.dto.AgvDto;
import org.nl.acs.device.domain.Device;
import org.nl.acs.ext.wms.service.AcsToWmsService;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.device.enums.DeviceType;
import org.nl.acs.task.service.TaskService;
import org.nl.common.exception.BadRequestException;
import org.nl.system.service.param.ISysParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class AgvServiceImpl implements AgvService {

    @Autowired
    DeviceAppService deviceAppService;
    @Autowired
    TaskService taskService;
    @Autowired
    InstructionService instructionService;

    @Autowired
    ISysParamService paramService;
    @Autowired
    AcsToWmsService acsToWmsService;

    Map<String, AgvDto> AGVDeviceStatus = new HashMap();

    @Override
    public Map<String, AgvDto> findAllAgvFromCache() {
        return AGVDeviceStatus;
    }

    @Override
    public void updateAgvFromCache(AgvDto dto) {
        if (AGVDeviceStatus.containsKey(dto.getName())) {
            AGVDeviceStatus.remove(dto.getName());
        }
        AGVDeviceStatus.put(dto.getName(), dto);
    }


    @Override
    public String requestOpenOrCloseDoor(String device_code, String param) {
        log.info("AGV请求开关门参数，设备:{}，参数：{}", device_code, param);
        int type = Integer.parseInt(paramService.findByCode(AcsConfig.BUSINESSTYPE).getValue());
        switch (type) {
            case 4:
                acsToWmsService.applyOpenOrCloseDoor(device_code, param);
                break;
            default:
                break;

        }
        return null;
    }


    @Override
    public String queryAllDoorStatus() {
        log.info("AGV查询全部自动门状态");
        List<Device> list = deviceAppService.findDevice(DeviceType.autodoor);
        JSONArray ja = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            Device doordevice = deviceAppService.findDeviceByCode(list.get(i).getDevice_code());
            if (ObjectUtil.isEmpty(doordevice)) {
                throw new BadRequestException("未找到对应设备");
            }
            String mes = null;
            JSONObject jo = JSONObject.parseObject(mes);
            ja.add(jo);
        }
        log.info("AGV查询全部自动门状态,反馈：{}", ja.toString());
        return ja.toString();
    }


    public static void main(String[] args) {
        String address = "L1-01-01";
        if (address.indexOf("-") > 0) {
            String str = address.substring(address.indexOf("-"), address.length());
            address = address.substring(0, address.indexOf("-"));
        }
    }
}
