package com.ss.android.ugc.aweme.sticker.presenter.handler;

import X.0CB;
import X.0CC;
import X.0CD;
import X.0CH;
import X.0CN;
import X.2Pr;
import X.5fX;
import X.6Is;
import X.C2515NHa;
import X.C2517NHc;
import X.C2519NHe;
import X.C2522NHh;
import X.C2523NHi;
import X.C4159NsY;
import X.C4167Nsg;
import X.Fpl;
import X.IFh;
import X.LsC;
import X.LsF;
import X.M12;
import X.NBS;
import X.NFH;
import X.NFL;
import X.NFV;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.effect.model.Effect;
import kotlin.h.b.n;

public final class AudioGraphStickerHandler extends NFV implements 6Is {
    public static Fpl LIZIZ;
    public static boolean LIZJ;
    public static boolean LIZLLL;
    public static boolean LJ;
    public static final NFL LJFF = new NFL((byte) 0);
    public final C2515NHa LIZ;
    public final MicStickerAudioController LJI;

    static {
        Covode.recordClassIndex(116115);
    }

    public final void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_STOP) {
            onStop();
        }
    }

    public final boolean LJ() {
        if (LIZIZ != null) {
            return true;
        }
        return false;
    }

    private final boolean LJFF() {
        Fpl fpl = LIZIZ;
        if (fpl == null || !fpl.enableMic()) {
            return false;
        }
        return true;
    }

    private final boolean LJI() {
        Fpl fpl = LIZIZ;
        if (fpl == null || !fpl.enableMusic()) {
            return false;
        }
        return true;
    }

    @0CN(LIZ = 0CB.ON_STOP)
    public final void onStop() {
        if (LIZIZ != null) {
            LIZ();
            this.LIZ.LJIIIZ();
        }
    }

    public final void LIZIZ() {
        if (LJ() && LJ && this.LIZ.LJII()) {
            this.LIZ.LJIIIZ();
        }
    }

    public final void LIZJ() {
        if (LJ() && !LJ && this.LIZ.LJII()) {
            this.LIZ.LJIIIIZZ();
        }
    }

    public final void LIZLLL() {
        if (LJ() && this.LIZ.LJII()) {
            this.LIZ.LJIIIIZZ();
        }
    }

    public final void LIZ() {
        this.LIZ.LJIIIZ();
        LIZIZ = null;
        C2523NHi.LIZ(this.LIZ, false, false, 15);
        LIZJ = false;
        LIZLLL = false;
        if (LJFF()) {
            MicStickerAudioController micStickerAudioController = this.LJI;
            C2519NHe nHe = C2519NHe.LIZ;
            5fX LJFF2 = C4167Nsg.LJIIIZ.LJFF();
            LJFF2.LIZLLL(micStickerAudioController.LIZJ + " cancelSticker isStop " + micStickerAudioController.LJFF);
            micStickerAudioController.LIZ = null;
            LsC<Boolean, 2Pr> lsC = micStickerAudioController.LJII;
            if (lsC != null) {
                lsC.invoke(false);
            }
            if (n.LIZ(micStickerAudioController.LJFF.LIZ(), true)) {
                micStickerAudioController.LIZ(nHe);
            } else {
                micStickerAudioController.LIZIZ = true;
            }
        }
        this.LIZ.LIZIZ();
    }

    public final boolean LIZ(NBS nbs) {
        M12.LIZ(nbs);
        Fpl fpl = (Fpl) C4167Nsg.LJIIIZ.LIZJ().LIZ(NFH.LIZ(nbs.LIZ.getSdkExtra(), "audio_graph"), Fpl.class);
        LIZIZ = fpl;
        if (fpl != null) {
            return true;
        }
        return false;
    }

    public final void LIZJ(NBS nbs) {
        if (LJI()) {
            this.LIZ.LIZ(nbs.LIZ);
        }
        if (!LJ) {
            this.LIZ.LJIIIIZZ();
            this.LIZ.LJIIIZ();
        }
        if (this.LIZ.LJII()) {
            if (LJ) {
                this.LIZ.LJIIIZ();
            }
            this.LIZ.LJIIIIZZ();
        }
    }

    public final void LIZ(IFh iFh, NBS nbs) {
        boolean z;
        Boolean bool;
        Boolean useOutput;
        M12.LIZ(iFh, nbs);
        this.LIZ.LIZ();
        this.LIZ.LJIIIZ();
        LIZJ = this.LIZ.LJ();
        LIZLLL = this.LIZ.LJFF();
        this.LIZ.LIZLLL();
        this.LIZ.LIZJ();
        C2515NHa nHa = this.LIZ;
        Fpl fpl = LIZIZ;
        if (fpl == null || (useOutput = fpl.getUseOutput()) == null) {
            z = false;
        } else {
            z = useOutput.booleanValue();
        }
        C2523NHi.LIZ(nHa, z, true, 12);
        this.LIZ.LJIIJ();
        this.LIZ.LJIIJJI();
        if (!LJFF() || (this.LIZ.LJFF() && !this.LIZ.LJI())) {
            LIZJ(nbs);
            return;
        }
        MicStickerAudioController micStickerAudioController = this.LJI;
        Effect effect = nbs.LIZ;
        C2522NHh nHh = C2522NHh.LIZ;
        C2517NHc nHc = new C2517NHc(this, nbs);
        M12.LIZ(effect);
        5fX LJFF2 = C4167Nsg.LJIIIZ.LJFF();
        LJFF2.LIZLLL(micStickerAudioController.LIZJ + " useSticker lifecycleOwner isActive: sticker " + effect);
        micStickerAudioController.LIZ = effect;
        LsC<Boolean, 2Pr> lsC = micStickerAudioController.LJII;
        if (lsC != null) {
            lsC.invoke(true);
        }
        if (((Boolean) micStickerAudioController.LJIIIIZZ.invoke()).booleanValue()) {
            micStickerAudioController.LIZIZ = false;
            0CD lifecycle = micStickerAudioController.LIZLLL.getLifecycle();
            n.LIZIZ(lifecycle, "");
            if (lifecycle.LIZ().isAtLeast(0CC.STARTED)) {
                micStickerAudioController.LJ.LIZ(nHh, nHc);
                if (LIZIZ != null) {
                    LsF<Boolean, Boolean, Boolean, Boolean, 2Pr> lsF = micStickerAudioController.LJIIIZ;
                    Fpl fpl2 = LIZIZ;
                    if (fpl2 != null) {
                        bool = fpl2.getUseOutput();
                    } else {
                        bool = null;
                    }
                    lsF.invoke(Boolean.valueOf(n.LIZ(bool, true)), true, Boolean.valueOf(LIZJ), Boolean.valueOf(LIZLLL));
                    return;
                }
                return;
            }
            return;
        }
        C4159NsY.LIZJ.LIZJ(micStickerAudioController.LJI, 2131834266, 1).LIZ();
        nHc.run();
    }

    public AudioGraphStickerHandler(0CH r3, MicStickerAudioController micStickerAudioController, C2515NHa nHa) {
        M12.LIZ(r3, micStickerAudioController, nHa);
        this.LJI = micStickerAudioController;
        this.LIZ = nHa;
        r3.getLifecycle().LIZ(this);
    }

    public static /* synthetic */ void LIZ(AudioGraphStickerHandler audioGraphStickerHandler, Boolean bool, Boolean bool2, boolean z, int i) {
        boolean z2;
        boolean z3;
        Boolean bool3 = null;
        if ((i & 1) != 0) {
            bool = null;
        }
        if ((i & 2) != 0) {
            bool2 = null;
        }
        if ((i & 4) != 0) {
            z = false;
        }
        if (!audioGraphStickerHandler.LJ()) {
            return;
        }
        if (bool != null || bool2 != null || z) {
            if ((bool != null && (!n.LIZ(bool, Boolean.valueOf(LIZJ)))) || ((bool2 != null && (!n.LIZ(bool2, Boolean.valueOf(LIZLLL)))) || z)) {
                C2515NHa nHa = audioGraphStickerHandler.LIZ;
                Fpl fpl = LIZIZ;
                if (fpl != null) {
                    bool3 = fpl.getUseOutput();
                }
                boolean LIZ2 = n.LIZ(bool3, true);
                if (bool != null) {
                    z2 = bool.booleanValue();
                } else {
                    z2 = LIZJ;
                }
                if (bool2 != null) {
                    z3 = bool2.booleanValue();
                } else {
                    z3 = LIZLLL;
                }
                nHa.LIZ(LIZ2, true, z2, z3);
                if (bool != null) {
                    LIZJ = bool.booleanValue();
                }
                if (bool2 != null) {
                    LIZLLL = bool2.booleanValue();
                }
            }
        }
    }
}
