/*-
 * ~~~~~~licensing~~~~~~
 * object-runtime-shell
 * ==========
 * 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.objects.runtime.shell;

import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.support.table.ShellTable;

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObject;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObjectManager;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObjectManager.OperationResult;

public abstract class ObjectOperationCommand extends RuntimeObjectAwareCommand {

	@Option(name = "-f", required = false, description = "Force action on affected objects")
	boolean force;
	
	@Option(name = "-s", required = false, description = "Skip action on affected objects")
	boolean soft;
	
	@Reference
	EntaxyRuntimeObjectManager objectManager;

	@Override
	public Object doExecute(EntaxyRuntimeObject entaxyObject) throws Exception {
		OperationResult result = doTargetExecute(entaxyObject);
		
		// output result
		System.out.println("\t -> " + result.getResult().name());

		switch (result.getResult()) {
		case DONE:
			
			break;
		case REQUEST:
			outputForRequest(result, entaxyObject);
			break;
		case BLOCKED:
			outputForBlocked(result, entaxyObject);
			break;
		case FAILED:
			outputForFailed(result, entaxyObject);
			break;
		default:
			break;
		}
		return null;
	}
	
	protected abstract OperationResult doTargetExecute(EntaxyRuntimeObject entaxyObject) throws Exception;
	
	protected void outputForRequest(OperationResult result, EntaxyRuntimeObject entaxyObject) {
		ShellTable table = new ShellTable();
		table.column("Object");
		table.column("Action");
		for (String key: result.getAffected().keySet())
			table.addRow().addContent(key, result.getAffected().get(key).name());
		
		// output
		System.out.println("\nOperation should be applied to the following affected objects:");
		table.print(System.out);
		
		String message = "\n"
				.concat("Use\n")
				.concat("\t-f option to apply all operations on affected objects\n")
				.concat("or\n")
				.concat("\t-s option to skip all operations on affected objects\n");
		
		// output
		System.out.println(message);
	}

	protected void outputForBlocked(OperationResult result, EntaxyRuntimeObject entaxyObject) {
		ShellTable table = new ShellTable();
		table.column("Object");
		table.column("Action");
		for (String key: result.getAffected().keySet())
			table.addRow().addContent(key, result.getAffected().get(key).name());
		
		// output
		System.out.println("\nThe following operations must be applied before invoking the current one:");
		table.print(System.out);
		
	}

	protected void outputForFailed(OperationResult result, EntaxyRuntimeObject entaxyObject) {
		
		// Output info
		System.out.println(
				"Operation failed with "
				.concat(CommonUtils.isValid(result.getMessage())?"the following ":"no ")
				.concat("message")
				.concat(
						CommonUtils.isValid(result.getMessage())
						?(": ".concat(result.getMessage()))
						:"."
					)
			);
		
		if (!result.getAffected().isEmpty()) {
			ShellTable table = new ShellTable();
			table.column("Object");
			table.column("Action");
			for (String key: result.getAffected().keySet())
				table.addRow().addContent(key, result.getAffected().get(key).name());
			
			// output
			System.out.println("\nThe following suboperations failed:");
			table.print(System.out);
		}
	}
	
}
