package org.nl.acs.opc;

import lombok.extern.slf4j.Slf4j;
import org.nl.acs.device.enums.DeviceType;
import org.nl.acs.device.service.DeviceDriverDefinationAppService;
import org.nl.acs.device_driver.DeviceDriver;
import org.nl.acs.device_driver.DeviceDriverDefination;
import org.nl.common.exception.BadRequestException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 20220102CG\noblelift
 */
@Service
@Slf4j
public class DeviceDriverDefinationAppServiceImpl implements DeviceDriverDefinationAppService, InitializingBean, ApplicationContextAware {
    @Autowired
    private Map<String, DeviceDriverDefination> deviceDriverDefinations = new HashMap();
    @Autowired(
            required = false
    )
    List<DeviceDriverDefination> deviceDriverDefinationsx;

    public DeviceDriverDefinationAppServiceImpl() {
        this.deviceDriverDefinations = Collections.synchronizedMap(this.deviceDriverDefinations);
    }

    @Override
    public List<String> getDeviceDriverCodes() {
        return null;
    }

    @Override
    public DeviceDriverDefination getDeviceDriverDefination(String driverCode) {
        return (DeviceDriverDefination) this.deviceDriverDefinations.get(driverCode);
    }

    @Override
    public List<DeviceDriverDefination> getDeviceDriverDefinations() {
        return new LinkedList(this.deviceDriverDefinations.values());
    }

    @Override
    public List<DeviceDriverDefination> getDeviceDriverDefinations(DeviceType deviceType) {
        Collection<DeviceDriverDefination> definations = this.deviceDriverDefinations.values();
        List<DeviceDriverDefination> deviceTypeDefinations = new LinkedList();
        Iterator var4 = definations.iterator();

        while (var4.hasNext()) {
            DeviceDriverDefination defination = (DeviceDriverDefination) var4.next();
            if (defination.getFitDeviceTypes() == null || defination.getDriverCode() == null || defination.getDeviceDriverType() == null) {
                throw new BadRequestException("当前驱动defination定义异常：" + defination.getClass());
            }

            if (defination.getFitDeviceTypes().contains(deviceType)) {
                deviceTypeDefinations.add(defination);
            }
        }

        return deviceTypeDefinations;
    }

    @Override
    public boolean isRoute(Class<? extends DeviceDriver> var1) {
        return false;
    }

    @Override
    public boolean isOpc(Class<? extends DeviceDriver> var1) {
        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.initial();
    }

    public void initial() {
        if (this.deviceDriverDefinationsx != null) {
            Iterator var1 = this.deviceDriverDefinationsx.iterator();

            DeviceDriverDefination deviceDriverDefination;
            while (var1.hasNext()) {
                deviceDriverDefination = (DeviceDriverDefination) var1.next();
                if (deviceDriverDefination.getDriverCode() != null && deviceDriverDefination.getFitDeviceTypes() != null && deviceDriverDefination.getFitDeviceTypes() != null) {
                    this.addDeviceDriverDefination(deviceDriverDefination.getDriverCode(), deviceDriverDefination);
                }

                //log.warn("{}驱动定义异常", deviceDriverDefination.getClass());
            }

        }
    }

    private void addDeviceDriverDefination(String driverCode, DeviceDriverDefination deviceDriverDefination) {
        this.deviceDriverDefinations.put(driverCode, deviceDriverDefination);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    }
}
