package at.procon.eventhub.processing.service;

import at.procon.eventhub.dto.EventHubEventDto;
import at.procon.eventhub.processing.model.UnifiedDiscoveredVehicleRef;
import at.procon.eventhub.processing.model.UnifiedDriverEventsRequest;
import at.procon.eventhub.processing.model.UnifiedEventSourceFamily;
import at.procon.eventhub.processing.model.UnifiedRuntimeEventBackend;
import at.procon.eventhub.processing.model.UnifiedRuntimeProcessingRequest;
import at.procon.eventhub.processing.model.UnifiedVehicleEventsRequest;
import java.util.List;
import org.springframework.stereotype.Component;

@Component
public class TachographFileSessionRuntimeEventLoader implements RuntimeDriverEventLoader, RuntimeVehicleEventLoader {

    private final UnifiedDriverEventSourceService driverEventSourceService;
    private final UnifiedVehicleEventSourceService vehicleEventSourceService;

    public TachographFileSessionRuntimeEventLoader(
            UnifiedDriverEventSourceService driverEventSourceService,
            UnifiedVehicleEventSourceService vehicleEventSourceService
    ) {
        this.driverEventSourceService = driverEventSourceService;
        this.vehicleEventSourceService = vehicleEventSourceService;
    }

    @Override
    public boolean supports(UnifiedRuntimeProcessingRequest request, UnifiedEventSourceFamily sourceFamily) {
        return request.eventBackend() == UnifiedRuntimeEventBackend.SOURCE_DB
                && sourceFamily == UnifiedEventSourceFamily.TACHOGRAPH_FILE_SESSION;
    }

    @Override
    public List<EventHubEventDto> loadDriverEvents(UnifiedRuntimeProcessingRequest request) {
        return driverEventSourceService.loadDriverEvents(
                UnifiedDriverEventsRequest.forTachographFileSession(
                        request.sessionId(),
                        request.driverKey(),
                        request.occurredFrom(),
                        request.occurredTo()
                )
        );
    }

    @Override
    public List<EventHubEventDto> loadVehicleEvents(
            UnifiedRuntimeProcessingRequest request,
            UnifiedDiscoveredVehicleRef vehicleRef
    ) {
        return vehicleEventSourceService.loadVehicleEvents(
                UnifiedVehicleEventsRequest.forTachographFileSession(
                        request.sessionId(),
                        vehicleRef.sourceVehicleEntityId(),
                        vehicleRef.vin(),
                        vehicleRef.registrationNation(),
                        vehicleRef.registrationNumber(),
                        request.vehicleOccurredFrom(),
                        request.vehicleOccurredTo()
                )
        );
    }
}
