package com.bytedance.assem.arch.core;

import X.0CB;
import X.0CC;
import X.0CD;
import X.0CH;
import X.1Bj;
import X.7he;
import X.Aob;
import X.BDh;
import X.BDk;
import X.BGB;
import X.C0295Crj;
import X.CuL;
import X.CuM;
import X.CuN;
import X.CuO;
import X.CuP;
import X.CuS;
import X.CxE;
import X.CxI;
import X.D1G;
import X.KEM;
import X.M12;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import com.bytedance.covode.number.Covode;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import kotlin.h.b.n;

public final class AssemSupervisor implements 1Bj {
    public static final BDh LIZ = new BDh((byte) 0);
    public final 0CH LIZIZ;
    public final Context LIZJ;
    public final Aob LIZLLL;
    public final CxE LJ;
    public volatile boolean LJFF;
    public final CopyOnWriteArrayList<CuL> LJI = new CopyOnWriteArrayList<>();
    public final ConcurrentHashMap<D1G<? extends CuL>, CxI> LJII = new ConcurrentHashMap<>();
    public View LJIIIIZZ;
    public final Map<D1G<? extends CuL>, CuL> LJIIIZ = new LinkedHashMap();
    public final C0295Crj LJIIJ = 7he.LIZ(BGB.LIZ);

    static {
        Covode.recordClassIndex(23890);
    }

    public final 0CD LIZ() {
        0CD lifecycle = this.LIZIZ.getLifecycle();
        M12.LIZ(lifecycle);
        return lifecycle;
    }

    public final void LIZIZ() {
        for (CuL cuL : this.LJI) {
            0CC r1 = 0CC.DESTROYED;
            n.LIZIZ(cuL, "");
            LIZ(r1, cuL);
        }
        this.LIZLLL.LIZ();
        this.LJ.LIZ();
        this.LJI.clear();
        this.LJIIIZ.clear();
        this.LJII.clear();
        LIZ().LIZIZ(this);
        LIZ((View) null);
    }

    private final void LIZJ() {
        if (CuS.LJI) {
            CopyOnWriteArrayList<CuL> copyOnWriteArrayList = this.LJI;
            ArrayList<CuL> arrayList = new ArrayList<>();
            for (T next : copyOnWriteArrayList) {
                CuL cuL = (CuL) next;
                if (cuL.LIZJ && cuL.LIZIZ) {
                    arrayList.add(next);
                }
            }
            for (CuL cuL2 : arrayList) {
                0CC r0 = 0CC.STARTED;
                n.LIZIZ(cuL2, "");
                LIZ(r0, cuL2);
            }
            return;
        }
        CopyOnWriteArrayList<CuL> copyOnWriteArrayList2 = this.LJI;
        ArrayList<CuL> arrayList2 = new ArrayList<>();
        for (T next2 : copyOnWriteArrayList2) {
            CuL cuL3 = (CuL) next2;
            if (!cuL3.LIZJ || cuL3.LIZIZ) {
                arrayList2.add(next2);
            }
        }
        for (CuL cuL4 : arrayList2) {
            0CC r02 = 0CC.STARTED;
            n.LIZIZ(cuL4, "");
            LIZ(r02, cuL4);
        }
    }

    private final void LIZLLL() {
        if (CuS.LJI) {
            CopyOnWriteArrayList<CuL> copyOnWriteArrayList = this.LJI;
            ArrayList<CuL> arrayList = new ArrayList<>();
            for (T next : copyOnWriteArrayList) {
                CuL cuL = (CuL) next;
                if (cuL.LIZJ && cuL.LIZIZ) {
                    arrayList.add(next);
                }
            }
            for (CuL cuL2 : arrayList) {
                0CC r0 = 0CC.RESUMED;
                n.LIZIZ(cuL2, "");
                LIZ(r0, cuL2);
            }
            return;
        }
        CopyOnWriteArrayList<CuL> copyOnWriteArrayList2 = this.LJI;
        ArrayList<CuL> arrayList2 = new ArrayList<>();
        for (T next2 : copyOnWriteArrayList2) {
            CuL cuL3 = (CuL) next2;
            if (!cuL3.LIZJ || cuL3.LIZIZ) {
                arrayList2.add(next2);
            }
        }
        for (CuL cuL4 : arrayList2) {
            0CC r02 = 0CC.RESUMED;
            n.LIZIZ(cuL4, "");
            LIZ(r02, cuL4);
        }
    }

