package com.ss.android.ugc.aweme.framework.services;

import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.framework.services.plugin.PluginInstaller;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public final class ServiceManager {
    public static boolean isOptNonExistCollection;
    public final ConcurrentHashMap<Class<?>, Set<ServiceProvider<?>>> classServiceProviderConcurrentHashMap;
    public volatile boolean enableDynamicProxyDefault;
    public final Map<Class<?>, Map<String, ServiceProvider<?>>> groupServiceProviderConcurrentHashMap;
    public final Set<String> legacyServiceSet;
    public final Set<Class<?>> resolvedClasses;

    /* renamed from: com.ss.android.ugc.aweme.framework.services.ServiceManager$1  reason: invalid class name */
    public static /* synthetic */ class AnonymousClass1 {
        static {
            Covode.recordClassIndex(81086);
        }
    }

    static {
        Covode.recordClassIndex(81085);
    }

    public static ServiceManager get() {
        return Holder.INSTANCE;
    }

    public final Map<Class<?>, Map<String, ServiceProvider<?>>> getGroupServiceProviderMap() {
        return this.groupServiceProviderConcurrentHashMap;
    }

    public final ConcurrentHashMap getServiceProviderMap() {
        return this.classServiceProviderConcurrentHashMap;
    }

    public static final class Holder {
        public static final ServiceManager INSTANCE = new ServiceManager((AnonymousClass1) null);

        static {
            Covode.recordClassIndex(81087);
        }
    }

    public ServiceManager() {
        this.classServiceProviderConcurrentHashMap = new ConcurrentHashMap<>();
        this.groupServiceProviderConcurrentHashMap = Collections.synchronizedMap(new HashMap());
        this.legacyServiceSet = Collections.synchronizedSet(new HashSet());
        this.enableDynamicProxyDefault = true;
        this.resolvedClasses = Collections.synchronizedSet(new HashSet());
    }

    public /* synthetic */ ServiceManager(AnonymousClass1 r1) {
        this();
    }

    public final void setDynamicProxyEnableDefault(boolean z) {
        this.enableDynamicProxyDefault = z;
    }

    private void markResolved(Class cls) {
        this.resolvedClasses.add(cls);
    }

    public static void setIsOptNonExistCollection(Boolean bool) {
        isOptNonExistCollection = bool.booleanValue();
    }

    public final void addLegacyService(String str) {
        this.legacyServiceSet.add(str);
    }

    public final boolean downgradeComponent(String str) {
        return downgradeComponent(str, false);
    }

    public final <T> T getService(Class<T> cls) {
        return getService(cls, false);
    }

    public final <T> Set<T> getServices(Class<T> cls) {
        return getServices(cls, false);
    }

    public final boolean onPluginInstall(String str) {
        return PluginInstaller.INSTANCE.onPluginInstall(str);
    }

    public final void addLegacyService(Set<String> set) {
        this.legacyServiceSet.addAll(set);
    }

    public final <T> T getServiceForReal(Class<T> cls) {
        return getService(cls, false, this.enableDynamicProxyDefault, true);
    }

    private <T> T getLegacyService(Class<T> cls) {
        if (!this.legacyServiceSet.contains(cls.getName())) {
            return null;
        }
        Iterator<T> it = ConfigLoader.iterator(cls);
        if (it.hasNext()) {
            return it.next();
        }
        return null;
    }

    public final <T> Set<T> getLegacyServices(Class<T> cls) {
        Set<T> synchronizedSet = Collections.synchronizedSet(new LinkedHashSet());
        if (!this.legacyServiceSet.contains(cls.getName())) {
            return synchronizedSet;
        }
        Iterator<T> it = ConfigLoader.iterator(cls);
        while (it.hasNext()) {
            synchronizedSet.add(it.next());
        }
        return synchronizedSet;
    }

    public final <T> Binding bind(Class<T> cls, ServiceProvider<T> serviceProvider) {
        return new Binding(this, cls, serviceProvider);
    }

    public final <T> void bindDowngradeImpl(Class<T> cls, ServiceProvider<T> serviceProvider) {
        bindDowngradeImpl(cls, serviceProvider, true);
    }

    public final <T> T getService(Class<T> cls, boolean z) {
        return getService(cls, z, this.enableDynamicProxyDefault);
    }

    public final <T> T getServiceByGroup(Class<T> cls, String str) {
        Map map = this.groupServiceProviderConcurrentHashMap.get(cls);
        if (map == null || !map.containsKey(str)) {
            return null;
        }
        return ((ServiceProvider) map.get(str)).get();
    }

    public final boolean downgradeComponent(String str, boolean z) {
        if (!z) {
            Map map = DowngradeImplManager.getInstance().componentsMap;
            for (Object next : map.keySet()) {
                if (this.resolvedClasses.contains(next) && TextUtils.equals((CharSequence) map.get(next), str)) {
                    return false;
                }
            }
        }
        DowngradeImplManager.getInstance().downgradeComponent(str);
        return true;
    }

    public final <T> Set<T> getServices(Class<T> cls, boolean z) {
        Set staticServiceImplSet;
        Set<ServiceProvider> set = this.classServiceProviderConcurrentHashMap.get(cls);
        if (set == null || set.isEmpty()) {
            Set<T> staticServiceImplSet2 = StaticServiceImplManager.getInstance().getStaticServiceImplSet(cls);
            if (staticServiceImplSet2 == null || staticServiceImplSet2.isEmpty()) {
                return getLegacyServices(cls);
            }
            return staticServiceImplSet2;
        }
        Set<T> synchronizedSet = Collections.synchronizedSet(new LinkedHashSet());
        for (ServiceProvider serviceProvider : set) {
            synchronizedSet.add(serviceProvider.get());
        }
        if (z && (staticServiceImplSet = StaticServiceImplManager.getInstance().getStaticServiceImplSet(cls)) != null && !staticServiceImplSet.isEmpty()) {
            synchronizedSet.addAll(staticServiceImplSet);
        }
        return synchronizedSet;
    }

    public final <T> GroupBinding bind(Class<T> cls, String str, ServiceProvider<T> serviceProvider) {
        return new GroupBinding(this.groupServiceProviderConcurrentHashMap, cls, str, serviceProvider);
    }

    public final <T> void bindDowngradeImpl(Class<T> cls, ServiceProvider<T> serviceProvider, boolean z) {
        if (z) {
            serviceProvider = new SingletonProvider<>(serviceProvider);
        }
        DowngradeImplManager.getInstance().bindDowngradeImpl(cls, serviceProvider);
    }

    public final <T> T getService(Class<T> cls, boolean z, boolean z2) {
        return getService(cls, z, z2, false);
    }

    public final <T> T getService(Class<T> cls, boolean z, boolean z2, boolean z3) {
        boolean z4;
        T t;
        if (z3 || !DowngradeImplManager.getInstance().isServiceForceDowngrade(cls)) {
            z4 = false;
        } else {
            T downgradeImpl = DowngradeImplManager.getInstance().getDowngradeImpl(cls);
            if (downgradeImpl != null) {
                return downgradeImpl;
            }
            z4 = true;
        }
        Set set = this.classServiceProviderConcurrentHashMap.get(cls);
        if (set == null || set.isEmpty() || z || (t = ((ServiceProvider) set.toArray()[0]).get()) == null) {
            T staticServiceImpl = StaticServiceImplManager.getInstance().getStaticServiceImpl(cls);
            if (staticServiceImpl != null) {
                markResolved(cls);
                return staticServiceImpl;
            } else if (z3) {
                return null;
            } else {
                T legacyService = getLegacyService(cls);
                if (legacyService != null) {
                    markResolved(cls);
                    return legacyService;
                } else if (!z4 && (legacyService = DowngradeImplManager.getInstance().getDowngradeImpl(cls)) != null) {
                    return legacyService;
                } else {
                    if (!z2) {
                        return legacyService;
                    }
                    new DynamicProxy();
                    return DynamicProxy.get(cls);
                }
            }
        } else {
            markResolved(cls);
            return t;
        }
    }
}
