/*-
 * ~~~~~~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.Map;

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.connector.entity.Connector;
import ru.entaxy.esb.system.connector.impl.ConnectorService;
import ru.entaxy.esb.system.jpa.SystemService;
import ru.entaxy.esb.system.jpa.entity.System;
import ru.entaxy.esb.system.management.bundle.jpa.dto.BundleDto;
import ru.entaxy.esb.system.management.bundle.jpa.dto.BundleStatus;
import ru.entaxy.esb.system.management.connector.manager.ConnectorManager;
import ru.entaxy.esb.system.management.connector.manager.dto.ConnectorDto;

@Component(service = ru.entaxy.platform.core.management.profile.support.legacy.ConnectorManager.class, immediate = true)
public class ConnectorManagerAdapter
        implements ru.entaxy.platform.core.management.profile.support.legacy.ConnectorManager {

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

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

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

    @Override
    public void addConnector(String idOrName, String connectorFactoryId, Map<String, String> parameters)
            throws Exception {

        System system = getSystemByIdOrName(idOrName);
        if (system == null)
            throw new IllegalArgumentException("System not found by Id or Name [" + idOrName + "]");
        ConnectorDto connectorDto = new ConnectorDto();
        connectorDto.setConnectorParams(parameters);
        connectorDto.setSystemUuid(system.getUuid());
        connectorDto.setTemplateName(connectorFactoryId);

        // FIXME remove hardcoded
        connectorDto.setCreatedBy("admin");

        checkBundleResult(connectorManager.installConnector(connectorDto), BundleStatus.ACTIVE);

    }

    @Override
    public void removeConnector(String idOrName, String connectorName) throws Exception {

        System system = getSystemByIdOrName(idOrName);
        if (system == null)
            throw new IllegalArgumentException("System not found by Id or Name [" + idOrName + "]");

        Connector connector = connectorService.getConnectorByName(connectorName);
        if (connector == null)
            throw new IllegalArgumentException("Connector not found by Name [" + connectorName + "]");

        checkBundleResult(connectorManager.uninstallConnector(connector), BundleStatus.UNINSTALL);
    }

    @Override
    public void startConnector(String idOrName, String connectorName) throws Exception {

        System system = getSystemByIdOrName(idOrName);
        if (system == null)
            throw new IllegalArgumentException("System not found by Id or Name [" + idOrName + "]");

        Connector connector = connectorService.getConnectorByName(connectorName);
        if (connector == null)
            throw new IllegalArgumentException("Connector not found by Name [" + connectorName + "]");

        checkBundleResult(connectorManager.startConnector(connector), BundleStatus.ACTIVE);

    }

    @Override
    public void stopConnector(String idOrName, String connectorName) throws Exception {

        System system = getSystemByIdOrName(idOrName);
        if (system == null)
            throw new IllegalArgumentException("System not found by Id or Name [" + idOrName + "]");

        Connector connector = connectorService.getConnectorByName(connectorName);
        if (connector == null)
            throw new IllegalArgumentException("Connector not found by Name [" + connectorName + "]");

        checkBundleResult(connectorManager.stopConnector(connector), BundleStatus.RESOLVED);

    }

    protected System getSystemByIdOrName(String idOrName) {
        System result = systemService.getByName(idOrName);
        if (result == null)
            result = systemService.getByUuid(idOrName);
        return result;
    }

    protected void checkBundleResult(BundleDto bundleDto, BundleStatus expectedStatus) throws Exception {
        if ((bundleDto == null) || !expectedStatus.equals(bundleDto.getStatus()))
            throw new Exception("Result bundle is null or in a wrong state");

    }
}
