/*-
 * ~~~~~~licensing~~~~~~
 * object-runtime-core
 * ==========
 * 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.impl.manager;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObject;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObject.ENTAXY_RUNTIME_OBJECT_STATE;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObjectManager;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObjectService;

@Component(service = EntaxyRuntimeObjectManager.class, immediate = true)
public class EntaxyRuntimeObjectManagerImpl implements EntaxyRuntimeObjectManager {

	@Reference(cardinality = ReferenceCardinality.MANDATORY, policyOption = ReferencePolicyOption.GREEDY)
	EntaxyRuntimeObjectService objectService;
	
	@Override
	public OperationResult execute(ENTAXY_RUNTIME_OBJECT_OPERATION operation, OperationConfig config) {
		return createExecutor(operation).execute(config);
	}
	
	@Override
	public OperationResult start(String objectFullId) {
		return start(new OperationConfigImpl(objectFullId));
	}

	@Override
	public OperationResult startForced(String objectFullId) {
		return startForced(objectFullId, false);
	}
	
    @Override
    public OperationResult startForced(String objectFullId, boolean isAffectedBundle) {
        return start((new OperationConfigImpl(objectFullId)).forced(true).affectedOperation(isAffectedBundle));
    }

	@Override
	public OperationResult startSoft(String objectFullId) {
		return start((new OperationConfigImpl(objectFullId)).soft(true));
	}

	@Override
	public OperationResult start(OperationConfig config) {
		EntaxyRuntimeObject object = objectService.getRuntimeObject(config.getObjectFullId());
		if (object == null)
			return OperationResultImpl.createFailed();
		if (ENTAXY_RUNTIME_OBJECT_STATE.ACTIVE.equals(objectService.getObjectState(object)))
			return OperationResultImpl.createDone();
		return createExecutor(ENTAXY_RUNTIME_OBJECT_OPERATION.START).execute(config);
	}

	@Override
	public OperationResult stop(String objectFullId) {
		return stop(new OperationConfigImpl(objectFullId));
	}

	@Override
	public OperationResult stopForced(String objectFullId) {
		return stop((new OperationConfigImpl(objectFullId)).forced(true));
	}

	@Override
	public OperationResult stopSoft(String objectFullId) {
		return stop((new OperationConfigImpl(objectFullId)).soft(true));
	}

	@Override
	public OperationResult stop(OperationConfig config) {
		EntaxyRuntimeObject object = objectService.getRuntimeObject(config.getObjectFullId());
		if (object == null)
			return OperationResultImpl.createFailed();
		if (ENTAXY_RUNTIME_OBJECT_STATE.INACTIVE.equals(objectService.getObjectState(object)))
			return OperationResultImpl.createDone();
		return createExecutor(ENTAXY_RUNTIME_OBJECT_OPERATION.STOP).execute(config);
	}

	@Override
	public OperationResult uninstall(String objectFullId) {
		return uninstall(new OperationConfigImpl(objectFullId));
	}

	@Override
	public OperationResult uninstallForced(String objectFullId) {
		return uninstall((new OperationConfigImpl(objectFullId)).forced(true));
	}

	@Override
	public OperationResult uninstallSoft(String objectFullId) {
		return uninstall((new OperationConfigImpl(objectFullId)).soft(true));
	}

	@Override
	public OperationResult uninstall(OperationConfig config) {
		return createExecutor(ENTAXY_RUNTIME_OBJECT_OPERATION.UNINSTALL).execute(config);
	}

	public EntaxyRuntimeObjectService getObjectService() {
		return objectService;
	}
	
	protected OperationExecutor<?> createExecutor(ENTAXY_RUNTIME_OBJECT_OPERATION operation) {
		switch (operation) {
		case START:
			return (new OperationExecutorStart()).objectManager(this);
		case STOP:
			return (new OperationExecutorStop()).objectManager(this);
		case UNINSTALL:
			return (new OperationExecutorUninstall()).objectManager(this);
		default:
			return (new OperationExecutorStart()).objectManager(this);
		}
	}
}
