from typing import Union, Optional

from messagebus import MessageBus

from externals.repository import MessageBusRepository
from nperipherals_core.service import ReaderDeviceServiceThread
from nperipherals_core.model.device import Device, ReaderDevice, WriterDevice


class DeviceServiceThreadFactory(object):
    def __init__(self, message_bus):
        # type: (MessageBus) -> None

        self.message_bus = message_bus

    def create_device_service_thread(self, device):
        # type: (Union[Device, ReaderDevice, WriterDevice]) -> Optional[ReaderDeviceServiceThread]

        device_type = type(device)  # type: str

        if issubclass(device_type, ReaderDevice):
            return ReaderDeviceServiceThread(device, notification_repository=MessageBusRepository(self.message_bus))
