/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sling.caconfig.impl.override;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.sling.caconfig.spi.ConfigurationOverrideProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.Designate;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

/**
 * Provides parameter override map from OSGi factory configuration.
 */
@Component(service = ConfigurationOverrideProvider.class, immediate = true)
@Designate(ocd = OsgiConfigurationOverrideProvider.Config.class, factory = true)
public final class OsgiConfigurationOverrideProvider implements ConfigurationOverrideProvider {

    @ObjectClassDefinition(name = "Apache Sling Context-Aware Configuration Override Provider: OSGi configuration",
            description = "Allows to override configuration property values from OSGi configurations.")
    public static @interface Config {

        @AttributeDefinition(name = "Description",
                description = "This description is used for display in the web console.")
        String description();

        @AttributeDefinition(name = "Overrides",
                description = "Override strings - examples:<br>\n"
                + "{configName}/{propertyName}={propertyJsonValue}<br>\n"
                + "{configName}={propertyJsonObject}<br>\n"
                + "[{contextPath}]{configName}/{propertyName}={propertyJsonValue}<br>\n"
                + "[{contextPath}]{configName}={propertyJsonObject}")
        String[] overrides();

        @AttributeDefinition(name = "Enabled",
                description = "Enable this override provider.")
        boolean enabled() default false;

        @AttributeDefinition(name = "Service Ranking",
                description = "Priority of configuration override providers (higher = higher priority).")
        int service_ranking() default 100;
        
        String webconsole_configurationFactory_nameHint() default "{description}, enabled={enabled}";

    }

    private Collection<String> overrideStrings;

    @Activate
    void activate(Config config) {
        List<String> overrides = new ArrayList<>();
        if (config.enabled()) {
            overrides.addAll(Arrays.asList(config.overrides()));
        }
        this.overrideStrings = overrides;
    }

    @Override
    public Collection<String> getOverrideStrings() {
        return overrideStrings;
    }
    
}

@@ -2198,20 +2198,16 @@
 xamples:
-%3Cbr%3E
 %5Cn%22%0A    
@@ -2260,36 +2260,32 @@
 opertyJsonValue%7D
-%3Cbr%3E
 %5Cn%22%0A            
@@ -2324,20 +2324,16 @@
 nObject%7D
-%3Cbr%3E
 %5Cn%22%0A    
@@ -2413,12 +2413,8 @@
 lue%7D
-%3Cbr%3E
 %5Cn%22%0A

remove <br> from property description, not interpreted by web console