package com.iailab.module.data.channel.common.service;

import com.iailab.module.data.channel.http.entity.HttpApiEntity;
import com.iailab.module.data.channel.http.service.HttpApiService;
import com.iailab.module.data.channel.kio.entity.ChannelKioDeviceEntity;
import com.iailab.module.data.channel.kio.service.ChannelKioDeviceService;
import com.iailab.module.data.channel.modbus.dto.ChannelModBusDeviceDTO;
import com.iailab.module.data.channel.modbus.entity.ChannelModBusDeviceEntity;
import com.iailab.module.data.channel.modbus.service.ChannelModbusDeviceService;
import com.iailab.module.data.channel.opcda.dto.ChannelOPCDADeviceDTO;
import com.iailab.module.data.channel.opcda.entity.ChannelOPCDADeviceEntity;
import com.iailab.module.data.channel.opcda.service.ChannelOPCDADeviceService;
import com.iailab.module.data.channel.opcua.dto.ChannelOPCUADeviceDTO;
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUADeviceEntity;
import com.iailab.module.data.channel.opcua.service.ChannelOPCUADeviceService;
import com.iailab.module.data.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年10月31日
 */
@Component
public class ChannelSourceService {

    @Autowired
    private ChannelOPCDADeviceService channelOPCDADeviceService;

    @Autowired
    private ChannelOPCUADeviceService channelOPCUADeviceService;

    @Autowired
    private ChannelModbusDeviceService channelModbusDeviceService;

    @Autowired
    private ChannelKioDeviceService channelKioDeviceService;

    @Autowired
    private HttpApiService httpApiService;

    public Map<String, Map<String, String>> getSourcesId() {

        Map<String, Map<String, String>> result = new HashMap<>();

        Map<String, String> opcdaMap = new HashMap<>();
        List<ChannelOPCDADeviceEntity> opcdaList = channelOPCDADeviceService.list(new HashMap<>());
        opcdaList.forEach(opcda -> {
            opcdaMap.put(opcda.getServerName(), opcda.getId());
        });
        result.put(DataSourceType.OPCDA.getCode(), opcdaMap);

        Map<String, String> opcuaMap = new HashMap<>();
        List<ChannelOPCUADeviceEntity> opcuaList = channelOPCUADeviceService.list(new HashMap<>());
        opcuaList.forEach(opcUa -> {
            opcuaMap.put(opcUa.getServerName(), opcUa.getId());
        });
        result.put(DataSourceType.OPCUA.getCode(), opcuaMap);

        Map<String, String> modbusMap = new HashMap<>();
        List<ChannelModBusDeviceEntity> modbusList = channelModbusDeviceService.list(new HashMap<>());
        modbusList.forEach(modbus -> {
            modbusMap.put(modbus.getName(), modbus.getId());
        });
        result.put(DataSourceType.ModBus.getCode(), modbusMap);

        Map<String, String> kioMap = new HashMap<>();
        List<ChannelKioDeviceEntity> kioList = channelKioDeviceService.list(new HashMap<>());
        kioList.forEach(kio -> {
            kioMap.put(kio.getInstanceName(), kio.getId());
        });
        result.put(DataSourceType.KIO.getCode(), kioMap);

        Map<String, String> httpMap = new HashMap<>();
        List<HttpApiEntity> httpList = httpApiService.list();
        httpList.forEach(http -> {
            httpMap.put(http.getCode(), http.getId());
        });
        result.put(DataSourceType.HTTP.getCode(), httpMap);

        return result;
    }
}