/*
 * 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.plc4x.kafka.config;

import org.apache.plc4x.kafka.Plc4xSinkConnector;
import org.apache.kafka.common.config.AbstractConfig;
import org.apache.kafka.common.config.Config;
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.config.ConfigValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SinkConfig extends AbstractConfig {

    private static final Logger log = LoggerFactory.getLogger(SinkConfig.class);

    private final List<Sink> sinks;

    public SinkConfig(Map originals) {
        super(configDef(), originals);

        sinks = new ArrayList<>(getList(Constants.SINKS_CONFIG).size());
        for (String sink : getList(Constants.SINKS_CONFIG)) {
            sinks.add(new Sink(sink, originalsWithPrefix(Constants.SINKS_CONFIG + "." + sink + ".")));
        }
    }

    public void validate() throws ConfigException {
        for (Sink sink : sinks) {
            sink.validate();
        }
    }

    public List<Sink> getSinks() {
        return sinks;
    }

    public Sink getSink(String sinkName) {
        if(sinks == null) {
            return null;
        }
        return sinks.stream().filter(sink -> sink.getName().equals(sinkName)).findFirst().orElse(null);
    }

    public static ConfigDef configDef() {
        return new ConfigDef()
            .define(Constants.SINKS_CONFIG,
                    ConfigDef.Type.LIST,
                    ConfigDef.Importance.HIGH,
                    Constants.SINKS_DOC);
    }

    @Override
    public String toString() {
        StringBuilder query = new StringBuilder();
        for (Sink sink : sinks) {
            query.append(sink.toString());
        }
        return query.toString();
    }

}
