package com.playtika.test.rabbitmq;

import com.playtika.test.common.spring.DockerPresenceBootstrapConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.testcontainers.containers.RabbitMQContainer;

import java.util.LinkedHashMap;

import static com.playtika.test.common.utils.ContainerUtils.configureCommonsAndStart;
import static com.playtika.test.rabbitmq.RabbitMQProperties.BEAN_NAME_EMBEDDED_RABBITMQ;

@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.rabbitmq.enabled", matchIfMissing = true)
@EnableConfigurationProperties(RabbitMQProperties.class)
public class EmbeddedRabbitMQBootstrapConfiguration {

    @Bean(name = BEAN_NAME_EMBEDDED_RABBITMQ, destroyMethod = "stop")
    public RabbitMQContainer rabbitmq(
            ConfigurableEnvironment environment,
            RabbitMQProperties properties) {
        log.info("Starting RabbitMQ server. Docker image: {}", properties.getDockerImage());

        RabbitMQContainer rabbitMQ =
                new RabbitMQContainer(properties.getDockerImage())
                        .withAdminPassword(properties.getPassword())
                        .withEnv("RABBITMQ_DEFAULT_VHOST", properties.getVhost())
                        .withExposedPorts(properties.getPort(), properties.getHttpPort());
        rabbitMQ = (RabbitMQContainer) configureCommonsAndStart(rabbitMQ, properties, log);
        registerRabbitMQEnvironment(rabbitMQ, environment, properties);
        return rabbitMQ;
    }


    private void registerRabbitMQEnvironment(RabbitMQContainer rabbitMQ,
                                             ConfigurableEnvironment environment,
                                             RabbitMQProperties properties) {
        Integer mappedPort = rabbitMQ.getMappedPort(properties.getPort());
        Integer mappedHttpPort = rabbitMQ.getMappedPort(properties.getHttpPort());
        String host = rabbitMQ.getContainerIpAddress();

        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("embedded.rabbitmq.port", mappedPort);
        map.put("embedded.rabbitmq.host", host);
        map.put("embedded.rabbitmq.vhost", properties.getVhost());
        map.put("embedded.rabbitmq.user", rabbitMQ.getAdminUsername());
        map.put("embedded.rabbitmq.password", rabbitMQ.getAdminPassword());
        map.put("embedded.rabbitmq.httpPort", mappedHttpPort);

        log.info("Started RabbitMQ server. Connection details: {}", map);

        MapPropertySource propertySource = new MapPropertySource("embeddedRabbitMqInfo", map);
        environment.getPropertySources().addFirst(propertySource);
    }
}

@@ -733,16 +733,67 @@
 ntainer;
+%0Aimport org.testcontainers.utility.DockerImageName;
 %0A%0Aimport
@@ -1339,16 +1339,125 @@
 tion %7B%0A%0A
+    private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse(%22rabbitmq:3.8-alpine%22);%0A%0A
     @Bea
@@ -1820,27 +1820,16 @@
 ntainer(
-properties.
 getDocke
@@ -1834,17 +1834,31 @@
 kerImage
-(
+Name(properties
 ))%0A     
@@ -2258,16 +2258,16 @@
 rties);%0A
-
         
@@ -2290,16 +2290,373 @@
 %0A    %7D%0A%0A
+    private static DockerImageName getDockerImageName(RabbitMQProperties properties) %7B%0A        DockerImageName imageName = DockerImageName.parse(properties.getDockerImage());%0A        if (imageName.isCompatibleWith(DEFAULT_IMAGE_NAME)) %7B%0A            return imageName;%0A        %7D%0A%0A        return imageName.asCompatibleSubstituteFor(DEFAULT_IMAGE_NAME);%0A    %7D%0A%0A
 %0A    pri

add ability to specify custom compatible image (#770)