package com.bytedance.ies.sdk.widgets;

import X.059;
import X.0CB;
import X.0CH;
import X.1hK;
import X.2HN;
import X.2Pr;
import X.6Is;
import X.7hc;
import X.7he;
import X.C2381GbG;
import X.CEi;
import X.Crj;
import X.M12;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.bytedance.android.widget.Widget;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.ies.sdk.widgets.LiveRecyclableWidget;
import java.util.Objects;

public final class LayeredWidgetManager extends RecyclableWidgetManager implements 6Is {
    public static final Companion Companion = new Companion((2HN) null);
    public final Crj animatableRecyclableWidgets$delegate = 7he.LIZ(7hc.NONE, LayeredWidgetManager$animatableRecyclableWidgets$2.INSTANCE);
    public final LayeredElementManager<? extends LayeredElementContext> layeredElementManager;

    public final boolean checkWidgetContainer() {
        return false;
    }

    public final void onStateChanged(0CH r1, 0CB r2) {
        super.onStateChanged(r1, r2);
    }

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

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

        public Companion() {
        }

        public final LayeredWidgetManager of(Fragment fragment, View view, boolean z, IWidgetProvider iWidgetProvider, FluencyOpt fluencyOpt, LayeredElementManager<? extends LayeredElementContext> layeredElementManager) {
            Fragment fragment2 = fragment;
            M12.LIZ(fragment2, layeredElementManager);
            LayeredWidgetManager layeredWidgetManager = new LayeredWidgetManager(iWidgetProvider, layeredElementManager);
            layeredWidgetManager.config((1hK) null, fragment2, view, fragment2.getContext(), fluencyOpt);
            layeredWidgetManager.mIsVisibilityToUser = z;
            return layeredWidgetManager;
        }
    }

    public final LayeredElementManager<? extends LayeredElementContext> getLayeredElementManager() {
        return this.layeredElementManager;
    }

    static {
        Covode.recordClassIndex(30516);
    }

    public final 059<IAnimatableWidget> getAnimatableRecyclableWidgets() {
        return (059) this.animatableRecyclableWidgets$delegate.getValue();
    }

    public final class LayeredWidgetCallback extends LiveRecyclableWidget.RecyclableWidgetCallbackWrapper {
        public final /* synthetic */ LayeredWidgetManager this$0;

        static {
            Covode.recordClassIndex(30519);
        }

        public final void onHide(Widget widget) {
            M12.LIZ(widget);
            super.onHide(widget);
            this.this$0.getLayeredElementManager().onWidgetHiddenChanged(widget.getId(), true);
        }

        public final void onPostUnload(LiveRecyclableWidget liveRecyclableWidget) {
            IAnimatableWidget iAnimatableWidget;
            M12.LIZ(liveRecyclableWidget);
            super.onPostUnload(liveRecyclableWidget);
            if ((liveRecyclableWidget instanceof IAnimatableWidget) && (iAnimatableWidget = (IAnimatableWidget) liveRecyclableWidget) != null) {
                this.this$0.getAnimatableRecyclableWidgets().LIZIZ(iAnimatableWidget.getId());
            }
        }

        public final void onPreLoad(LiveRecyclableWidget liveRecyclableWidget) {
            IAnimatableWidget iAnimatableWidget;
            M12.LIZ(liveRecyclableWidget);
            super.onPreLoad(liveRecyclableWidget);
            if ((liveRecyclableWidget instanceof IAnimatableWidget) && (iAnimatableWidget = (IAnimatableWidget) liveRecyclableWidget) != null) {
                this.this$0.getAnimatableRecyclableWidgets().LIZIZ(iAnimatableWidget.getId(), iAnimatableWidget);
            }
        }

        public final void onShow(Widget widget) {
            M12.LIZ(widget);
            super.onShow(widget);
            this.this$0.getLayeredElementManager().onWidgetHiddenChanged(widget.getId(), false);
        }

        /* JADX INFO: super call moved to the top of the method (can break code semantics) */
        public LayeredWidgetCallback(LayeredWidgetManager layeredWidgetManager, LiveRecyclableWidget.RecyclableWidgetCallback recyclableWidgetCallback) {
            super(recyclableWidgetCallback);
            M12.LIZ(layeredWidgetManager, recyclableWidgetCallback);
            this.this$0 = layeredWidgetManager;
        }

        public final void onDetachContentView(LiveRecyclableWidget liveRecyclableWidget, ViewGroup viewGroup, View view) {
            M12.LIZ(liveRecyclableWidget);
            if (view != null) {
                if (!this.this$0.getLayeredElementManager().detachView(view.getId())) {
                    super.onDetachContentView(liveRecyclableWidget, viewGroup, view);
                }
                if (2Pr.LIZ != null) {
                    return;
                }
            }
            super.onDetachContentView(liveRecyclableWidget, viewGroup, view);
        }
    }

    public final View onProvideContainer(int i) {
        View findViewById = this.contentView.findViewById(i);
        if (findViewById instanceof CEi) {
            View view = this.contentView;
            Objects.requireNonNull(view, "null cannot be cast to non-null type android.view.ViewGroup");
            return (ViewGroup) view;
        }
        if (findViewById == null) {
            findViewById = this.contentView;
        }
        M12.LIZ(findViewById);
        return findViewById;
    }

    /* JADX INFO: super call moved to the top of the method (can break code semantics) */
    public LayeredWidgetManager(IWidgetProvider iWidgetProvider, LayeredElementManager<? extends LayeredElementContext> layeredElementManager2) {
        super(iWidgetProvider);
        M12.LIZ(layeredElementManager2);
        this.layeredElementManager = layeredElementManager2;
        C2381GbG gbG = this.widgetCallback;
        Objects.requireNonNull(gbG, "null cannot be cast to non-null type com.bytedance.ies.sdk.widgets.LiveRecyclableWidget.RecyclableWidgetCallback");
        this.widgetCallback = new LayeredWidgetCallback(this, (LiveRecyclableWidget.RecyclableWidgetCallback) gbG);
        layeredElementManager2.setAnimationListener(new AnimationListener(this) {
            public final /* synthetic */ LayeredWidgetManager this$0;

            static {
                Covode.recordClassIndex(30517);
            }

            {
                this.this$0 = r1;
            }

            public final void onHideAnimationEnd(int i) {
                IAnimatableWidget iAnimatableWidget = (IAnimatableWidget) this.this$0.getAnimatableRecyclableWidgets().LIZ(i, (Object) null);
                if (iAnimatableWidget != null) {
                    iAnimatableWidget.onHideAnimationEnd();
                }
            }

            public final void onHideAnimationStart(int i) {
                IAnimatableWidget iAnimatableWidget = (IAnimatableWidget) this.this$0.getAnimatableRecyclableWidgets().LIZ(i, (Object) null);
                if (iAnimatableWidget != null) {
                    iAnimatableWidget.onHideAnimationStart();
                }
            }

            public final void onShowAnimationEnd(int i) {
                IAnimatableWidget iAnimatableWidget = (IAnimatableWidget) this.this$0.getAnimatableRecyclableWidgets().LIZ(i, (Object) null);
                if (iAnimatableWidget != null) {
                    iAnimatableWidget.onShowAnimationEnd();
                }
            }

            public final void onShowAnimationStart(int i) {
                IAnimatableWidget iAnimatableWidget = (IAnimatableWidget) this.this$0.getAnimatableRecyclableWidgets().LIZ(i, (Object) null);
                if (iAnimatableWidget != null) {
                    iAnimatableWidget.onShowAnimationStart();
                }
            }
        });
    }

    public final void onDetachContentView(Widget widget, ViewGroup viewGroup, View view) {
        ILayeredWidget iLayeredWidget;
        MethodCollector.i(8572);
        if (viewGroup == null) {
            MethodCollector.o(8572);
        } else if (view == null) {
            MethodCollector.o(8572);
        } else if (widget == null) {
            MethodCollector.o(8572);
        } else {
            if ((widget instanceof ILayeredWidget) && (iLayeredWidget = (ILayeredWidget) widget) != null) {
                iLayeredWidget.setLayeredElementContext((LayeredElementContext) null);
            }
            if (!this.layeredElementManager.detachView(view.getId())) {
                viewGroup.removeView(view);
            }
            MethodCollector.o(8572);
        }
    }

    public final void onAttachContentView(Widget widget, ViewGroup viewGroup, View view) {
        ILayeredWidget iLayeredWidget;
        MethodCollector.i(8570);
        if (viewGroup == null) {
            MethodCollector.o(8570);
        } else if (view == null) {
            MethodCollector.o(8570);
        } else if (widget == null) {
            MethodCollector.o(8570);
        } else {
            if ((widget instanceof ILayeredWidget) && (iLayeredWidget = (ILayeredWidget) widget) != null) {
                iLayeredWidget.setLayeredElementContext(this.layeredElementManager.getLayeredElementContext());
            }
            if (this.contentView.findViewById(widget.getId()) instanceof CEi) {
                view.setId(widget.getId());
                this.layeredElementManager.attachView(widget.getId(), view);
                MethodCollector.o(8570);
                return;
            }
            viewGroup.addView(view);
            MethodCollector.o(8570);
        }
    }
}
