package org.wildfly.swarm.container;

import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.jbossdeployment12.DependenciesType;
import org.jboss.shrinkwrap.descriptor.api.jbossdeployment12.JBossDeploymentStructureDescriptor;
import org.jboss.shrinkwrap.descriptor.api.jbossdeployment12.ModuleDependencyType;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import static org.wildfly.swarm.container.util.ClassLoading.withTCCL;

/**
 * @author Bob McWhirter
 */
public class JBossDeploymentStructureAsset implements Asset{

    public JBossDeploymentStructureAsset() {
        this.descriptor =
                withTCCL(Descriptors.class.getClassLoader(),
                         () -> Descriptors.create(JBossDeploymentStructureDescriptor.class));
    }

    public JBossDeploymentStructureAsset(InputStream fromStream) {
        this.descriptor =
                withTCCL(Descriptors.class.getClassLoader(),
                         () -> Descriptors.importAs(JBossDeploymentStructureDescriptor.class)
                                 .fromStream(fromStream));
    }


    public void addModule(final String name, final String slot) {
        System.out.println("TC: ADDING MODULE "+  name + " " + slot);
        final DependenciesType dependencies = this.descriptor
                .getOrCreateDeployment()
                .getOrCreateDependencies();
        final List<ModuleDependencyType> modules = dependencies.getAllModule();
        for (ModuleDependencyType each : modules) {
            if (name.equals(each.getName()) &&
                    slot.equals(each.getSlot())) {

                //module exists
                return;
            }
        }

        dependencies.createModule()
                .name(name)
                .slot(slot);
    }

    @Override
    public InputStream openStream() {
        String output = this.descriptor.exportAsString();
        System.out.println("TC: JBDS " + output);
        return new ByteArrayInputStream(output.getBytes());
    }

    private final JBossDeploymentStructureDescriptor descriptor;
}

@@ -1265,78 +1265,8 @@
 ) %7B%0A
-        System.out.println(%22TC: ADDING MODULE %22+  name + %22 %22 + slot);%0A
     
@@ -1934,57 +1934,8 @@
 ();%0A
-        System.out.println(%22TC: JBDS %22 + output);
 %0A   

Remove debug printlns.