/*-
 * ~~~~~~licensing~~~~~~
 * bridge-profile-manager
 * ==========
 * 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.management.bridge.profile.manager.sender;

import com.google.gson.Gson;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;

public class BridgeProfileSenderImpl implements BridgeProfileSender {

    private static final String HEADER_BRIDGE_NAME = "NTX_Bridge";
    private static final String HEADER_COMMAND = "NTX_CommandBridgeProfileManager";
    private static final String HEADER_DESTINATION_SYSTEM = "NTX_DestinationQueueAfterBridge";

    private static final Gson gson = new Gson();
    
    private CamelContext context;

    @Override
    public String sendOnOtherEsb(BridgeProfileType bridgeProfileType, String esbName, String command) {
        Exchange exchange = ExchangeBuilder.anExchange(context)
                .withHeader(HEADER_DESTINATION_SYSTEM, "profile-controller-route")
                .withHeader(HEADER_BRIDGE_NAME, esbName)
                .withHeader(HEADER_COMMAND, command)
                .withBody(gson.toJson(bridgeProfileType))
                .build();
        
        ProducerTemplate template = exchange.getContext().createProducerTemplate();
        template.send("direct-vm:send-message", exchange);

        return exchange.getOut() != null && exchange.getOut().getBody() != null ?
                exchange.getOut().getBody().toString() : null;
    }

	public CamelContext getContext() {
		return context;
	}

	public void setContext(CamelContext context) {
		this.context = context;
	}
    
    
}
