/*-
 * ~~~~~~licensing~~~~~~
 * test-producers
 * ==========
 * Copyright (C) 2020 - 2022 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.platform.core.producer.executor.generationmodel;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
import ru.entaxy.platform.base.objects.EntaxyObject;
import ru.entaxy.platform.base.objects.factory.EntaxyFactoryException;
import ru.entaxy.platform.core.producer.api.EntaxyProducerService;
import ru.entaxy.platform.core.producer.api.ProducerResult;
import ru.entaxy.platform.core.producer.api.ProducerResult.CommandResult;
import ru.entaxy.platform.core.producer.executor.generationmodel.merge.MergeHelper;

public class MergeTask extends AbstractTask {

	private static final Logger log = LoggerFactory.getLogger(MergeTask.class);
	
	protected AbstractTask fromTask;
	protected AbstractTask toTask;
	
	public MergeTask(AbstractTask fromTask, AbstractTask toTask) {
		this.fromTask = fromTask;
		this.toTask = toTask;
		this.fromTask.mergeTo(this.toTask);
	}
	
	@Override
	public String getInfo() {
		return "MERGE: " + "\n\t FROM: " + fromTask.getInfo() + "\n\t TO: " + toTask.getInfo();
	}

	@Override
	public boolean execute(ProducerResult currentResult, CommandResult currentCommand,
			EntaxyProducerService producerService, Map<String, Object> instructions) throws Exception {
		
		Generated source = this.fromTask.getGeneratedResult();
		Generated target = this.toTask.getGeneratedResult();
		
		if ( (source!=null) && (target != null)) {
		
			if (!MergeHelper.getInstance().canMerge(source.getType(), target.getType())) {
				printOutput("CAN'T MERGE " + source.getType() + " TO " + target.getType());
				log.error("CAN'T MERGE " + source.getType() + " TO " + target.getType());
				throw new EntaxyFactoryException();
			}
			
			this.generatedResult = MergeHelper.getInstance().merge(source, target);
	
			// merge headers
			
			GeneratedHeaders sourceGeneratedHeaders = GeneratedHeaders.getHeaders(source.getProperties());
			GeneratedHeaders targetGeneratedHeaders = GeneratedHeaders.getHeaders(target.getProperties());
			
			GeneratedHeaders resultGeneratedHeaders = GeneratedHeaders.getHeaders(this.generatedResult.getProperties());
			
			for (String header: targetGeneratedHeaders.headers.keySet())
				resultGeneratedHeaders.set(header, targetGeneratedHeaders.headers.get(header));
			
			for (String header: sourceGeneratedHeaders.headers.keySet())
				if (EntaxyObject.HEADERS.MAIN_OBJECT.equals(header))
					continue;
				else
					resultGeneratedHeaders.append(header, sourceGeneratedHeaders.headers.get(header));
		} else {
			if (source != null)
				this.generatedResult = source;
			else if (target != null)
				this.generatedResult = target;
			else
				// nothing to do
				return true;
		}
		
		this.fromTask.setFinal(false);
		this.toTask.updateGenerated(generatedResult);
		
		// prevent building result of this task
		this.generatedResult = null;
		
		return true;
	}


	
}
