
package com.mortals.xhx.queue.provider;

import com.mortals.xhx.queue.*;
import com.mortals.xhx.queue.rabbitmq.TbRabbitMqConsumerTemplate;
import com.mortals.xhx.queue.rabbitmq.TbRabbitMqProducerTemplate;
import com.mortals.xhx.queue.rabbitmq.TbRabbitMqSettings;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@ConditionalOnExpression("'${queue.type:null}'=='rabbitmq'")
public class RabbitMqTbCoreQueueFactory implements TbCoreQueueFactory {

    private final TbRabbitMqSettings rabbitMqSettings;
    private final TbQueueCoreSettings coreSettings;

    public RabbitMqTbCoreQueueFactory(TbRabbitMqSettings rabbitMqSettings, TbQueueCoreSettings coreSettings) {
        this.rabbitMqSettings = rabbitMqSettings;
        this.coreSettings = coreSettings;

    }

    @Override
    public TbQueueProducer<TbQueueMsg> createMsgProducer() {
        return new TbRabbitMqProducerTemplate<>(rabbitMqSettings, coreSettings.getTopic());

    }

    @Override
    public TbQueueConsumer<TbQueueMsg> createMsgConsumer() {
        return new TbRabbitMqConsumerTemplate<>(rabbitMqSettings, coreSettings.getTopic(), msg -> new TbQueueMsg() {
            @Override
            public UUID getKey() {
                return msg.getKey();
            }

            @Override
            public TbQueueMsgHeaders getHeaders() {

                return msg.getHeaders();
            }

            @Override
            public byte[] getData() {
                return msg.getData();
            }
        });
    }
}
