package com.ss.android.ugc.aweme.shortvideo.cut;

import X.07U;
import X.0Bs;
import X.0Bv;
import X.0Bx;
import X.0By;
import X.0CB;
import X.0CD;
import X.0CH;
import X.0CN;
import X.0Gf;
import X.0Gp;
import X.1Bn;
import X.1hK;
import X.2GD;
import X.2Pr;
import X.7he;
import X.8NL;
import X.AnonymousClass520;
import X.AnonymousClass52A;
import X.AnonymousClass52C;
import X.AnonymousClass52S;
import X.AnonymousClass53P;
import X.AnonymousClass5EA;
import X.AnonymousClass5F3;
import X.AnonymousClass5TW;
import X.AnonymousClass66Y;
import X.AnonymousClass6ZR;
import X.AnonymousClass7PO;
import X.C015552z;
import X.C028457y;
import X.C07935Rn;
import X.C07945Ro;
import X.C07965Rq;
import X.C08235Sr;
import X.C10675ar;
import X.C10685as;
import X.C10695at;
import X.C10725aw;
import X.C10755az;
import X.C10765b0;
import X.C10785b2;
import X.C10845b8;
import X.C10855b9;
import X.C10875bB;
import X.C11105bY;
import X.C11135bb;
import X.C11355bx;
import X.C12285dS;
import X.C13415fH;
import X.C13705fk;
import X.C31126Is;
import X.C40906iW;
import X.C48756vB;
import X.C535677a;
import X.Crj;
import X.CwZ;
import X.Cwu;
import X.FWb;
import X.LWw;
import X.LeS;
import X.Lea;
import X.Ls2;
import X.M12;
import X.MpO;
import X.NtG;
import android.content.Intent;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.shortvideo.WorkSpace.Workspace;
import com.ss.android.ugc.aweme.shortvideo.cut.model.CutVideoContext;
import com.ss.android.ugc.aweme.shortvideo.cut.model.VideoSegment;
import com.ss.android.ugc.aweme.shortvideo.cut.videoedit.VideoEditViewModel;
import com.ss.android.ugc.aweme.shortvideo.cut.viewmodel.VEVideoCutterViewModel;
import com.ss.android.vesdk.VEEditor;
import com.ss.android.vesdk.VETimelineParams;
import com.ss.android.vesdk.VEUserConfig;
import dmt.av.video.VEEditorAutoStartStopArbiter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import kotlin.h.b.n;

public final class VECutVideoPresenter implements C31126Is {
    public int LIZ;
    public boolean LIZIZ;
    public boolean LIZJ;
    public float LIZLLL;
    public int LJ;
    public C10785b2 LJFF;
    public C48756vB LJI;
    public C13705fk LJII;
    public VEVideoCutterViewModel LJIIIIZZ;
    public VideoEditViewModel LJIIIZ;
    public AnonymousClass5TW LJIIJ;
    public C015552z LJIIJJI;
    public AnonymousClass66Y LJIIL;
    public boolean LJIILIIL;
    public VEEditorAutoStartStopArbiter LJIILJJIL;
    public final C10755az LJIILL;
    public boolean LJIILLIIL;
    public final Crj LJIIZILJ;
    public long LJIJ;
    public boolean LJIJI;
    public boolean LJIJJ;
    public CutMultiVideoViewModel LJIJJLI;
    public final String LJIL;

    static {
        Covode.recordClassIndex(108477);
    }

    public final boolean LIZJ() {
        return ((Boolean) this.LJIIZILJ.getValue()).booleanValue();
    }

    public final void onStateChanged(0CH r2, 0CB r3) {
        if (r3 == 0CB.ON_RESUME) {
            performInit();
        } else if (r3 == 0CB.ON_DESTROY) {
            destroy();
        }
    }

    public final void LIZ(List<? extends VideoSegment> list, int i) {
        07U<Long, Long> LIZLLL2 = this.LJIILL.LIZLLL();
        Long l = (Long) LIZLLL2.LIZ;
        if (l == null) {
            l = 0L;
        }
        n.LIZIZ(l, "");
        long longValue = l.longValue();
        Long l2 = (Long) LIZLLL2.LIZIZ;
        if (l2 == null) {
            l2 = Long.valueOf(this.LJIILL.LIZJ());
        }
        n.LIZIZ(l2, "");
        this.LJIIJ.LIZ(list, i, longValue, l2.longValue());
    }

