package com.ss.android.ugc.aweme.arch.widgets.base;

import X.04m;
import X.04n;
import X.0A8;
import X.0AA;
import X.0AP;
import X.0CC;
import X.0CH;
import X.1hK;
import X.3Ez;
import X.CvF;
import X.FIO;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class WidgetManager extends Fragment {
    public static final String LIZLLL = WidgetManager.class.getCanonicalName();
    public Fragment LIZ;
    public DataCenter LIZIZ;
    public 0A8 LIZJ = new 0A8() {
        static {
            Covode.recordClassIndex(53525);
        }

        public final void onFragmentViewDestroyed(0AA r3, Fragment fragment) {
            if (fragment == WidgetManager.this.LIZ) {
                r3.LIZ(WidgetManager.this.LIZJ);
                0AP LIZ2 = fragment.getChildFragmentManager().LIZ();
                LIZ2.LIZ(WidgetManager.this);
                LIZ2.LJ();
            }
        }
    };
    public Thread LJ;
    public Handler LJFF = new Handler(Looper.getMainLooper());
    public FIO LJI = new FIO() {
        static {
            Covode.recordClassIndex(53524);
        }

        public final 0CH LIZ() {
            return WidgetManager.this;
        }

        public final Activity LIZIZ() {
            return WidgetManager.this.getActivity();
        }

        public final void LIZ(Intent intent, int i) {
            WidgetManager.this.startActivityForResult(intent, i);
        }
    };
    public View LJII;
    public Context LJIIIIZZ;
    public 04n LJIIIZ;
    public LayoutInflater LJIIJ;
    public List<Widget> LJIIJJI = new CopyOnWriteArrayList();

    static {
        Covode.recordClassIndex(53523);
    }

    private boolean LIZ() {
        if (this.LJ == null) {
            this.LJ = Looper.getMainLooper().getThread();
        }
        if (Thread.currentThread() == this.LJ) {
            return true;
        }
        return false;
    }

    public final WidgetManager LIZIZ(Widget widget) {
        MethodCollector.i(7727);
        if (widget == null) {
            MethodCollector.o(7727);
            return this;
        }
        getLifecycle().LIZIZ(widget);
        int i = CvF.LIZ[getLifecycle().LIZ().ordinal()];
        if (i == 2) {
            widget.onDestroy();
        } else if (i == 3) {
            widget.onStop();
            widget.onDestroy();
        } else if (i == 4) {
            widget.onPause();
            widget.onStop();
            widget.onDestroy();
        }
        widget.LJFF = null;
        widget.LJ = null;
        this.LJIIJJI.remove(widget);
        if (widget.LIZJ != widget.LIZLLL && (widget.LIZJ instanceof ViewGroup)) {
            ((ViewGroup) widget.LIZJ).removeAllViews();
        }
        MethodCollector.o(7727);
        return this;
    }

    public final WidgetManager LIZ(DataCenter dataCenter) {
        this.LIZIZ = dataCenter;
        for (Widget widget : this.LJIIJJI) {
            widget.LJ = dataCenter;
        }
        return this;
    }

    public final WidgetManager LIZ(Widget widget) {
        if (widget == null) {
            return this;
        }
        widget.LJFF = this.LJI;
        widget.LIZIZ = this.LJIIIIZZ;
        widget.LJ = this.LIZIZ;
        this.LJIIJJI.add(widget);
        getLifecycle().LIZ(widget);
        return this;
    }

    public static WidgetManager LIZ(Fragment fragment, View view) {
        return LIZ((1hK) null, fragment, view, fragment.getContext());
    }

    public final WidgetManager LIZIZ(int i, Widget widget) {
        if (widget == null) {
            return this;
        }
        LIZ(this.LJII.findViewById(i), widget);
        return this;
    }

    public final WidgetManager LIZ(int i, final Widget widget) {
        if (widget == null) {
            return this;
        }
        widget.LJFF = this.LJI;
        widget.LIZIZ = this.LJIIIIZZ;
        widget.LJ = this.LIZIZ;
        final ViewGroup viewGroup = (ViewGroup) this.LJII.findViewById(i);
        widget.LIZJ = viewGroup;
        if (widget.LIZJ() == 0) {
            LIZ(widget, viewGroup, (View) null);
            return this;
        }
        this.LJIIIZ.LIZ(widget.LIZJ(), viewGroup, new 04m() {
            static {
                Covode.recordClassIndex(53526);
            }

            public final void onInflateFinished(View view, int i, ViewGroup viewGroup) {
                if (!WidgetManager.this.isRemoving() && !WidgetManager.this.isDetached() && WidgetManager.this.getLifecycle().LIZ() != 0CC.DESTROYED) {
                    WidgetManager.this.LIZ(widget, viewGroup, view);
                }
            }
        });
        return this;
    }

    public final WidgetManager LIZ(View view, Widget widget) {
        if (widget == null) {
            return this;
        }
        widget.LJFF = this.LJI;
        widget.LIZIZ = this.LJIIIIZZ;
        widget.LJ = this.LIZIZ;
        widget.LIZLLL = view;
        this.LJIIJJI.add(widget);
        if (!LIZ()) {
            this.LJFF.post(new 3Ez(this, widget));
            return this;
        }
        getLifecycle().LIZ(widget);
        return this;
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        WidgetManager.super.onActivityResult(i, i2, intent);
        for (Widget LIZ2 : this.LJIIJJI) {
            LIZ2.LIZ(i, i2, intent);
        }
    }

    public final void LIZ(Widget widget, View view, View view2) {
        MethodCollector.i(7337);
        widget.LIZLLL = view2;
        if ((view instanceof ViewGroup) && view2 != null) {
            ((ViewGroup) view).addView(view2);
        }
        this.LJIIJJI.add(widget);
        getLifecycle().LIZ(widget);
        MethodCollector.o(7337);
    }

    public static WidgetManager LIZ(1hK r4, Fragment fragment, View view, Context context) {
        0AA childFragmentManager;
        if (r4 != null) {
            childFragmentManager = r4.getSupportFragmentManager();
        } else if (fragment == null) {
            return null;
        } else {
            childFragmentManager = fragment.getChildFragmentManager();
        }
        WidgetManager widgetManager = new WidgetManager();
        widgetManager.LIZ = fragment;
        widgetManager.LJII = view;
        widgetManager.LJIIIIZZ = context;
        widgetManager.LJIIIZ = new 04n(widgetManager.LJIIIIZZ);
        widgetManager.LJIIJ = LayoutInflater.from(widgetManager.LJIIIIZZ);
        if (!(fragment == null || fragment.getFragmentManager() == null)) {
            fragment.getFragmentManager().LIZ(widgetManager.LIZJ, false);
        }
        0AP LIZ2 = childFragmentManager.LIZ();
        LIZ2.LIZ(widgetManager, LIZLLL);
        LIZ2.LJ();
        return widgetManager;
    }
}
