package com.bytedance.ies.sdk.widgets;

import X.2HN;
import X.M12;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Objects;
import kotlin.h.b.n;

public final class LiveWidgetProvider implements IWidgetProvider {
    public static final Companion Companion = new Companion((2HN) null);
    public static volatile LiveWidgetProvider sInstance;
    public final RecycledLiveWidgetPool mRecycledViewPool;

    public static final class RecycledLiveWidgetPool {
        public static final Companion Companion = new Companion((2HN) null);
        public static final int DEFAULT_MAX_SCRAP = 3;
        public final HashMap<Class<? super IRecyclableWidget>, ScrapData> mScrap = new HashMap<>();

        public static final class Companion {
            static {
                Covode.recordClassIndex(30529);
            }

            public /* synthetic */ Companion(2HN r1) {
                this();
            }

            public Companion() {
            }
        }

        public static final class ScrapData {
            public final int mMaxScrap = RecycledLiveWidgetPool.DEFAULT_MAX_SCRAP;
            public final ArrayList<IRecyclableWidget> mScrapHeap = new ArrayList<>(RecycledLiveWidgetPool.DEFAULT_MAX_SCRAP);

            static {
                Covode.recordClassIndex(30530);
            }

            public final int getMMaxScrap() {
                return this.mMaxScrap;
            }

            public final ArrayList<IRecyclableWidget> getMScrapHeap() {
                return this.mScrapHeap;
            }
        }

        static {
            Covode.recordClassIndex(30528);
        }

        public final void clear() {
            Collection<ScrapData> values = this.mScrap.values();
            M12.LIZ(values);
            for (ScrapData mScrapHeap : values) {
                mScrapHeap.getMScrapHeap().clear();
            }
        }

        public final void clearWidgetCache(Class<IRecyclableWidget> cls) {
            if (cls != null) {
                getScrapDataForType(cls).getMScrapHeap().clear();
            }
        }

        public final int getRecycledViewCount(Class<? super IRecyclableWidget> cls) {
            M12.LIZ(cls);
            return getScrapDataForType(cls).getMScrapHeap().size();
        }

        public final ScrapData getScrapDataForType(Class<? super IRecyclableWidget> cls) {
            M12.LIZ(cls);
            ScrapData scrapData = this.mScrap.get(cls);
            if (scrapData != null) {
                return scrapData;
            }
            ScrapData scrapData2 = new ScrapData();
            this.mScrap.put(cls, scrapData2);
            return scrapData2;
        }

        public final IRecyclableWidget getRecycledView(Class<? super IRecyclableWidget> cls) {
            M12.LIZ(cls);
            ScrapData scrapData = this.mScrap.get(cls);
            if (scrapData == null || !(!scrapData.getMScrapHeap().isEmpty())) {
                return null;
            }
            ArrayList<IRecyclableWidget> mScrapHeap = scrapData.getMScrapHeap();
            return mScrapHeap.remove(mScrapHeap.size() - 1);
        }

        public final void putRecycledView(IRecyclableWidget iRecyclableWidget) {
            M12.LIZ(iRecyclableWidget);
            Class<?> cls = iRecyclableWidget.getClass();
            ArrayList<IRecyclableWidget> mScrapHeap = getScrapDataForType(cls).getMScrapHeap();
            ScrapData scrapData = this.mScrap.get(cls);
            if (scrapData == null) {
                n.LIZIZ();
            }
            if (scrapData.getMMaxScrap() > mScrapHeap.size()) {
                iRecyclableWidget.resetInternal();
                mScrapHeap.add(iRecyclableWidget);
            }
        }
    }

    static {
        Covode.recordClassIndex(30526);
    }

    public /* synthetic */ LiveWidgetProvider(2HN r1) {
        this();
    }

    public static final LiveWidgetProvider getInstance() {
        return Companion.getInstance();
    }

    public static final class Companion {
        static {
            Covode.recordClassIndex(30527);
        }

        public /* synthetic */ Companion(2HN r1) {
            this();
        }

        public static /* synthetic */ void getInstance$annotations() {
        }

        public Companion() {
        }

        public final LiveWidgetProvider getInstance() {
            MethodCollector.i(8135);
            if (LiveWidgetProvider.sInstance == null) {
                synchronized (LiveWidgetProvider.class) {
                    try {
                        if (LiveWidgetProvider.sInstance == null) {
                            LiveWidgetProvider.sInstance = new LiveWidgetProvider((2HN) null);
                        }
                    } catch (Throwable th) {
                        MethodCollector.o(8135);
                        throw th;
                    }
                }
            }
            LiveWidgetProvider liveWidgetProvider = LiveWidgetProvider.sInstance;
            if (liveWidgetProvider == null) {
                n.LIZIZ();
            }
            MethodCollector.o(8135);
            return liveWidgetProvider;
        }
    }

    public final void clear() {
        this.mRecycledViewPool.clear();
    }

    public LiveWidgetProvider() {
        this.mRecycledViewPool = new RecycledLiveWidgetPool();
    }

    public final void clearWidgetCache(Class<? extends LiveRecyclableWidget> cls) {
        RecycledLiveWidgetPool recycledLiveWidgetPool = this.mRecycledViewPool;
        if (cls == null) {
            cls = null;
        }
        recycledLiveWidgetPool.clearWidgetCache(cls);
    }

    public final void recycleRecyclableWidget(IRecyclableWidget iRecyclableWidget) {
        M12.LIZ(iRecyclableWidget);
        this.mRecycledViewPool.putRecycledView(iRecyclableWidget);
    }

    public final <T extends IRecyclableWidget> T provide(Class<T> cls) {
        RecycledLiveWidgetPool recycledLiveWidgetPool = this.mRecycledViewPool;
        Objects.requireNonNull(cls, "null cannot be cast to non-null type java.lang.Class<com.bytedance.ies.sdk.widgets.IRecyclableWidget>");
        T recycledView = recycledLiveWidgetPool.getRecycledView(cls);
        if (recycledView != null) {
            return recycledView;
        }
        try {
            T t = (IRecyclableWidget) cls.newInstance();
            if (t == null) {
                n.LIZIZ();
            }
            if (t instanceof IRecyclableWidget) {
                return t;
            }
            return null;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e2) {
            throw new RuntimeException(e2);
        }
    }
}