    public final void LIZ(List<? extends VideoSegment> list, boolean z) {
        AnonymousClass5TW r1 = this.LJIIJ;
        if (r1.LIZJ) {
            C08235Sr r0 = r1.LIZIZ;
            if (r0 != null) {
                r0.LIZ(list, z);
            }
        } else {
            C08235Sr r02 = r1.LIZ;
            if (r02 != null) {
                r02.LIZ(list, z);
            }
        }
        this.LJIIJJI.LIZ(list, false);
    }

    public final void LIZ(Workspace workspace, BackgroundVideoCompileConfigure backgroundVideoCompileConfigure, boolean z, boolean z2, C11105bY r20, boolean z3) {
        long j;
        VideoSegment videoSegment;
        C11105bY r14 = r20;
        Workspace workspace2 = workspace;
        M12.LIZ(workspace2, r14);
        if (!this.LJIJJ) {
            AnonymousClass6ZR.LIZ("VECutVideo,return compile because of editor not init");
            return;
        }
        BackgroundVideoCompileConfigure backgroundVideoCompileConfigure2 = backgroundVideoCompileConfigure;
        boolean z4 = backgroundVideoCompileConfigure2 != null;
        long currentTimeMillis = System.currentTimeMillis();
        VideoEditViewModel videoEditViewModel = this.LJIIIZ;
        if (videoEditViewModel == null) {
            n.LIZ("");
        }
        List<VideoSegment> LJIIJ2 = videoEditViewModel.LJIIJ();
        long j2 = (LJIIJ2 == null || (videoSegment = (VideoSegment) Lea.LIZIZ(LJIIJ2, 0)) == null) ? -1 : videoSegment.LIZJ;
        CutMultiVideoViewModel cutMultiVideoViewModel = this.LJIJJLI;
        if (cutMultiVideoViewModel == null) {
            n.LIZ("");
        }
        if (j2 > cutMultiVideoViewModel.LJIIL) {
            C48756vB r1 = this.LJI;
            VideoEditViewModel videoEditViewModel2 = this.LJIIIZ;
            if (videoEditViewModel2 == null) {
                n.LIZ("");
            }
            List<VideoSegment> LJIIJ3 = videoEditViewModel2.LJIIJ();
            n.LIZIZ(LJIIJ3, "");
            AnonymousClass5EA.LIZ(r1, LJIIJ3);
        }
        C10785b2 r0 = this.LJFF;
        this.LJIJI = r0 != null ? r0.LIZLLL() : false;
        VEVideoCutterViewModel vEVideoCutterViewModel = this.LJIIIIZZ;
        if (vEVideoCutterViewModel == null) {
            n.LIZ("");
        }
        vEVideoCutterViewModel.LIZ(new C10765b0(2, true));
        C10785b2 r02 = this.LJFF;
        if (r02 != null) {
            j = r02.LIZ();
        } else {
            j = 0;
        }
        this.LJIJ = j;
        C10675ar r12 = new C10675ar(this);
        if (z2) {
            this.LIZ = -11008;
        } else if (!z4 && !z) {
            this.LJIILL.LJI();
            if (r12.LIZ() && !z3) {
                C11355bx.LJIIIIZZ.LIZ(true, LIZLLL(), String.valueOf(this.LIZ));
                r14.LIZ(true);
                AnonymousClass6ZR.LIZ("VECutVideo,using fastImport strategy");
                r14.onCompileDone();
                if (LIZJ()) {
                    C48756vB r03 = this.LJI;
                    if (r03 != null) {
                        r03.LIZJ().LJIIIIZZ();
                    }
                    C48756vB r04 = this.LJI;
                    if (r04 != null) {
                        r04.LIZJ().LJIIJ();
                    }
                    this.LJIJJ = false;
                    if (this.LJFF != null) {
                        this.LJFF = null;
                        if (this.LJIILJJIL != null) {
                            0CD lifecycle = this.LJIILL.LIZIZ().getLifecycle();
                            VEEditorAutoStartStopArbiter vEEditorAutoStartStopArbiter = this.LJIILJJIL;
                            if (vEEditorAutoStartStopArbiter == null) {
                                n.LIZIZ();
                            }
                            lifecycle.LIZIZ(vEEditorAutoStartStopArbiter);
                            return;
                        }
                        return;
                    }
                    return;
                }
                destroy();
                return;
            }
        }
        r14.LIZ(false);
        AnonymousClass6ZR.LIZ("VECutVideo,using normalImport strategy");
        0Gp.LIZ(new C12285dS(this, currentTimeMillis, backgroundVideoCompileConfigure2, workspace2, r14));
    }

