package io.quarkus.kubernetes.deployment;

import io.dekorate.kubernetes.decorator.AddInitContainerDecorator;
import io.dekorate.kubernetes.decorator.AddSidecarDecorator;
import io.dekorate.kubernetes.decorator.ApplicationContainerDecorator;
import io.dekorate.kubernetes.decorator.ApplyImageDecorator;
import io.dekorate.kubernetes.decorator.Decorator;
import io.dekorate.kubernetes.decorator.ResourceProvidingDecorator;
import io.fabric8.kubernetes.api.model.ContainerFluent;

/**
 * A decorator for applying an image to a container capable of overriding the internal {@link ApplyImageDecorator}.
 */
public class ApplyContainerImageDecorator extends ApplicationContainerDecorator<ContainerFluent> {
    private final String image;

    public ApplyContainerImageDecorator(String containerName, String image) {
        super((String) null, containerName);
        this.image = image;
    }

    public ApplyContainerImageDecorator(String deploymentName, String containerName, String image) {
        super(deploymentName, containerName);
        this.image = image;
    }

    public void andThenVisit(ContainerFluent container) {
        container.withImage(this.image);
    }

    @Override
    public Class<? extends Decorator>[] after() {
        return new Class[] { ResourceProvidingDecorator.class, AddSidecarDecorator.class, AddInitContainerDecorator.class,
                ApplyImageDecorator.class };
    }

}

@@ -36,16 +36,214 @@
 yment;%0A%0A
+import static io.dekorate.ConfigReference.generateConfigReferenceName;%0A%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport io.dekorate.ConfigReference;%0Aimport io.dekorate.WithConfigReferences;%0A
 import i
@@ -293,32 +293,32 @@
 ainerDecorator;%0A
-
 import io.dekora
@@ -605,32 +605,66 @@
 idingDecorator;%0A
+import io.dekorate.utils.Strings;%0A
 import io.fabric
@@ -924,16 +924,56 @@
 rFluent%3E
+%0A        implements WithConfigReferences
  %7B%0A    p
@@ -1633,16 +1633,16 @@
 .class,%0A
-
         
@@ -1685,10 +1685,1102 @@
 %0A    %7D%0A%0A
+    @Override%0A    public List%3CConfigReference%3E getConfigReferences() %7B%0A        return Arrays.asList(buildConfigReferenceForImage());%0A    %7D%0A%0A    private ConfigReference buildConfigReferenceForImage() %7B%0A        String property = generateConfigReferenceName(%22image%22, getContainerName(), getDeploymentName());%0A        String jsonPath = %22$..spec.template.spec.containers..image%22;%0A        if (!Strings.equals(getDeploymentName(), ANY) && !Strings.equals(getContainerName(), ANY)) %7B%0A            jsonPath = %22$.%5B?(@.metadata.name == '%22 + getDeploymentName() + %22')%5D.spec.template.spec.containers%5B?(@.name == '%22%0A                    + getContainerName() + %22')%5D.image%22;%0A        %7D else if (!Strings.equals(getDeploymentName(), ANY)) %7B%0A            jsonPath = %22$.%5B?(@.metadata.name == '%22 + getDeploymentName() + %22')%5D.spec.template.spec.containers..image%22;%0A        %7D else if (!Strings.equals(getContainerName(), ANY)) %7B%0A            jsonPath = %22$..spec.template.spec.containers%5B?(@.name == '%22 + getContainerName() + %22')%5D.image%22;%0A        %7D%0A%0A        return new ConfigReference(property, jsonPath, image);%0A    %7D%0A%0A
 %7D%0A

Update apply container decorator to implement WithConfigRef