/*-
 * ~~~~~~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.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.core.producer.api.ExecutionPlan.ExecutionPlanUpdate.OPERATIONS;

public class ExecutionPlan implements Iterable<ExecutionTask> {

	public List<ExecutionTask> tasks = new ArrayList<>();
	
	public ExecutionPlan(List<ExecutionTask> tasks) {
		this.tasks = tasks;
	}

	protected Iterator<ExecutionTask> currentIterator;
	
	protected ExecutionPlanIterator iterator = null;
	
	@Override
	public Iterator<ExecutionTask> iterator() {
		this.currentIterator = this.tasks.listIterator();
		if (this.iterator == null)
			this.iterator = new ExecutionPlanIterator();
		return this.iterator;
	}

	public List<ExecutionPlanUpdate.OPERATIONS> applyUpdate(ExecutionPlanUpdate planUpdate) throws Exception {
		List<ExecutionPlanUpdate.OPERATIONS> result = new LinkedList<>();
		
		for (ExecutionPlanUpdate.ExecutionPlanUpdateRecord record: planUpdate.records) {
			if (ExecutionPlanUpdate.OPERATIONS.RESET.equals(record.operation)) {
				String target = record.target;
				reset(target);
				result.add(OPERATIONS.RESET);
				continue;
			}
			if (ExecutionPlanUpdate.OPERATIONS.UPDATE_INSTRUCTIONS.equals(record.operation)) {
				String target = record.target;
				synchronized (tasks) {
					for (ExecutionTask task: tasks)
						if (task.commandId.equals(target)) {
							if ((task.instructions == null) || task.instructions.isEmpty())
								task.instructions = new HashMap<>();
							task.instructions.putAll(record.values);
							result.add(OPERATIONS.UPDATE_INSTRUCTIONS);
							continue;
						}
				}
			}
		}
		
		return result;
	}
	
	protected void reset(String commandId) throws Exception {
		synchronized (tasks) {
			if (CommonUtils.isValid(commandId)) {
				for (int i=0; i<tasks.size(); i++)
					if (tasks.get(i).commandId.equals(commandId)) {
						this.currentIterator = tasks.listIterator(i);
						return;
					}
			} else {
				this.currentIterator = tasks.listIterator();
			}
		}
	}
	
	protected Iterator<ExecutionTask> getCurrentIterator(){
		return this.currentIterator;
	}
	
	protected class ExecutionPlanIterator implements Iterator<ExecutionTask>{

		@Override
		public boolean hasNext() {
			return ExecutionPlan.this.getCurrentIterator().hasNext();
		}

		@Override
		public ExecutionTask next() {
			return ExecutionPlan.this.getCurrentIterator().next();
		}
		
	}
	
	public static class ExecutionPlanUpdate {
		
		public static enum OPERATIONS {
			RESET,
			UPDATE_INSTRUCTIONS
		}
		
		protected List<ExecutionPlanUpdateRecord> records = new LinkedList<>();
		
		public static class ExecutionPlanUpdateRecord {
			
			protected ExecutionPlanUpdate planUpdate;
			
			protected OPERATIONS operation;
			
			protected String target;
			
			protected Map<String, Object> values = new LinkedHashMap<>();
			
			protected ExecutionPlanUpdateRecord(ExecutionPlanUpdate planUpdate) {
				this.planUpdate = planUpdate;
			}
			
			public ExecutionPlanUpdateRecord target(String value) {
				this.target = value;
				return this;
			}
			
			public ExecutionPlanUpdateRecord value(String name, Object value) {
				this.values.put(name, value);
				return this;
			}
			
			public ExecutionPlanUpdate complete() {
				return this.planUpdate;
			}
		}
		
		public static ExecutionPlanUpdate create() {
			return new ExecutionPlanUpdate();
		}
		
		public ExecutionPlanUpdateRecord reset() {
			ExecutionPlanUpdateRecord record = new ExecutionPlanUpdateRecord(this);
			record.operation = OPERATIONS.RESET;
			this.records.add(record);
			return record;
		}

		public ExecutionPlanUpdateRecord updateInstructions() {
			ExecutionPlanUpdateRecord record = new ExecutionPlanUpdateRecord(this);
			record.operation = OPERATIONS.UPDATE_INSTRUCTIONS;
			this.records.add(record);
			return record;
		}

		
	}
	
}
