/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management
 * ==========
 * 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.platform.core.artifact.installer.builder.typed;

import java.util.HashMap;
import java.util.Map;

import ru.entaxy.platform.core.artifact.installer.builder.ClusterInstaller;
import ru.entaxy.platform.core.artifact.installer.builder.Installer;
import ru.entaxy.platform.core.artifact.installer.builder.LocalInstaller;

public class TypedInstallerManager {


    protected static Map<Class<? extends Installer<?>>, TypedInstallerStorage> storages = new HashMap<>();

    protected static TypedInstallerStorage commonStorage = new TypedInstallerStorage();

    static {
        storages.put(LocalInstaller.class, new TypedInstallerStorage());
        storages.put(ClusterInstaller.class, new TypedInstallerStorage());
    }

    protected static Object storagesLock = new Object();

    public static void register(String artifactType,
            TypedInstallerFactory factory) {

        register(commonStorage, artifactType, factory);
    }

    public static void register(TypedInstallerFactory factory) {

        register(commonStorage, factory);
    }

    protected static TypedInstallerStorage getOrCreateStorage(Class<? extends Installer<?>> installerClass) {
        if (!storages.containsKey(installerClass)) {
            synchronized (storagesLock) {
                storages.put(installerClass, new TypedInstallerStorage());
            }
        }
        return storages.get(installerClass);
    }

    public static void register(Class<? extends Installer<?>> installerClass, String artifactType,
            TypedInstallerFactory factory) {
        register(getOrCreateStorage(installerClass), artifactType, factory);
    }

    public static void register(Class<? extends Installer<?>> installerClass,
            TypedInstallerFactory factory) {
        register(getOrCreateStorage(installerClass), factory);
    }

    protected static void register(TypedInstallerStorage storage, TypedInstallerFactory factory) {
        for (String type : factory.getSupportedTypes())
            register(storage, type, factory);
    }

    protected static void register(TypedInstallerStorage storage, String artifactType,
            TypedInstallerFactory factory) {
        storage.register(artifactType, factory);
    }

    public static TypedInstaller create(Object owner, String type) {
        return create(owner, type, false);
    }

    public static TypedInstaller create(Object owner, String type, boolean strict) {
        for (Class<? extends Installer<?>> installerClass : storages.keySet())
            if (installerClass.isInstance(owner))
                return create(installerClass, type, strict);
        if (strict)
            return null;
        return create(null, type, strict);
    }

    public static TypedInstaller create(Class<? extends Installer<?>> installerClass, String type) {
        return create(installerClass, type, false);
    }

    public static TypedInstaller create(Class<? extends Installer<?>> installerClass, String type, boolean strict) {
        TypedInstallerStorage storage = installerClass == null ? null : storages.get(installerClass);
        TypedInstaller result = null;
        if (storage != null) {
            result = storage.create(type);
        }
        if (result != null)
            return result;
        if (strict)
            return null;
        else
            return commonStorage.create(type);

    }

    public static TypedInstaller create(Object owner, Class<? extends TypedInstaller> typedClass) {
        return create(owner, typedClass, false);
    }

    public static TypedInstaller create(Object owner, Class<? extends TypedInstaller> typedClass, boolean strict) {
        for (Class<? extends Installer<?>> installerClass : storages.keySet())
            if (installerClass.isInstance(owner))
                return create(installerClass, typedClass, strict);
        if (strict)
            return null;
        return create(null, typedClass, strict);
    }

    public static TypedInstaller create(Class<? extends Installer<?>> installerClass,
            Class<? extends TypedInstaller> typedClass) {
        return create(installerClass, typedClass, false);
    }

    public static TypedInstaller create(Class<? extends Installer<?>> installerClass,
            Class<? extends TypedInstaller> typedClass, boolean strict) {
        TypedInstallerStorage storage = installerClass == null ? null : storages.get(installerClass);
        TypedInstaller result = null;
        if (storage != null) {
            result = storage.create(typedClass);
        }
        if (result != null)
            return result;
        if (strict)
            return null;
        else
            return commonStorage.create(typedClass);

    }


    protected static class TypedInstallerStorage {

        protected Map<String, TypedInstallerFactory> factories = new HashMap<>();

        protected Object factoriesLock = new Object();

        public void register(String type, TypedInstallerFactory factory) {
            synchronized (factoriesLock) {
                factories.put(type, factory);
            }
        }

        public TypedInstaller create(String type) {
            if (!factories.containsKey(type))
                return null;
            return factories.get(type).create(type);
        }

        public TypedInstaller create(Class<? extends TypedInstaller> typedClass) {

            TypedInstallerFactory factory = null;

            for (TypedInstallerFactory f : factories.values())
                if (f.getSupportedClasses().contains(typedClass)) {
                    factory = f;
                    break;
                }

            if (factory == null)
                return null;

            return factory.create(typedClass);
        }

    }

}