    private final void LJ() {
        if (CuS.LJI) {
            CopyOnWriteArrayList<CuL> copyOnWriteArrayList = this.LJI;
            ArrayList<CuL> arrayList = new ArrayList<>();
            for (T next : copyOnWriteArrayList) {
                CuL cuL = (CuL) next;
                if (cuL.LIZJ && cuL.LIZIZ) {
                    arrayList.add(next);
                }
            }
            for (CuL cuL2 : arrayList) {
                0CC r0 = 0CC.STARTED;
                n.LIZIZ(cuL2, "");
                LIZ(r0, cuL2);
            }
            return;
        }
        CopyOnWriteArrayList<CuL> copyOnWriteArrayList2 = this.LJI;
        ArrayList<CuL> arrayList2 = new ArrayList<>();
        for (T next2 : copyOnWriteArrayList2) {
            CuL cuL3 = (CuL) next2;
            if (!cuL3.LIZJ || cuL3.LIZIZ) {
                arrayList2.add(next2);
            }
        }
        for (CuL cuL4 : arrayList2) {
            0CC r02 = 0CC.STARTED;
            n.LIZIZ(cuL4, "");
            LIZ(r02, cuL4);
        }
    }

    private final void LJFF() {
        if (CuS.LJI) {
            CopyOnWriteArrayList<CuL> copyOnWriteArrayList = this.LJI;
            ArrayList<CuL> arrayList = new ArrayList<>();
            for (T next : copyOnWriteArrayList) {
                CuL cuL = (CuL) next;
                if (cuL.LIZJ && cuL.LIZIZ) {
                    arrayList.add(next);
                }
            }
            for (CuL cuL2 : arrayList) {
                0CC r0 = 0CC.CREATED;
                n.LIZIZ(cuL2, "");
                LIZ(r0, cuL2);
            }
            return;
        }
        CopyOnWriteArrayList<CuL> copyOnWriteArrayList2 = this.LJI;
        ArrayList<CuL> arrayList2 = new ArrayList<>();
        for (T next2 : copyOnWriteArrayList2) {
            CuL cuL3 = (CuL) next2;
            if (!cuL3.LIZJ || cuL3.LIZIZ) {
                arrayList2.add(next2);
            }
        }
        for (CuL cuL4 : arrayList2) {
            0CC r02 = 0CC.CREATED;
            n.LIZIZ(cuL4, "");
            LIZ(r02, cuL4);
        }
    }

    public final void LIZ(View view) {
        if (view == null) {
            view = null;
        } else if (this.LJIIIIZZ != null) {
            throw new IllegalArgumentException("Duplicate attach the assems to root view on the same layer.".toString());
        }
        this.LJIIIIZZ = view;
    }

    public final void LIZ(CuL cuL) {
        CuN cuN = new CuN(cuL, this);
        if (n.LIZ(Thread.currentThread(), Looper.getMainLooper().getThread())) {
            cuN.invoke();
        } else {
            ((Handler) this.LJIIJ.getValue()).post(new CuP(cuN));
        }
    }

    public final void LIZIZ(CuL cuL) {
        M12.LIZ(cuL);
        if (LIZ().LIZ().compareTo(0CC.DESTROYED) <= 0) {
            BDk bDk = CuS.LIZJ;
            if (bDk != null) {
                bDk.LIZ("AssemList", "loadAssem return: " + this + ", " + LIZ().LIZ() + ", " + cuL);
            }
        } else if (!this.LJI.contains(cuL)) {
            this.LJI.add(cuL);
            cuL.LIZ(this);
            cuL.LIZ(this.LIZIZ);
            this.LJIIIZ.put(KEM.LIZ.LIZ(cuL.getClass()), cuL);
            if (!cuL.LIZJ) {
                M12.LIZ(new CuO(this, cuL));
            } else {
                cuL.LIZIZ = true;
            }
        } else {
            throw new IllegalStateException(n.LIZ("This assem has already existed: ", cuL).toString());
        }
    }

