/*-
 * ~~~~~~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.api;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

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

import ru.entaxy.platform.core.producer.api.ExecutionPlan.ExecutionPlanUpdate;

public class ProducerResult {
	
	private static final Logger log = LoggerFactory.getLogger(ProducerResult.class);
	
	public static class CommandResult {

		public String commandId;
		public boolean result = true;
		public boolean skipped = false;
		public List<Exception> exceptions = new ArrayList<>();
		public Object resultObject = null;
		public ExecutionPlanUpdate planUpdate = null;
		
		public CommandResult(String id) {
			this.commandId = id;
		}
		
		public ProducerResult.CommandResult result(boolean resultValue) {
			this.result = resultValue;
			return this;
		}; 
		
		public ProducerResult.CommandResult resultObject(Object resultObjectValue) {
			this.resultObject = resultObjectValue;
			return this;
		};
		
		public ProducerResult.CommandResult exception(Exception exceptionValue) {
			this.exceptions.add(exceptionValue);
			return this;
		};
	}

	public static ProducerResult createFailed(Exception e) {
		ProducerResult result = new ProducerResult();
		result.commandResult("#").exception(e).result(false);
		return result;
	} 	
	
	protected List<CommandResult> commandResults = new LinkedList<>();
	
	
	public Object getLastObject() {
		if (commandResults.isEmpty())
			return null;
		return commandResults.get(commandResults.size()-1).resultObject;
	}

	public CommandResult getLastCommandResult() {
		if (commandResults.isEmpty())
			return null;
		return commandResults.get(commandResults.size()-1);
	}
	
	public Map<String, List<Exception>> getAllExceptions(){
		Map<String, List<Exception>> result = new LinkedHashMap<>();
		for (CommandResult cr: commandResults)
			if (!cr.exceptions.isEmpty())
				result.put(cr.commandId, cr.exceptions);
		return result;
	}

	public List<CommandResult> getCommandResults() {
		return commandResults;
	}

	public <T> T findResultObject(Class<T> clazz){
		log.debug("findResultObject : " + clazz.getName());
		ListIterator<CommandResult> listIterator = commandResults.listIterator(commandResults.size());
		while (listIterator.hasPrevious()) {
			CommandResult cr = listIterator.previous();
			log.debug("Inspecting : " + cr.commandId);
			if ((cr.resultObject != null) && (clazz.isInstance(cr.resultObject))) {
				log.debug("... found");
				return clazz.cast(cr.resultObject);
			}
			log.debug("... not found");
		}
		return null;
	}
	
	public CommandResult commandResult(String id) {
		CommandResult commandResult = new CommandResult(id);
		this.commandResults.add(commandResult);
		return commandResult;
	}
	
	public void addCommandResult(CommandResult commandResult) {
		this.commandResults.add(commandResult);
	}
}
