/*-
 * ~~~~~~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 java.util.ArrayList;
import java.util.List;

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.core.permission.jpa.PermissionService;
import ru.entaxy.esb.system.core.permission.jpa.entity.Permission;
import ru.entaxy.esb.system.jpa.SystemService;
import ru.entaxy.esb.system.management.permission.manager.dto.PermissionDto;
import ru.entaxy.platform.core.management.profile.support.legacy.PermissionManager;

@Component(service = PermissionManager.class,
        immediate = true)
public class PermissionManagerAdapter implements PermissionManager {

    @Reference(service = ru.entaxy.esb.system.management.permission.manager.PermissionManager.class,
            cardinality = ReferenceCardinality.MANDATORY,
            collectionType = CollectionType.SERVICE)
    ru.entaxy.esb.system.management.permission.manager.PermissionManager permissionManager;

    @Reference(service = PermissionService.class, cardinality = ReferenceCardinality.MANDATORY,
            collectionType = CollectionType.SERVICE)
    PermissionService permissionService;

    @Reference(service = SystemService.class, cardinality = ReferenceCardinality.MANDATORY,
            collectionType = CollectionType.SERVICE)
    SystemService systemService;

    @Override
    public List<String> getObjectPermissions(String objectId) throws Exception {
        ru.entaxy.esb.system.jpa.entity.System system = getRequiredSystemByIdOrName(objectId);
        List<Permission> permissions = permissionService.getAllById(system.getId(), "system");
        long systemId = system.getId();
        List<String> result = new ArrayList<>();
        for (Permission perm : permissions) {
            if (perm.getObjectId() == systemId) {
                if ("system".equals(perm.getSubjectType())) {
                    ru.entaxy.esb.system.jpa.entity.System s = systemService.get(Long.parseLong(perm.getSubjectId()));
                    if (s != null)
                        result.add(s.getName());
                }
            }
        }
        return result;
    }

    @Override
    public List<String> getSubjectPermissions(String subjectId) throws Exception {
        ru.entaxy.esb.system.jpa.entity.System system = getRequiredSystemByIdOrName(subjectId);
        List<Permission> permissions = permissionService.getAllById(system.getId(), "system");
        String systemId = system.getId() + "";
        List<String> result = new ArrayList<>();
        for (Permission perm : permissions) {
            if (perm.getSubjectId().equals(systemId)) {
                ru.entaxy.esb.system.jpa.entity.System s = systemService.get(perm.getObjectId());
                if (s != null)
                    result.add(s.getName());
            }
        }
        return result;
    }

    @Override
    public void createPermission(String objectId, String subjectId) throws Exception {
        PermissionDto dto = new PermissionDto(objectId, subjectId);
        permissionManager.createPermission(dto);
    }

    protected ru.entaxy.esb.system.jpa.entity.System getSystemByIdOrName(String idOrName) {
        ru.entaxy.esb.system.jpa.entity.System result = systemService.getByName(idOrName);
        if (result == null)
            result = systemService.getByUuid(idOrName);
        return result;
    }

    protected ru.entaxy.esb.system.jpa.entity.System getRequiredSystemByIdOrName(String idOrName) throws Exception {
        ru.entaxy.esb.system.jpa.entity.System result = systemService.getByName(idOrName);
        if (result == null)
            result = systemService.getByUuid(idOrName);
        if (result == null)
            throw new IllegalArgumentException("System not found by Id or Name: [" + idOrName + "]");
        return result;
    }
}