    public final boolean LIZIZ() {
        C10785b2 r0 = this.LJFF;
        if (r0 != null) {
            return r0.LIZIZ();
        }
        return false;
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public final void destroy() {
        if (this.LJIJJ) {
            AnonymousClass6ZR.LIZLLL("VECutVideoPresenter Destroy method stack: \n" + C13415fH.LIZ(10));
            this.LJIJJ = false;
            C48756vB r0 = this.LJI;
            if (r0 != null) {
                r0.LJIL();
            }
            this.LJIILL.LJFF();
            if (this.LJFF != null) {
                this.LJFF = null;
            }
        }
    }

    public final int LIZLLL() {
        VideoEditViewModel videoEditViewModel = this.LJIIIZ;
        if (videoEditViewModel == null) {
            n.LIZ("");
        }
        if (videoEditViewModel.LJIIJ().size() == 1) {
            return 1;
        }
        VideoEditViewModel videoEditViewModel2 = this.LJIIIZ;
        if (videoEditViewModel2 == null) {
            n.LIZ("");
        }
        List<VideoSegment> LJIIJ2 = videoEditViewModel2.LJIIJ();
        n.LIZIZ(LJIIJ2, "");
        ArrayList arrayList = new ArrayList();
        for (T next : LJIIJ2) {
            if (!((VideoSegment) next).LJIIIZ) {
                arrayList.add(next);
            }
        }
        if (arrayList.size() > 1) {
            return 2;
        }
        return 3;
    }

    public final long LIZ() {
        C10785b2 r0 = this.LJFF;
        if (r0 != null) {
            return r0.LIZJ();
        }
        return 0;
    }

    @0CN(LIZ = 0CB.ON_RESUME)
    public final void performInit() {
        int size;
        C48756vB r8;
        boolean z;
        int LIZ2;
        C10785b2 r1;
        if (!this.LJIJJ) {
            VideoEditViewModel videoEditViewModel = this.LJIIIZ;
            if (videoEditViewModel == null) {
                n.LIZ("");
            }
            1Bn<CutVideoContext> r0 = videoEditViewModel.LJIL;
            n.LIZIZ(r0, "");
            if (r0.getValue() == null) {
                AnonymousClass6ZR.LIZIZ("VECutVideoPresenter,init error because of null cutVideoContext");
            } else {
                VideoEditViewModel videoEditViewModel2 = this.LJIIIZ;
                if (videoEditViewModel2 == null) {
                    n.LIZ("");
                }
                List<VideoSegment> LJIIJ2 = videoEditViewModel2.LJIIJ();
                if (!(LJIIJ2 == null || (size = LJIIJ2.size()) == 0)) {
                    this.LJIJJ = true;
                    if (AnonymousClass53P.LIZ()) {
                        r8 = new C48756vB(this.LJIL, this.LJIILL.LJII(), C028457y.LIZ((VEUserConfig) null));
                    } else {
                        r8 = new C48756vB(this.LJIL, this.LJIILL.LJII());
                    }
                    this.LJI = r8;
                    if (this.LJII == null) {
                        this.LJII = new C10855b9(this);
                    }
                    C13705fk r02 = this.LJII;
                    if (r02 == null) {
                        n.LIZ("");
                    }
                    r8.LIZJ(r02);
                    r8.LIZ(VEEditor.SCALE_MODE.SCALE_MODE_CENTER_INSIDE);
                    r8.LIZ(true);
                    if (this.LJIIL == null) {
                        this.LJIIL = new C10875bB(this);
                    }
                    AnonymousClass66Y r03 = this.LJIIL;
                    if (r03 == null) {
                        n.LIZ("");
                    }
                    r8.LIZ(r03);
                    int[] iArr = new int[size];
                    int[] iArr2 = new int[size];
                    float[] fArr = new float[size];
                    int[] iArr3 = new int[size];
                    int[] iArr4 = iArr2;
                    int[] iArr5 = iArr;
                    ArrayList arrayList = new ArrayList();
                    List<VideoSegment> list = LJIIJ2;
                    LIZ((List<? extends VideoSegment>) list, (List<String>) arrayList, iArr5, iArr4, fArr, iArr3);
                    Object[] array = arrayList.toArray(new String[0]);
                    Objects.requireNonNull(array, "null cannot be cast to non-null type kotlin.Array<T>");
                    VETimelineParams vETimelineParams = new VETimelineParams((String[]) array);
                    C48756vB r04 = this.LJI;
                    if (r04 == null) {
                        n.LIZIZ();
                    }
                    C07935Rn r9 = new C07935Rn(r04, vETimelineParams);
                    C48756vB r05 = this.LJI;
                    if (r05 == null) {
                        n.LIZIZ();
                    }
                    C07945Ro r12 = new C07945Ro(r05, vETimelineParams);
                    this.LJIIJ.LIZ = r9;
                    this.LJIIJ.LIZIZ = r12;
                    this.LJIIJJI.LIZ = this.LJI;
                    C48756vB r13 = this.LJI;
                    if (r13 == null) {
                        n.LIZIZ();
                    }
                    this.LJFF = new C10685as(r13, new C10725aw(this));
                    C48756vB r92 = this.LJI;
                    if (r92 != null) {
                        r92.LIZLLL(FWb.LIZIZ.LIZ().LJIIJ().getVideoWidth(), FWb.LIZIZ.LIZ().LJIIJ().getVideoHeight());
                        if (!C535677a.LIZLLL.LIZJ() || arrayList.size() <= 1) {
                            z = false;
                            Object[] array2 = arrayList.toArray(new String[0]);
                            Objects.requireNonNull(array2, "null cannot be cast to non-null type kotlin.Array<T>");
                            AnonymousClass52S r14 = new AnonymousClass52S((String[]) array2);
                            r14.LIZ(AnonymousClass520.VIDEO_OUT_RATIO_ORIGINAL);
                            r14.LIZ = iArr;
                            r14.LIZIZ = iArr2;
                            r14.LJI = fArr;
                            LIZ2 = r92.LIZ((AnonymousClass52C) r14);
                        } else {
                            z = true;
                            Object[] array3 = arrayList.toArray(new String[0]);
                            Objects.requireNonNull(array3, "null cannot be cast to non-null type kotlin.Array<T>");
                            LIZ2 = r92.LIZ((AnonymousClass52C) new AnonymousClass52A((String[]) array3, AnonymousClass520.VIDEO_OUT_RATIO_ORIGINAL));
                        }
                        if (LIZ2 == 0) {
                            C07965Rq.LIZ(vETimelineParams, LJIIJ2, iArr, iArr2, fArr, iArr3);
                            C48756vB r06 = this.LJI;
                            if (r06 != null) {
                                r06.LIZ(vETimelineParams);
                            }
                            this.LJIIJJI.LIZ((List<? extends VideoSegment>) LJIIJ2, false);
                            r92.LIZIZ(this.LJ);
                            r92.LJIIIZ(C40906iW.LIZ());
                            r92.LIZ(0, 1, 0.5f);
                            if (!z || !this.LIZJ) {
                                r92.LIZJ(1);
                            } else {
                                r92.LIZJ(0);
                            }
                            r92.LIZIZ(true);
                            r92.LIZJ().LJI();
                            07U<Long, Long> LIZLLL2 = this.LJIILL.LIZLLL();
                            Long l = (Long) LIZLLL2.LIZ;
                            if (l == null) {
                                l = 0L;
                            }
                            n.LIZIZ(l, "");
                            long longValue = l.longValue();
                            Long l2 = (Long) LIZLLL2.LIZIZ;
                            if (l2 == null) {
                                l2 = Long.valueOf(this.LJIILL.LIZJ());
                            }
                            n.LIZIZ(l2, "");
                            r92.LIZIZ((int) longValue, (int) l2.longValue());
                            if (LJIIJ2 != null && (!LJIIJ2.isEmpty())) {
                                VideoSegment videoSegment = LJIIJ2.get(0);
                                if (this.LJIILL.LJI() == 0 && videoSegment.LJIIJ > 0) {
                                    this.LJIIJ.LIZ((float) videoSegment.LJIIJ, videoSegment.LJIIJJI, videoSegment.LJIIL, 0, 0);
                                }
                            }
                            boolean z2 = this.LJIJI;
                            if (z2) {
                                C10785b2 r07 = this.LJFF;
                                if (r07 != null) {
                                    r07.LIZJ(z2);
                                }
                                this.LJIJI = false;
                            }
                            if ((!z || !this.LIZJ) && (r1 = this.LJFF) != null) {
                                boolean unused = r1.LIZ(this.LJIJ, VEEditor.SEEK_MODE.EDITOR_SEEK_FLAG_LastSeek, (8NL) null);
                            }
                            this.LJIILL.LIZ(this.LJI);
                            return;
                        }
                    }
                }
            }
            this.LJIILL.LJ();
        }
    }

    public static final /* synthetic */ VEVideoCutterViewModel LIZ(VECutVideoPresenter vECutVideoPresenter) {
        VEVideoCutterViewModel vEVideoCutterViewModel = vECutVideoPresenter.LJIIIIZZ;
        if (vEVideoCutterViewModel == null) {
            n.LIZ("");
        }
        return vEVideoCutterViewModel;
    }

    public static 0Bx LIZIZ(1hK r2) {
        0Bx LIZ2 = 0By.LIZ(r2, (0Bv) null);
        if (2GD.LIZ) {
            0Bs.LIZ(LIZ2, r2);
        }
        return LIZ2;
    }

    public static final /* synthetic */ VideoEditViewModel LIZIZ(VECutVideoPresenter vECutVideoPresenter) {
        VideoEditViewModel videoEditViewModel = vECutVideoPresenter.LJIIIZ;
        if (videoEditViewModel == null) {
            n.LIZ("");
        }
        return videoEditViewModel;
    }

    public final void LIZ(07U<Long, Long> r7) {
        long j;
        M12.LIZ(r7);
        long LIZJ2 = this.LJIILL.LIZJ();
        if (r7.LIZ != null) {
            Object obj = r7.LIZ;
            if (obj == null) {
                n.LIZIZ();
            }
            j = ((Number) obj).longValue();
        } else {
            j = 0;
        }
        if (r7.LIZIZ != null) {
            Object obj2 = r7.LIZIZ;
            if (obj2 == null) {
                n.LIZIZ();
            }
            LIZJ2 = ((Number) obj2).longValue();
        }
        AnonymousClass5TW r3 = this.LJIIJ;
        int i = (int) j;
        int i2 = (int) LIZJ2;
        if (r3.LIZJ) {
            C08235Sr r0 = r3.LIZIZ;
            if (r0 != null) {
                r0.LIZJ(i, i2);
                return;
            }
            return;
        }
        C08235Sr r02 = r3.LIZ;
        if (r02 != null) {
            r02.LIZJ(i, i2);
        }
    }

    public final void LIZ(1hK r4) {
        M12.LIZ(r4);
        CutMultiVideoViewModel LIZ2 = LIZIZ(r4).LIZ(CutMultiVideoViewModel.class);
        n.LIZIZ(LIZ2, "");
        this.LJIJJLI = LIZ2;
        VEVideoCutterViewModel LIZ3 = LIZIZ(r4).LIZ(VEVideoCutterViewModel.class);
        n.LIZIZ(LIZ3, "");
        this.LJIIIIZZ = LIZ3;
        VideoEditViewModel LIZ4 = LIZIZ(r4).LIZ(VideoEditViewModel.class);
        n.LIZIZ(LIZ4, "");
        this.LJIIIZ = LIZ4;
        this.LJIILIIL = n.LIZ("from_chat", LIZ(r4.getIntent(), "enter_from"));
        this.LJIILL.LIZIZ().getLifecycle().LIZ(this);
        VEVideoCutterViewModel vEVideoCutterViewModel = this.LJIIIIZZ;
        if (vEVideoCutterViewModel == null) {
            n.LIZ("");
        }
        vEVideoCutterViewModel.LIZ().observe(this.LJIILL.LIZIZ(), new C10695at(this));
    }

    public final void LIZ(Ls2<2Pr> ls2) {
        0Gp.LIZ(new C10845b8(ls2), 0Gp.LIZIZ, (0Gf) null);
    }

    public /* synthetic */ VECutVideoPresenter(String str, C10755az r3) {
        this(str, r3, true);
    }

    public static String LIZ(Intent intent, String str) {
        try {
            return intent.getStringExtra(str);
        } catch (Exception unused) {
            return null;
        }
    }

    public VECutVideoPresenter(String str, C10755az r5, boolean z) {
        M12.LIZ(str, r5);
        this.LJIL = str;
        this.LJIILL = r5;
        this.LJIILLIIL = z;
        this.LJIIZILJ = 7he.LIZ(new C11135bb(this));
        this.LIZ = -1;
        this.LIZIZ = true;
        this.LIZJ = true;
        this.LJ = NtG.LIZLLL.LIZ(true, false, false, false);
        this.LJIIJ = new AnonymousClass5TW();
        this.LJIIJJI = new C015552z();
    }

    private final void LIZ(List<? extends VideoSegment> list, List<String> list2, int[] iArr, int[] iArr2, float[] fArr, int[] iArr3) {
        if (list != null && (!list.isEmpty())) {
            int i = 0;
            for (T next : list) {
                int i2 = i + 1;
                if (i < 0) {
                    LeS.LIZIZ();
                }
                VideoSegment videoSegment = (VideoSegment) next;
                String LIZ2 = videoSegment.LIZ(false);
                n.LIZIZ(LIZ2, "");
                list2.add(LIZ2);
                iArr[i] = (int) videoSegment.LIZLLL();
                iArr2[i] = (int) videoSegment.LJ();
                fArr[i] = videoSegment.LJFF();
                iArr3[i] = videoSegment.LJIIJ;
                i = i2;
            }
            if (this.LJIILL.LJI() == 0) {
                iArr[0] = 0;
                iArr2[0] = (int) ((VideoSegment) list.get(0)).LIZJ;
                iArr3[0] = 0;
            }
        }
    }

    public final void LIZ(int i, long j, long j2, AnonymousClass7PO r9, int i2, String str) {
        MpO.LIZ("av_ve_import_compile", i, AnonymousClass5F3.LIZ(LWw.LIZ(new Cwu[]{CwZ.LIZ("fps_time", String.valueOf(j)), CwZ.LIZ("compile_time", String.valueOf(j2)), CwZ.LIZ("is_hard_encode", String.valueOf(r9.LJI)), CwZ.LIZ("fps", String.valueOf(r9.LJFF)), CwZ.LIZ("errorCode", String.valueOf(i2)), CwZ.LIZ("error_message", str)})));
    }

    public static /* synthetic */ void LIZ(VECutVideoPresenter vECutVideoPresenter, int i, float f, boolean z, float f2, float f3, int i2, int i3, int i4) {
        float f4 = f2;
        int i5 = i2;
        if ((i4 & 4) != 0) {
            z = true;
        }
        float f5 = 1.0f;
        if ((i4 & 8) != 0) {
            f4 = 1.0f;
        }
        if ((i4 & 16) == 0) {
            f5 = f3;
        }
        int i6 = 0;
        if ((i4 & 32) != 0) {
            i5 = 0;
        }
        if ((i4 & 64) == 0) {
            i6 = i3;
        }
        float f6 = f;
        vECutVideoPresenter.LIZLLL = f6;
        if (z) {
            vECutVideoPresenter.LJIIJ.LIZ(f6, f4, f5, i5, i6);
            return;
        }
        AnonymousClass5TW r1 = vECutVideoPresenter.LJIIJ;
        if (r1.LIZ != null) {
            C08235Sr r0 = r1.LIZ;
            if (r0 == null) {
                n.LIZIZ();
            }
            r0.LIZ(i, f6);
        }
    }
}