    public final void LIZ(0CC r3, CuL cuL) {
        M12.LIZ(r3, cuL);
        int i = CuM.LIZ[r3.ordinal()];
        if (i != 1) {
            if (i != 2) {
                if (i != 3) {
                    if (i == 4) {
                        if (cuL.LIZLLL.LIZ().compareTo(0CC.RESUMED) >= 0) {
                            cuL.LJIIJJI();
                        }
                        if (cuL.LIZLLL.LIZ().compareTo(0CC.STARTED) >= 0) {
                            cuL.LJIILIIL();
                        }
                        if (cuL.LIZLLL.LIZ().compareTo(0CC.CREATED) >= 0) {
                            cuL.LJIILL();
                        }
                    }
                } else if (cuL.LIZLLL.LIZ().compareTo(0CC.RESUMED) < 0) {
                    if (cuL.LIZLLL.LIZ().compareTo(0CC.CREATED) < 0) {
                        cuL.cY_();
                    }
                    if (cuL.LIZLLL.LIZ().compareTo(0CC.STARTED) < 0) {
                        cuL.LJII();
                    }
                    cuL.dd_();
                }
            } else if (cuL.LIZLLL.LIZ().compareTo(0CC.STARTED) < 0) {
                if (cuL.LIZLLL.LIZ().compareTo(0CC.CREATED) < 0) {
                    cuL.cY_();
                }
                cuL.LJII();
            } else if (cuL.LIZLLL.LIZ().compareTo(0CC.STARTED) > 0) {
                cuL.LJIIJJI();
            }
        } else if (cuL.LIZLLL.LIZ().compareTo(0CC.CREATED) < 0) {
            cuL.cY_();
        } else {
            if (cuL.LIZLLL.LIZ().compareTo(0CC.STARTED) > 0) {
                cuL.LJIIJJI();
            }
            if (cuL.LIZLLL.LIZ().compareTo(0CC.CREATED) > 0) {
                cuL.LJIILIIL();
            }
        }
    }

    public final void onStateChanged(0CH r6, 0CB r7) {
        M12.LIZ(r6, r7);
        switch (CuM.LIZIZ[r7.ordinal()]) {
            case 1:
                if (CuS.LJI) {
                    CopyOnWriteArrayList<CuL> copyOnWriteArrayList = this.LJI;
                    ArrayList<CuL> arrayList = new ArrayList<>();
                    for (T next : copyOnWriteArrayList) {
                        if (((CuL) next).LIZJ) {
                            arrayList.add(next);
                        }
                    }
                    for (CuL cuL : arrayList) {
                        0CC r0 = 0CC.CREATED;
                        n.LIZIZ(cuL, "");
                        LIZ(r0, cuL);
                    }
                    return;
                }
                CopyOnWriteArrayList<CuL> copyOnWriteArrayList2 = this.LJI;
                ArrayList<CuL> arrayList2 = new ArrayList<>();
                for (T next2 : copyOnWriteArrayList2) {
                    if (!((CuL) next2).LIZJ) {
                        arrayList2.add(next2);
                    }
                }
                for (CuL cuL2 : arrayList2) {
                    0CC r02 = 0CC.CREATED;
                    n.LIZIZ(cuL2, "");
                    LIZ(r02, cuL2);
                }
                return;
            case 2:
                LIZJ();
                return;
            case 3:
                LIZLLL();
                return;
            case 4:
                LJ();
                return;
            case 5:
                LJFF();
                return;
            case 6:
                LIZIZ();
                return;
            default:
                return;
        }
    }

    public AssemSupervisor(0CH r2, Context context, Aob aob, CxE cxE) {
        M12.LIZ(r2, aob, cxE);
        this.LIZIZ = r2;
        this.LIZJ = context;
        this.LIZLLL = aob;
        this.LJ = cxE;
    }
}
