/*-
 * ~~~~~~licensing~~~~~~
 * secure-vault-runtime
 * ==========
 * 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.security.vault.runtime.lookup;

import java.util.Collection;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.security.vault.runtime.EntaxyVault;

@Component(service = SecretsLookupProvider.class, immediate = true)
public class SecretsLookupProvider {

    public static final String PREFIX_VALUE = "ENTAXY_SECRETS";

    public static final String PREFIX = "$" + PREFIX_VALUE + "{";

    private static SecretsLookupProvider INSTANCE = null;

    public static String lookupSecret(String spec) {
        if (INSTANCE == null)
            return null;
        return INSTANCE.lookup(spec);
    }

    public static String lookupSecret(String alias, String vault) {
        if (INSTANCE == null)
            return null;
        return INSTANCE.lookup(alias, vault);
    }

    protected BundleContext bundleContext;

    @Activate
    public void activate(ComponentContext componentContext) {
        this.bundleContext = componentContext.getBundleContext();
        INSTANCE = this;
    }

    public String lookup(String spec) {

        if (!CommonUtils.isValid(spec))
            return null;

        int index = spec.lastIndexOf('@');
        if (index <= 0)
            return null;

        String alias = spec.substring(0, index);
        String vault = spec.substring(index + 1);

        return SecretsLookupProvider.lookupSecret(alias, vault);

    }

    public String lookup(String alias, String vault) {
        try {

            Collection<ServiceReference<EntaxyVault>> refs =
                    bundleContext.getServiceReferences(EntaxyVault.class, "(name=" + vault + ")");
            if (refs == null || refs.isEmpty())
                return null;
            ServiceReference<EntaxyVault> ref = refs.iterator().next();
            EntaxyVault entaxyVault = bundleContext.getService(ref);

            String value = entaxyVault.getData(alias);

            bundleContext.ungetService(ref);


            return value;
        } catch (Exception e) {
            return null;
        }
    }
}
