/*-
 * ~~~~~~licensing~~~~~~
 * system-component
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed 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.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.esb.system.component;

import org.apache.camel.Exchange;
import org.apache.camel.support.DefaultProducer;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.entaxy.esb.system.common.exception.ProfileNotFound;
import ru.entaxy.esb.system.common.osgi.OSGIUtils;
import ru.entaxy.esb.system.registry.systems.profile.SystemCollectorListener;
import ru.entaxy.esb.system.registry.systems.profile.SystemProfile;


public class SystemProducer extends DefaultProducer {

    private static final Logger LOG = LoggerFactory.getLogger(SystemProducer.class);
    private final SystemEndpoint endpoint;

    private SystemCollectorListener systemCollectorListener;

    public SystemProducer(SystemEndpoint endpoint) {
        super(endpoint);
        this.endpoint = endpoint;
    }

    public void process(Exchange exchange) throws Exception {
        LOG.debug("In SystemProducer " + exchange.getIn().getBody());

        //get System Profile from systemRegistry endpoint.getSystemName()
        SystemProfile systemProfile = null;

        systemProfile = getSystemProfile(endpoint.getSystemName());

        LOG.debug("Called system profile " + (systemProfile != null ? systemProfile.getSystemName() : "NULL"));

        systemProfile.send(exchange);

    }

    public SystemProfile getSystemProfile(String nameSystem) throws ProfileNotFound {
        systemCollectorListener = getSystemProfileNamedListener();
        LOG.debug("Registry SystemProfile {}; systems count {}", nameSystem, systemCollectorListener.getReferenceNames().size());
        SystemProfile systemProfile = null;
        if (systemCollectorListener.isRegistered(nameSystem)) {
            systemProfile = (SystemProfile) systemCollectorListener.getReference(nameSystem);
        } else {
            throw new ProfileNotFound("Profile for system " + nameSystem + " not found");
        }
        return systemProfile;
    }

    public SystemCollectorListener getSystemProfileNamedListener() {
        if (systemCollectorListener == null) {
            systemCollectorListener = (SystemCollectorListener) OSGIUtils.getServiceReference(
                    FrameworkUtil.getBundle(SystemProducer.class).getBundleContext(),
                    SystemCollectorListener.class.getName());
        }
        return systemCollectorListener;
    }
}
