/*-
 * ~~~~~~licensing~~~~~~
 * test-producers
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/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);
	}
}
