/**
 * Copyright (c) 2010-2021 Contributors to the openHAB project
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.openhab.binding.miio.internal;

import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;

import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.miio.internal.basic.BasicChannelTypeProvider;
import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
import org.openhab.binding.miio.internal.cloud.CloudConnector;
import org.openhab.binding.miio.internal.handler.MiIoBasicHandler;
import org.openhab.binding.miio.internal.handler.MiIoGenericHandler;
import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.openhab.core.thing.type.ChannelTypeRegistry;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
 * handlers.
 *
 * @author Marcel Verpaalen - Initial contribution
 */
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
@NonNullByDefault
public class MiIoHandlerFactory extends BaseThingHandlerFactory {
    private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
    protected final ScheduledExecutorService scheduler = ThreadPoolManager
            .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);

    private MiIoDatabaseWatchService miIoDatabaseWatchService;
    private CloudConnector cloudConnector;
    private ChannelTypeRegistry channelTypeRegistry;
    private BasicChannelTypeProvider basicChannelTypeProvider;

    @Activate
    public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry,
            @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
            @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
        this.miIoDatabaseWatchService = miIoDatabaseWatchService;
        this.cloudConnector = cloudConnector;
        @Nullable
        String username = (String) properties.get("username");
        @Nullable
        String password = (String) properties.get("password");
        @Nullable
        String country = (String) properties.get("country");
        cloudConnector.setCredentials(username, password, country);
        scheduler.submit(() -> cloudConnector.isConnected());
        this.channelTypeRegistry = channelTypeRegistry;
        this.basicChannelTypeProvider = basicChannelTypeProvider;
    }

    @Override
    public boolean supportsThingType(ThingTypeUID thingTypeUID) {
        return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
    }

    @Override
    protected @Nullable ThingHandler createHandler(Thing thing) {
        ThingTypeUID thingTypeUID = thing.getThingTypeUID();
        if (thingTypeUID.equals(THING_TYPE_MIIO)) {
            return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
        }
        if (thingTypeUID.equals(THING_TYPE_BASIC)) {
            return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
                    basicChannelTypeProvider);
        }
        if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
            return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
        }
        return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector);
    }
}

@@ -513,16 +513,108 @@
 il.Map;%0A
+import java.util.concurrent.Future;%0Aimport java.util.concurrent.RejectedExecutionException;%0A
 import j
@@ -1762,25 +1762,140 @@
 tations.
-Reference
+Deactivate;%0Aimport org.osgi.service.component.annotations.Reference;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0A/**%0A 
@@ -2427,17 +2427,16 @@
 _NAME);%0A
-%0A
     priv
@@ -2648,16 +2648,154 @@
 rovider;
+%0A    private @Nullable Future%3CBoolean%3E scheduledTask;%0A    private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
 %0A%0A    @A
@@ -3170,24 +3170,146 @@
 tchService;%0A
+        this.channelTypeRegistry = channelTypeRegistry;%0A        this.basicChannelTypeProvider = basicChannelTypeProvider;%0A
         this
@@ -3663,183 +3663,635 @@
     
-scheduler.submit(() -%3E cloudConnector.isConnected());%0A        this.channelTypeRegistry = channelTypeRegistry;%0A        this.basicChannelTypeProvider = basicChannelTypeProvider;
+try %7B%0A            if (!scheduler.isShutdown()) %7B%0A                scheduledTask = scheduler.submit(() -%3E cloudConnector.isConnected());%0A            %7D else %7B%0A                logger.debug(%22Unexpected: ScheduledExecutorService is shutdown.%22);%0A            %7D%0A        %7D catch (RejectedExecutionException e) %7B%0A            logger.debug(%22Unexpected: ScheduledExecutorService task rejected.%22, e);%0A        %7D%0A    %7D%0A%0A    @Deactivate%0A    private void dispose() %7B%0A        final Future%3CBoolean%3E scheduledTask = this.scheduledTask;%0A        if (scheduledTask != null && !scheduledTask.isDone()) %7B%0A            scheduledTask.cancel(true);%0A        %7D
 %0A   

fix bundle restart error (#10190)