/*-
 * ~~~~~~licensing~~~~~~
 * profile-management-service-adapters
 * ==========
 * 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.esb.platform.core.management.profile.adapters;

import javax.naming.OperationNotSupportedException;

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.platform.core.management.profile.support.legacy.ProfileManager.class}, immediate = true)
public class ProfileManagerAdapter implements ru.entaxy.platform.core.management.profile.support.legacy.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);
    }

    @Override
    public String getProfileConfig(String idOrName) throws Exception {
        throw new OperationNotSupportedException();
    }

    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);
    }
}
