/*-
 * ~~~~~~licensing~~~~~~
 * profile-management-service-adapters
 * ==========
 * Copyright (C) 2020 - 2021 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.esb.platform.core.management.profile.adapters;

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

import ru.entaxy.esb.system.jpa.SystemService;
import ru.entaxy.esb.system.jpa.entity.System;
import ru.entaxy.esb.system.management.profile.manager.ProfileManager;
import ru.entaxy.esb.system.management.profile.manager.dto.ProfileDto;
import ru.entaxy.esb.system.management.profile.manager.mapper.ProfileMapper;

@Component (
		service = {ru.entaxy.esb.platform.core.management.profile.api.ProfileManager.class}
		, immediate = true
		)
public class ProfileManagerAdapter implements ru.entaxy.esb.platform.core.management.profile.api.ProfileManager{
	
	@Reference (service = ProfileManager.class, cardinality = ReferenceCardinality.MANDATORY
			, collectionType = CollectionType.SERVICE)
	ProfileManager profileManager;

	@Reference (service = ProfileMapper.class, cardinality = ReferenceCardinality.MANDATORY
			, collectionType = CollectionType.SERVICE)
	ProfileMapper profileMapper;
	
	@Reference (service = SystemService.class, cardinality = ReferenceCardinality.MANDATORY
			, collectionType = CollectionType.SERVICE)
	SystemService systemService;
	
	@Override
	public void createProfile(String id, String name, String description) throws Exception {
		ProfileDto dto = new ProfileDto();
		dto.setUuid(id);
		dto.setName(name);
		dto.setDescription(description);
		
		// TODO get the user name from JMX, 
		// need to investigate, how
		dto.setCreatedBy("admin");
		
		profileManager.installProfile(dto);
	}

	@Override
	public void startProfile(String idOrName) throws Exception {
		ProfileDto dto = getProfileForSystem(getSystemByIdOrName(idOrName));
		if (dto == null)
			throw new IllegalArgumentException("System/profile not found for [" + idOrName + "]");
		profileManager.startProfile(dto);
	}

	@Override
	public void stopProfile(String idOrName) throws Exception {
		ProfileDto dto = getProfileForSystem(getSystemByIdOrName(idOrName));
		if (dto == null)
			throw new IllegalArgumentException("System/profile not found for [" + idOrName + "]");
		profileManager.stopProfile(dto);
	}

	@Override
	public void uninstallProfile(String idOrName) throws Exception {
		ProfileDto dto = getProfileForSystem(getSystemByIdOrName(idOrName));
		if (dto == null)
			throw new IllegalArgumentException("System/profile not found for [" + idOrName + "]");
		profileManager.uninstallProfile(dto);
	}

	protected System getSystemByIdOrName(String idOrName) {
		System result = systemService.getByName(idOrName);
		if (result == null)
			result = systemService.getByUuid(idOrName);
		return result;
	}
	
	protected ProfileDto getProfileForSystem(System system) {
		if (system == null)
			return null;
		return profileMapper.toProfileDto(system);
	}
}
