package com.bytedance.android.widget;

import X.0CB;
import X.0CC;
import X.0CD;
import X.0CH;
import X.0CN;
import X.0HH;
import X.1Bk;
import X.1hK;
import X.6Is;
import X.C2381GbG;
import X.C2882GjL;
import X.C2883GjM;
import X.C2884GjN;
import X.C2885GjO;
import X.C2886GjP;
import X.C2887GjQ;
import X.C2888GjR;
import X.C2889GjS;
import X.HYK;
import X.HYL;
import X.HYN;
import android.content.Context;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import androidx.fragment.app.Fragment;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.sdk.widgets.FluencyOpt;
import com.bytedance.ies.sdk.widgets.LiveRecyclableWidget;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import kotlin.h.b.n;

public class WidgetManager implements 0CH, 6Is {
    public static final String TAG = WidgetManager.class.getCanonicalName();
    public HYK asyncLayoutInflater;
    public boolean configured;
    public View contentView;
    public Context context;
    public Map<Object, Long> customCallBackInfoMap = new ConcurrentHashMap();
    public boolean isRTL;
    public 1Bk lifecycleRegistry = new 1Bk(this);
    public FluencyOpt mFluencyOpt;
    public C2888GjR mWidgetCreateTimeListener;
    public List<C2885GjO> mWidgetEventListeners;
    public Fragment parentFragment;
    public LayoutInflater syncLayoutInflater;
    public C2381GbG widgetCallback = new C2381GbG() {
        static {
            Covode.recordClassIndex(22991);
        }

        public final void loadRecyclableWidget(LiveRecyclableWidget liveRecyclableWidget) {
        }

        public final void loadSubWidget(LiveRecyclableWidget liveRecyclableWidget) {
        }

        public final void onHide(Widget widget) {
        }

        public final void onShow(Widget widget) {
        }

        public final void recycleRecyclableWidget(LiveRecyclableWidget liveRecyclableWidget) {
        }

        public final void removeAllMessages(Object obj) {
        }

        public final Fragment getFragment() {
            return WidgetManager.this.parentFragment;
        }

        public final void onPostCreate(Widget widget) {
            WidgetManager.this.notifyOnPostCreate(widget);
        }

        public final void onPostDestroy(Widget widget) {
            WidgetManager.this.notifyOnPostDestroy(widget);
        }

        public final void onPreCreate(Widget widget) {
            WidgetManager.this.notifyOnPreCreate(widget);
        }

        public final void onPreDestroy(Widget widget) {
            WidgetManager.this.notifyOnPreDestroy(widget);
        }

        public final void unloadWidget(Widget widget) {
            WidgetManager.this.unload(widget);
        }

        public final void loadWidget(Widget widget) {
            WidgetManager.this.load(widget);
        }

        public final void loadWidget(int i, Widget widget, boolean z) {
            WidgetManager.this.load(i, widget, z);
        }
    };
    public Map<Widget, ViewGroup> widgetViewGroupHashMap = new HashMap();
    public Set<Widget> widgets = new CopyOnWriteArraySet();

    public boolean checkWidgetContainer() {
        return true;
    }

