// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.clients;

import com.yahoo.component.ComponentId;
import com.yahoo.component.ComponentSpecification;
import com.yahoo.component.chain.Phase;
import com.yahoo.component.chain.dependencies.Dependencies;
import com.yahoo.component.chain.model.ChainSpecification;
import com.yahoo.component.chain.model.ChainedComponentModel;
import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.vespa.model.container.ContainerCluster;
import com.yahoo.vespa.model.container.component.Handler;
import com.yahoo.vespa.model.container.component.chain.ProcessingHandler;
import com.yahoo.vespa.model.container.search.ContainerSearch;
import com.yahoo.vespa.model.container.search.searchchain.SearchChain;
import com.yahoo.vespa.model.container.search.searchchain.SearchChains;
import com.yahoo.vespa.model.container.search.searchchain.Searcher;
import com.yahoo.vespaclient.config.FeederConfig;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Einar M R Rosenvinge
 */
public class ContainerDocumentApi implements FeederConfig.Producer {

    public static final String vespaClientBundleSpecification = "vespaclient-container-plugin";
    private final Options options;

    public ContainerDocumentApi(ContainerCluster cluster, Options options) {
        this.options = options;
        setupHandlers(cluster);
    }

    private void setupHandlers(ContainerCluster cluster) {
        cluster.addComponent(newVespaClientHandler("com.yahoo.document.restapi.resource.RestApi", "document/v1/*"));
        cluster.addComponent(newVespaClientHandler("com.yahoo.vespa.http.server.FeedHandler", ContainerCluster.RESERVED_URI_PREFIX + "/feedapi"));
    }

    private Handler newVespaClientHandler(String componentId, String bindingSuffix) {
        Handler<AbstractConfigProducer<?>> handler = new Handler<>(new ComponentModel(
                BundleInstantiationSpecification.getFromStrings(componentId, null, vespaClientBundleSpecification), ""));

        for (String rootBinding : options.bindings) {
            handler.addServerBindings(rootBinding + bindingSuffix,
                    rootBinding + bindingSuffix + '/');
        }
        return handler;
    }

    @Override
    public void getConfig(FeederConfig.Builder builder) {
        if (options.abortondocumenterror != null)
            builder.abortondocumenterror(options.abortondocumenterror);
        if (options.route!= null)
            builder.route(options.route);
        if (options.maxpendingdocs != null)
            builder.maxpendingdocs(options.maxpendingdocs);
        if (options.maxpendingbytes != null)
            builder.maxpendingbytes(options.maxpendingbytes);
        if (options.retryenabled != null)
            builder.retryenabled(options.retryenabled);
        if (options.retrydelay != null)
            builder.retrydelay(options.retrydelay);
        if (options.timeout != null)
            builder.timeout(options.timeout);
        if (options.tracelevel != null)
            builder.tracelevel(options.tracelevel);
        if (options.mbusport != null)
            builder.mbusport(options.mbusport);
        if (options.docprocChain != null)
            builder.docprocchain(options.docprocChain);
    }

    public static final class Options {
        private final Collection<String> bindings;
        private final Boolean abortondocumenterror;
        private final String route;
        private final Integer maxpendingdocs;
        private final Integer maxpendingbytes;
        private final Boolean retryenabled;
        private final Double retrydelay;
        private final Double timeout;
        private final Integer tracelevel;
        private final Integer mbusport;
        private final String docprocChain;

        public Options(Collection<String> bindings,
                       Boolean abortondocumenterror,
                       String route,
                       Integer maxpendingdocs,
                       Integer maxpendingbytes,
                       Boolean retryenabled,
                       Double retrydelay,
                       Double timeout,
                       Integer tracelevel,
                       Integer mbusport,
                       String docprocChain) {

            this.bindings = Collections.unmodifiableCollection(bindings);
            this.abortondocumenterror = abortondocumenterror;
            this.route = route;
            this.maxpendingdocs = maxpendingdocs;
            this.maxpendingbytes = maxpendingbytes;
            this.retryenabled = retryenabled;
            this.retrydelay = retrydelay;
            this.timeout = timeout;
            this.tracelevel = tracelevel;
            this.mbusport = mbusport;
            this.docprocChain = docprocChain;
        }
    }

}

@@ -175,944 +175,284 @@
 o.co
-mponent.ComponentId;%0Aimport com.yahoo.component.ComponentSpecification;%0Aimport com.yahoo.component.chain.Phase;%0Aimport com.yahoo.component.chain.dependencies.Dependencies;%0Aimport com.yahoo.component.chain.model.ChainSpecification;%0Aimport com.yahoo.component.chain.model.ChainedComponentModel;%0Aimport com.yahoo.config.model.producer.AbstractConfigProducer;%0Aimport com.yahoo.container.bundle.BundleInstantiationSpecification;%0Aimport com.yahoo.osgi.provider.model.ComponentModel;%0Aimport com.yahoo.vespa.model.container.ContainerCluster;%0Aimport com.yahoo.vespa.model.container.component.Handler;%0Aimport com.yahoo.vespa.model.container.component.chain.ProcessingHandler;%0Aimport com.yahoo.vespa.model.container.search.ContainerSearch;%0Aimport com.yahoo.vespa.model.container.search.searchchain.SearchChain;%0Aimport com.yahoo.vespa.model.container.search.searchchain.SearchChains;%0Aimport com.yahoo.vespa.model.container.search.searchchain.Search
+nfig.model.producer.AbstractConfigProducer;%0Aimport com.yahoo.container.bundle.BundleInstantiationSpecification;%0Aimport com.yahoo.osgi.provider.model.ComponentModel;%0Aimport com.yahoo.vespa.model.container.ContainerCluster;%0Aimport com.yahoo.vespa.model.container.component.Handl
 er;%0A
@@ -506,36 +506,8 @@
 g;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -564,56 +564,8 @@
 ons;
-%0Aimport java.util.Set;%0Aimport java.util.TreeSet;
 %0A%0A/*

Optimize imports