package com.knecon.fforesight.service.layoutparser.server.queue;

import java.util.Set;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingQueueNames;
import com.knecon.fforesight.tenantcommons.TenantProvider;
import com.knecon.fforesight.tenantcommons.model.TenantCreatedEvent;
import com.knecon.fforesight.tenantcommons.model.TenantQueueConfiguration;
import com.knecon.fforesight.tenantcommons.model.TenantResponse;
import com.knecon.fforesight.tenantcommons.queue.RabbitQueueFromExchangeService;
import com.knecon.fforesight.tenantcommons.queue.TenantExchangeMessageReceiver;

@Service
public class TenantExchangeMessageReceiverImpl extends TenantExchangeMessageReceiver {

    public TenantExchangeMessageReceiverImpl(RabbitQueueFromExchangeService rabbitQueueService, TenantProvider tenantProvider) {

        super(rabbitQueueService, tenantProvider);
    }


    @Override
    protected Set<TenantQueueConfiguration> getTenantQueueConfigs() {

        return Set.of(TenantQueueConfiguration.builder()
                              .listenerId(MessageHandler.LAYOUT_PARSING_REQUEST_LISTENER_ID)
                              .exchangeName(LayoutParsingQueueNames.LAYOUT_PARSING_REQUEST_EXCHANGE)
                              .queuePrefix(LayoutParsingQueueNames.LAYOUT_PARSING_REQUEST_QUEUE_PREFIX)
                              .dlqName(LayoutParsingQueueNames.LAYOUT_PARSING_DLQ)
                              .build());
    }


    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {

        System.out.println("application ready invoked");
        super.initializeQueues();
    }


    @RabbitHandler
    @RabbitListener(queues = "#{tenantMessagingConfigurationImpl.getTenantCreatedQueueName()}")
    public void reactToTenantCreation(TenantCreatedEvent tenantCreatedEvent) {

        super.reactToTenantCreation(tenantCreatedEvent);
    }


    @RabbitHandler
    @RabbitListener(queues = "#{tenantMessagingConfigurationImpl.getTenantDeletedQueueName()}")
    public void reactToTenantDeletion(TenantResponse tenantResponse) {

        super.reactToTenantDeletion(tenantResponse);

    }

}