    public void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_CREATE) {
            onCreate();
        } else if (r3 == 0CB.ON_START) {
            onStart();
        } else if (r3 == 0CB.ON_RESUME) {
            onResume();
        } else if (r3 == 0CB.ON_PAUSE) {
            onPause();
        } else if (r3 == 0CB.ON_STOP) {
            onStop();
        } else if (r3 == 0CB.ON_DESTROY) {
            onDestroy();
        }
    }

    public 0CD getLifecycle() {
        return this.lifecycleRegistry;
    }

    @0CN(LIZ = 0CB.ON_CREATE)
    public void onCreate() {
        this.lifecycleRegistry.LIZ(0CB.ON_CREATE);
    }

    @0CN(LIZ = 0CB.ON_PAUSE)
    public void onPause() {
        this.lifecycleRegistry.LIZ(0CB.ON_PAUSE);
    }

    @0CN(LIZ = 0CB.ON_RESUME)
    public void onResume() {
        this.lifecycleRegistry.LIZ(0CB.ON_RESUME);
    }

    @0CN(LIZ = 0CB.ON_START)
    public void onStart() {
        this.lifecycleRegistry.LIZ(0CB.ON_START);
    }

    @0CN(LIZ = 0CB.ON_STOP)
    public void onStop() {
        this.lifecycleRegistry.LIZ(0CB.ON_STOP);
    }

    static {
        Covode.recordClassIndex(22990);
    }

    public void detachWidget() {
        for (Widget onDetachWidget : this.widgets) {
            onDetachWidget.onDetachWidget();
        }
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public void onDestroy() {
        this.lifecycleRegistry.LIZ(0CB.ON_DESTROY);
        Fragment fragment = this.parentFragment;
        if (fragment != null) {
            fragment.getLifecycle().LIZIZ(this);
        }
        this.parentFragment = null;
        this.widgets.clear();
        this.widgetViewGroupHashMap.clear();
        this.customCallBackInfoMap.clear();
        FluencyOpt fluencyOpt = this.mFluencyOpt;
        if (fluencyOpt != null && fluencyOpt.shouldReleaseAsyncLayoutInflaterTaskQueue()) {
            HYL hyl = this.asyncLayoutInflater.LIZJ;
            HYN[] hynArr = (HYN[]) hyl.LIZIZ.toArray(new HYN[0]);
            hyl.LIZIZ.clear();
            for (HYN LIZ : hynArr) {
                hyl.LIZ(LIZ);
            }
        }
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            list.clear();
            this.mWidgetEventListeners = null;
        }
    }

    public View onProvideContainer(int i) {
        return this.contentView.findViewById(i);
    }

    public void addWidgetAndCallCustomBack(Widget widget) {
        boolean contains = this.widgets.contains(widget);
        this.widgets.add(widget);
        if (!contains) {
            onCustomInfoCallBack(widget);
        }
    }

    public void addWidgetEventListener(C2885GjO gjO) {
        if (gjO != null) {
            if (this.mWidgetEventListeners == null) {
                this.mWidgetEventListeners = new ArrayList();
            }
            this.mWidgetEventListeners.add(gjO);
        }
    }

    public void notifyOnPostCreate(Widget widget) {
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            for (C2885GjO LIZLLL : list) {
                LIZLLL.LIZLLL(widget);
            }
        }
    }

    public void notifyOnPostCreateView(Widget widget) {
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            for (C2885GjO LIZIZ : list) {
                LIZIZ.LIZIZ(widget);
            }
        }
    }

    public void notifyOnPostDestroy(Widget widget) {
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            for (C2885GjO LJFF : list) {
                LJFF.LJFF(widget);
            }
        }
    }

    public void notifyOnPreCreate(Widget widget) {
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            for (C2885GjO LIZJ : list) {
                LIZJ.LIZJ(widget);
            }
        }
    }

    public void notifyOnPreCreateView(Widget widget) {
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            for (C2885GjO LIZ : list) {
                LIZ.LIZ(widget);
            }
        }
    }

    public void notifyOnPreDestroy(Widget widget) {
        List<C2885GjO> list = this.mWidgetEventListeners;
        if (list != null) {
            for (C2885GjO LJ : list) {
                LJ.LJ(widget);
            }
        }
    }

    public WidgetManager load(Widget widget) {
        if (widget == null) {
            return this;
        }
        if (!widget.shouldAttach()) {
            widget.saveSnapShot(-1, false);
            addWidgetAndCallCustomBack(widget);
            return this;
        }
        widget.setWidgetCallback(this.widgetCallback);
        widget.context = this.context;
        boolean contains = this.widgets.contains(widget);
        this.widgets.add(widget);
        getLifecycle().LIZ(widget);
        if (!contains) {
            onCustomInfoCallBack(widget);
        }
        return this;
    }

    public void onCustomInfoCallBack(Widget widget) {
        for (Object next : this.customCallBackInfoMap.keySet()) {
            if (this.customCallBackInfoMap.get(next).longValue() < SystemClock.elapsedRealtime() && widget.hasRegister(next.getClass())) {
                widget.onCustomInfoCallBack(next);
            }
        }
    }

    public WidgetManager unload(Widget widget) {
        ViewParent parent;
        ViewGroup viewGroup;
        ViewGroup viewGroup2;
        MethodCollector.i(8659);
        if (widget == null) {
            MethodCollector.o(8659);
            return this;
        }
        getLifecycle().LIZIZ(widget);
        if (widget.isViewValid) {
            int i = 2.LIZ[getLifecycle().LIZ().ordinal()];
            if (i != 1) {
                if (i == 2) {
                    widget.performDestroy();
                } else if (i == 3) {
                    widget.performStop();
                    widget.performDestroy();
                } else if (i == 4) {
                    widget.performPause();
                    widget.performStop();
                    widget.performDestroy();
                }
            } else if (!widget.isDestroyed) {
                widget.performDestroy();
            }
        }
        if (widget.widgetContainer != null) {
            C2883GjM gjM = widget.widgetContainer;
            View view = widget.contentView;
            if (gjM.getParent() == null) {
                if (view == null) {
                    parent = null;
                } else {
                    parent = view.getParent();
                }
                if ((parent instanceof ViewGroup) && (viewGroup2 = (ViewGroup) parent) != null) {
                    viewGroup2.removeView(view);
                }
                ViewParent parent2 = gjM.getParent();
                if ((parent2 instanceof ViewGroup) && (viewGroup = (ViewGroup) parent2) != null) {
                    viewGroup.removeView(gjM);
                }
                C2882GjL gjL = gjM.LIZ;
                if (gjL != null) {
                    gjM.setId(gjL.LIZLLL);
                    if (view != null) {
                        view.setId(-1);
                    }
                    gjM.setVisibility(gjL.LJ);
                    ViewGroup viewGroup3 = gjL.LJFF;
                    int i2 = gjL.LIZJ;
                    ViewGroup.LayoutParams layoutParams = gjM.getLayoutParams();
                    layoutParams.width = gjL.LIZIZ;
                    layoutParams.height = gjL.LIZ;
                    viewGroup3.addView(gjM, i2, layoutParams);
                }
            }
        } else if (this.widgetViewGroupHashMap.containsKey(widget)) {
            if (this.widgetViewGroupHashMap.get(widget) != null) {
                onDetachContentView(widget, this.widgetViewGroupHashMap.get(widget), widget.contentView);
            }
            this.widgetViewGroupHashMap.remove(widget);
        }
        this.widgets.remove(widget);
        widget.context = null;
        widget.containerView = null;
        widget.widgetCallback = null;
        widget.widgetContainer = null;
        MethodCollector.o(8659);
        return this;
    }

    public WidgetManager load(int i, Widget widget) {
        return load(i, widget, true);
    }

    public <T> void onCustomInfoCallBack(Class<T> cls, T t) {
        if (t != null && cls != null) {
            for (Widget next : this.widgets) {
                if (next.hasRegister(cls)) {
                    next.onCustomInfoCallBack(t);
                }
            }
            this.customCallBackInfoMap.put(t, Long.valueOf(SystemClock.elapsedRealtime()));
        }
    }

    public static WidgetManager of(1hK r4, View view, FluencyOpt fluencyOpt) {
        WidgetManager widgetManager = new WidgetManager();
        1hK r1 = r4;
        widgetManager.config(r1, (Fragment) null, view, r1, fluencyOpt);
        return widgetManager;
    }

    public void onAttachContentView(Widget widget, ViewGroup viewGroup, View view) {
        MethodCollector.i(8047);
        if (!(viewGroup == null || view == null)) {
            viewGroup.addView(view);
        }
        MethodCollector.o(8047);
    }

    public void onDetachContentView(Widget widget, ViewGroup viewGroup, View view) {
        MethodCollector.i(8661);
        viewGroup.removeAllViews();
        MethodCollector.o(8661);
    }

    public static WidgetManager of(Fragment fragment, View view, FluencyOpt fluencyOpt) {
        WidgetManager widgetManager = new WidgetManager();
        Fragment fragment2 = fragment;
        widgetManager.config((1hK) null, fragment2, view, fragment2.getContext(), fluencyOpt);
        return widgetManager;
    }

    private WidgetManager loadNew(C2883GjM gjM, Widget widget, boolean z) {
        View view;
        widget.widgetContainer = gjM;
        if (widget.getLayoutId() == 0) {
            continueLoadNew(widget, gjM, (View) null);
        } else {
            if (z) {
                notifyOnPreCreateView(widget);
                System.currentTimeMillis();
                FluencyOpt fluencyOpt = this.mFluencyOpt;
                if (fluencyOpt == null || (view = fluencyOpt.getPreloadLayout(widget.getLayoutId())) == null) {
                    this.asyncLayoutInflater.LIZ(widget.getLayoutId(), (ViewGroup) gjM.getParent(), new C2886GjP(this, widget, gjM));
                }
            } else {
                notifyOnPreCreateView(widget);
                System.currentTimeMillis();
                FluencyOpt fluencyOpt2 = this.mFluencyOpt;
                if (fluencyOpt2 == null || (view = fluencyOpt2.getPreloadLayout(widget.getLayoutId())) == null) {
                    view = 0HH.LIZ(this.syncLayoutInflater, widget.getLayoutId(), (ViewGroup) gjM.getParent(), false);
                }
            }
            notifyOnPostCreateView(widget);
            continueLoadNew(widget, gjM, view);
        }
        return this;
    }

    private WidgetManager loadOld(ViewGroup viewGroup, Widget widget, boolean z) {
        View view;
        widget.containerView = viewGroup;
        this.widgetViewGroupHashMap.put(widget, viewGroup);
        if (widget.getLayoutId() == 0) {
            continueLoadOld(widget, viewGroup, (View) null);
            return this;
        }
        if (z) {
            notifyOnPreCreateView(widget);
            FluencyOpt fluencyOpt = this.mFluencyOpt;
            if (fluencyOpt == null || (view = fluencyOpt.getPreloadLayout(widget.getLayoutId())) == null) {
                this.asyncLayoutInflater.LIZ(widget.getLayoutId(), viewGroup, new C2887GjQ(this, widget, viewGroup));
            }
            notifyOnPostCreateView(widget);
            continueLoadOld(widget, viewGroup, view);
        } else {
            notifyOnPreCreateView(widget);
            FluencyOpt fluencyOpt2 = this.mFluencyOpt;
            if (fluencyOpt2 == null || (view = fluencyOpt2.getPreloadLayout(widget.getLayoutId())) == null) {
                view = 0HH.LIZ(this.syncLayoutInflater, widget.getLayoutId(), viewGroup, false);
            }
            notifyOnPostCreateView(widget);
            continueLoadOld(widget, viewGroup, view);
        }
        return this;
    }

    public void continueLoadOld(Widget widget, ViewGroup viewGroup, View view) {
        if (this.parentFragment != null) {
            widget.widgetContainer = null;
            if (widget.containerView != null) {
                onAttachContentView(widget, viewGroup, view);
                widget.setContentView(view);
                boolean contains = this.widgets.contains(widget);
                this.widgets.add(widget);
                C2888GjR gjR = this.mWidgetCreateTimeListener;
                if (gjR == null || !gjR.needUploadTime(widget)) {
                    getLifecycle().LIZ(widget);
                } else {
                    long elapsedRealtime = SystemClock.elapsedRealtime();
                    getLifecycle().LIZ(widget);
                    this.mWidgetCreateTimeListener.onLoad(widget, SystemClock.elapsedRealtime() - elapsedRealtime);
                }
                if (!contains) {
                    onCustomInfoCallBack(widget);
                }
            }
        }
    }

    public WidgetManager load(int i, Widget widget, boolean z) {
        if (this.parentFragment == null || widget == null) {
            return this;
        }
        widget.setWidgetCallback(this.widgetCallback);
        widget.context = this.context;
        widget.id = i;
        if (!widget.shouldAttach()) {
            widget.saveSnapShot(i, z);
            addWidgetAndCallCustomBack(widget);
            return this;
        } else if (this.contentView.findViewById(i) instanceof C2883GjM) {
            loadNew((C2883GjM) this.contentView.findViewById(i), widget, z);
            return this;
        } else {
            boolean z2 = false;
            Widget widget2 = null;
            for (Widget next : this.widgets) {
                if (next.getClass() == widget.getClass()) {
                    z2 = true;
                    widget2 = next;
                }
            }
            if (!C2889GjS.LIZ(widget) && z2) {
                unload(widget2);
                return load(i, widget, z);
            } else if (!checkWidgetContainer() || C2889GjS.LIZ(widget) || !C2889GjS.LIZ(this.context)) {
                loadOld((ViewGroup) onProvideContainer(i), widget, z);
                return this;
            } else {
                throw new C2884GjN(widget);
            }
        }
    }

    public void continueLoadNew(Widget widget, C2883GjM gjM, View view) {
        ViewGroup viewGroup;
        int i;
        int i2;
        int i3;
        ViewGroup.LayoutParams layoutParams;
        int i4;
        ViewGroup viewGroup2;
        MethodCollector.i(8459);
        if (this.parentFragment == null) {
            MethodCollector.o(8459);
            return;
        }
        if (view != null) {
            widget.setContentView(view);
        }
        if (gjM != null) {
            ViewParent parent = gjM.getParent();
            if ((parent instanceof ViewGroup) && (viewGroup = (ViewGroup) parent) != null) {
                if (view == null) {
                    viewGroup.removeViewInLayout(gjM);
                } else {
                    C2883GjM gjM2 = gjM;
                    int indexOfChild = viewGroup.indexOfChild(gjM2);
                    ViewGroup.LayoutParams layoutParams2 = gjM.getLayoutParams();
                    if (layoutParams2 == null) {
                        i = 0;
                    } else {
                        i = layoutParams2.height;
                    }
                    ViewGroup.LayoutParams layoutParams3 = gjM.getLayoutParams();
                    if (layoutParams3 == null) {
                        i2 = 0;
                    } else {
                        i2 = layoutParams3.width;
                    }
                    C2882GjL gjL = r4;
                    C2882GjL gjL2 = new C2882GjL(i, i2, indexOfChild, gjM.getId(), gjM.getVisibility(), viewGroup);
                    gjM.LIZ = gjL;
                    if (view.getVisibility() == 0 && gjM.getVisibility() == 0) {
                        view.setVisibility(0);
                    } else {
                        view.setVisibility(8);
                    }
                    if ((viewGroup instanceof RelativeLayout) || (viewGroup instanceof FrameLayout)) {
                        i3 = gjM.getId();
                    } else {
                        i3 = View.generateViewId();
                    }
                    view.setId(i3);
                    gjM.setId(-1);
                    viewGroup.removeViewInLayout(gjM2);
                    ViewParent parent2 = view.getParent();
                    if ((parent2 instanceof ViewGroup) && (viewGroup2 = (ViewGroup) parent2) != null) {
                        viewGroup2.removeView(view);
                    }
                    if (gjM.getLayoutParams() != null) {
                        if (view.getLayoutParams() == null) {
                            layoutParams = gjM.getLayoutParams();
                        } else {
                            layoutParams = gjM.getLayoutParams();
                            int i5 = -2;
                            if (gjM.LIZ(view.getLayoutParams().width)) {
                                i4 = view.getLayoutParams().width;
                            } else if (gjM.LIZ(layoutParams.width) || layoutParams.width == view.getLayoutParams().width) {
                                i4 = layoutParams.width;
                            } else {
                                i4 = -2;
                            }
                            layoutParams.width = i4;
                            if (gjM.LIZ(view.getLayoutParams().height)) {
                                i5 = view.getLayoutParams().height;
                            } else if (gjM.LIZ(layoutParams.height) || layoutParams.height == view.getLayoutParams().height) {
                                i5 = layoutParams.height;
                            }
                            layoutParams.height = i5;
                        }
                        viewGroup.addView(view, indexOfChild, layoutParams);
                    } else {
                        viewGroup.addView(view, indexOfChild);
                    }
                }
            }
            if (this.isRTL && view != null) {
                view.setLayoutDirection(1);
            }
        }
        boolean contains = this.widgets.contains(widget);
        this.widgets.add(widget);
        C2888GjR gjR = this.mWidgetCreateTimeListener;
        if (gjR == null || !gjR.needUploadTime(widget)) {
            getLifecycle().LIZ(widget);
        } else {
            long elapsedRealtime = SystemClock.elapsedRealtime();
            getLifecycle().LIZ(widget);
            this.mWidgetCreateTimeListener.onLoad(widget, SystemClock.elapsedRealtime() - elapsedRealtime);
        }
        if (!contains) {
            onCustomInfoCallBack(widget);
        }
        MethodCollector.o(8459);
    }

    public final /* synthetic */ void lambda$loadNew$0$WidgetManager(Widget widget, C2883GjM gjM, View view, int i, ViewGroup viewGroup) {
        notifyOnPostCreateView(widget);
        Fragment fragment = this.parentFragment;
        if (fragment != null && !fragment.isRemoving() && !this.parentFragment.isDetached() && getLifecycle().LIZ() != 0CC.DESTROYED) {
            continueLoadNew(widget, gjM, view);
        }
    }

    public final /* synthetic */ void lambda$loadOld$1$WidgetManager(Widget widget, ViewGroup viewGroup, View view, int i, ViewGroup viewGroup2) {
        notifyOnPostCreateView(widget);
        Fragment fragment = this.parentFragment;
        if (fragment != null && !fragment.isRemoving() && !this.parentFragment.isDetached() && getLifecycle().LIZ() != 0CC.DESTROYED) {
            continueLoadOld(widget, viewGroup, view);
        }
    }

    public void config(1hK r4, Fragment fragment, View view, Context context2, FluencyOpt fluencyOpt) {
        boolean z;
        if (!this.configured && fragment != null) {
            this.mFluencyOpt = fluencyOpt;
            this.parentFragment = fragment;
            this.contentView = view;
            this.context = context2;
            if (context2 == null || (!n.LIZ(context2.getResources().getConfiguration().locale.getLanguage(), "ar") && context2.getResources().getConfiguration().getLayoutDirection() != 1)) {
                z = false;
            } else {
                z = true;
            }
            this.isRTL = z;
            this.asyncLayoutInflater = new HYK(context2);
            this.syncLayoutInflater = LayoutInflater.from(context2);
            fragment.getLifecycle().LIZ(this);
            this.configured = true;
        }
    }
}
