package com.ss.android.ugc.aweme.tools.extract.video;

import X.0Gp;
import X.0Gq;
import X.0HH;
import X.3jN;
import X.7lW;
import X.7zK;
import X.805;
import X.AnonymousClass5E9;
import X.C35526Zq;
import X.C46796s1;
import X.C46826s4;
import X.C46836s5;
import X.C47116sX;
import X.C47126sY;
import X.C47136sZ;
import X.C47146sa;
import X.C47536tD;
import X.C47546tE;
import X.C47556tF;
import X.C47566tG;
import X.C47596tJ;
import X.FW6;
import X.FWb;
import X.IUu;
import X.Jr2;
import X.Jr5;
import X.JrZ;
import X.KHs;
import X.M12;
import X.NtX;
import X.PUu;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import androidx.core.app.SafeJobIntentService;
import com.bytedance.covode.number.Covode;
import com.bytedance.helios.sdk.utils.ServiceLifecycle;
import com.bytedance.ies.ugc.aweme.network.RetrofitFactory;
import com.google.gson.e;
import com.ss.android.ugc.aweme.base.api.BaseResponse;
import com.ss.android.ugc.aweme.shortvideo.AVApiImpl;
import com.ss.android.ugc.aweme.shortvideo.model.ExtractFramesModel;
import com.ss.android.ugc.aweme.shortvideo.model.FrameItem;
import com.ss.android.ugc.aweme.uploader.factory.AbstractImageUploader;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;

public class VideoFramesUploadService extends SafeJobIntentService {
    public static final String LIZ = VideoFramesUploadService.class.getSimpleName();

    public interface FramesUploadApi {
        static {
            Covode.recordClassIndex(119810);
        }

        @IUu(LIZ = "/aweme/v2/aweme/vframe/update/")
        @C47596tJ
        0Gp<BaseResponse> uploadFrame(@PUu(LIZ = "aweme_id") String str, @PUu(LIZ = "video_id") String str2, @PUu(LIZ = "vframe_uri") String str3, @PUu(LIZ = "stickers") String str4, @PUu(LIZ = "aweme_type") Integer num);

        @IUu(LIZ = "/tiktok/v1/multi/vframe/update/")
        @C47596tJ
        0Gp<BaseResponse> uploadMultiFrame(@PUu(LIZ = "vframe_requests") JSONArray jSONArray);
    }

    /* JADX WARNING: type inference failed for: r1v0, types: [androidx.core.app.JobIntentService, android.app.Service, com.ss.android.ugc.aweme.tools.extract.video.VideoFramesUploadService] */
    public int onStartCommand(Intent intent, int i, int i2) {
        ServiceLifecycle.onStartCommand(this, intent, i, i2);
        return VideoFramesUploadService.super.onStartCommand(intent, i, i2);
    }

    public final 0Gp<BaseResponse> LIZ(C47146sa r12, String str) {
        0Gp<BaseResponse> uploadFrame;
        try {
            C46796s1.LIZ(str, "create_report_task");
            FramesUploadApi framesUploadApi = (FramesUploadApi) RetrofitFactory.LIZ().LIZ(AVApiImpl.LIZIZ().LIZ()).LIZ(FramesUploadApi.class);
            if (r12.LIZ.isEmpty()) {
                return 0Gp.LIZ(new IllegalStateException("VideoFramesUploadModel is empty"));
            }
            if (r12.LIZ.size() > 1) {
                0Gp<BaseResponse> uploadMultiFrame = framesUploadApi.uploadMultiFrame(C47136sZ.LIZ(r12));
                uploadMultiFrame.LJFF();
                for (C47126sY next : r12.LIZ) {
                    if (!uploadMultiFrame.LIZ()) {
                        LIZ(next, "task fail with exception : " + Log.getStackTraceString(uploadMultiFrame.LJ()), -3003);
                    } else if (((BaseResponse) uploadMultiFrame.LIZLLL()).status_code == 0) {
                        LIZ(next);
                    } else {
                        LIZ(next, "response: " + ((BaseResponse) uploadMultiFrame.LIZLLL()).toString(), -3003);
                    }
                }
                return uploadMultiFrame;
            }
            C47126sY r1 = r12.LIZ.get(0);
            Integer valueOf = r1.LIZIZ == -1 ? null : Integer.valueOf(r1.LIZIZ);
            C35526Zq.LIZLLL("[Original Frame] Reporting -AwemeId:" + r1.LIZ);
            if (r1.LJFF == null || r1.LJFF.getStickerIds() == null) {
                uploadFrame = framesUploadApi.uploadFrame(r1.LIZ, r1.LIZJ, r1.LIZLLL, (String) null, valueOf);
            } else {
                uploadFrame = framesUploadApi.uploadFrame(r1.LIZ, r1.LIZJ, r1.LIZLLL, r1.LJFF.getStickerIds(), valueOf);
            }
            uploadFrame.LJFF();
            if (!uploadFrame.LIZ()) {
                C46796s1.LIZ(str, "report_fail");
                LIZ(r1, "task fail with exception : " + Log.getStackTraceString(uploadFrame.LJ()), -3002);
            } else if (((BaseResponse) uploadFrame.LIZLLL()).status_code == 0) {
                C46796s1.LIZ(str, "report_success");
                LIZ(r1);
                C35526Zq.LIZLLL("[Original Frame] Report Success -AwemeId:" + r1.LIZ);
            } else {
                C46796s1.LIZ(str, "report_fail");
                LIZ(r1, "response: " + ((BaseResponse) uploadFrame.LIZLLL()).toString(), -3002);
            }
            return uploadFrame;
        } catch (Exception e) {
            C46796s1.LIZ(str, "report_error");
            return 0Gp.LIZ(e);
        }
    }

    public final 0Gp<C47146sa> LIZ(C47146sa r9, JrZ jrZ, String str) {
        C46796s1.LIZ(str, "create_upload_task");
        for (C47126sY next : r9.LIZ) {
            try {
                0Gp<C47126sY> LIZ2 = LIZ(next, jrZ);
                LIZ2.LJFF();
                if (LIZ2.LIZJ() || LIZ2.LIZIZ()) {
                    FW6 LJIIL = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                    LJIIL.LIZ("extract_frame", "upload failed for awemeId: " + next.LIZ + ", reason: " + Log.getStackTraceString(LIZ2.LJ()));
                } else {
                    FW6 LJIIL2 = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                    LJIIL2.LIZ("extract_frame", "upload uri: " + next.LIZLLL + ", awemeId: " + next.LIZ + " success");
                }
            } catch (InterruptedException e) {
                FW6 LJIIL3 = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                LJIIL3.LIZ("extract_frame", "upload interrupted for awemeId: " + next.LIZ + ", reason: " + Log.getStackTraceString(e));
            }
        }
        return 0Gp.LIZ(r9);
    }

    public final void LIZ(C47126sY r5, long j, String str) {
        C46836s5 r3 = new C46836s5();
        r3.LIZ = r5.LIZ;
        r3.LJ = Boolean.valueOf(r5.LJIIJJI);
        r3.LIZLLL = Boolean.valueOf(r5.LJIIIZ);
        r3.LIZIZ = r5.LJIIIIZZ;
        r3.LIZJ = Integer.valueOf(r5.LJIIJ);
        boolean z = true;
        if (r5.LJIIL <= 1) {
            z = false;
        }
        r3.LJII = Boolean.valueOf(z);
        r3.LJIIIIZZ = j;
        r3.LJI = 0;
        r3.LIZ(str);
        r3.LJFF = -3001;
        C46826s4.LIZ(r3);
    }

    public final boolean LIZ() {
        return FWb.LIZIZ.LIZ().LJJIIJ() != null && FWb.LIZIZ.LIZ().LJJIIJ().LIZ();
    }

    static {
        Covode.recordClassIndex(119808);
    }

    private void LIZ(C47126sY r4) {
        boolean z;
        C46836s5 r2 = new C46836s5();
        r2.LIZ = r4.LIZ;
        r2.LJ = Boolean.valueOf(r4.LJIIJJI);
        r2.LIZLLL = Boolean.valueOf(r4.LJIIIZ);
        r2.LIZIZ = r4.LJIIIIZZ;
        r2.LIZJ = Integer.valueOf(r4.LJIIJ);
        if (r4.LJIIL > 1) {
            z = true;
        } else {
            z = false;
        }
        r2.LJII = Boolean.valueOf(z);
        r2.LJI = 1;
        C46826s4.LIZ(r2);
    }

    public void onHandleWork(Intent intent) {
        String str;
        int i;
        if (!NtX.LJIJ.LIZ()) {
            Intent intent2 = intent;
            String LIZ2 = LIZ(intent2, "aid");
            C46796s1.LIZ(LIZ2, "handle_work");
            Context applicationContext = getApplicationContext();
            if (7lW.LIZJ && applicationContext == null) {
                applicationContext = 7lW.LIZ;
            }
            C47116sX LIZ3 = C47116sX.LIZ(applicationContext);
            if (intent2 != null) {
                str = LIZ(intent2, "authkey");
            } else {
                str = "";
            }
            if (TextUtils.isEmpty(str)) {
                C46796s1.LIZ(LIZ2, "authkey_error");
                return;
            }
            try {
                JrZ jrZ = (JrZ) new e().LIZIZ().LIZ(str, JrZ.class);
                if (!(jrZ == null || jrZ.LIZLLL == null)) {
                    C46796s1.LIZ(LIZ2, "database_query");
                    List<C47126sY> LIZ4 = LIZ3.LIZ();
                    M12.LIZ(LIZ4);
                    ArrayList<C47146sa> arrayList = new ArrayList<>();
                    LinkedHashMap linkedHashMap = new LinkedHashMap();
                    for (T next : LIZ4) {
                        String str2 = ((C47126sY) next).LJII;
                        Object obj = linkedHashMap.get(str2);
                        if (obj == null) {
                            obj = new ArrayList();
                            linkedHashMap.put(str2, obj);
                        }
                        ((List) obj).add(next);
                    }
                    Iterator it = linkedHashMap.entrySet().iterator();
                    while (true) {
                        i = 0;
                        if (!it.hasNext()) {
                            break;
                        }
                        Map.Entry entry = (Map.Entry) it.next();
                        String str3 = (String) entry.getKey();
                        List<C47126sY> list = (List) entry.getValue();
                        if (str3 == null || str3.length() == 0) {
                            for (C47126sY LIZ5 : list) {
                                C47146sa r0 = new C47146sa();
                                r0.LIZ(LIZ5);
                                arrayList.add(r0);
                            }
                        } else {
                            C47146sa r2 = new C47146sa();
                            for (C47126sY LIZ6 : list) {
                                r2.LIZ(LIZ6);
                            }
                            arrayList.add(r2);
                        }
                    }
                    if (arrayList.isEmpty()) {
                        C46796s1.LIZ(LIZ2, "database_query_empty");
                        FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL().LIZ("extract_frame", "task is empty");
                        return;
                    }
                    if (C47566tG.LIZ()) {
                        LIZ3.LIZ((List<C47146sa>) arrayList);
                    }
                    FW6 LJIIL = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                    LJIIL.LIZ("extract_frame", "pending task count: " + arrayList.size());
                    for (C47146sa r13 : arrayList) {
                        if (!r13.LIZ.isEmpty()) {
                            if (System.currentTimeMillis() - r13.LIZ.get(i).LJI > 21600000) {
                                LIZ(r13);
                                LIZ(r13, LIZ3);
                            } else {
                                0Gp<C47146sa> LIZ7 = LIZ(r13, LIZ3, LIZ2);
                                C47546tE r02 = r10;
                                C47546tE r10 = new C47546tE(this, LIZ3, r13, jrZ, LIZ2);
                                try {
                                    LIZ7.LIZIZ(r02).LIZIZ(new C47536tD(this, LIZ3, LIZ2)).LIZ(new C47556tF(this, r13, LIZ3)).LIZ(3jN.LIZ).LJFF();
                                    FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL().LIZ("extract_frame", "upload extract frame success.");
                                } catch (InterruptedException e) {
                                    0HH.LIZ(e);
                                }
                                i = 0;
                            }
                        }
                    }
                    return;
                }
            } catch (Exception unused) {
            }
            C46796s1.LIZ(LIZ2, "authkey_error_2");
        }
    }

    private void LIZ(C47146sa r6) {
        for (C47126sY next : r6.LIZ) {
            C46836s5 r3 = new C46836s5();
            r3.LIZ = next.LIZ;
            r3.LJ = Boolean.valueOf(next.LJIIJJI);
            r3.LIZLLL = Boolean.valueOf(next.LJIIIZ);
            r3.LIZIZ = next.LJIIIIZZ;
            r3.LIZJ = Integer.valueOf(next.LJIIJ);
            boolean z = true;
            if (next.LJIIL <= 1) {
                z = false;
            }
            r3.LJII = Boolean.valueOf(z);
            r3.LJI = 0;
            r3.LJFF = -4001;
            C46826s4.LIZJ(r3);
        }
    }

    private void LIZ(String str) {
        FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL().LIZ("extract_frame", "msg = ".concat(String.valueOf(str)));
        C46826s4.LIZ((Object) null, -1, 14, str);
        AnonymousClass5E9.LIZ.LIZIZ();
    }

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

    public final void LIZ(C47146sa r3, C47116sX r4) {
        for (C47126sY LIZ2 : r3.LIZ) {
            LIZ(LIZ2, r4);
        }
    }

    private void LIZ(C47126sY r4, C47116sX r5) {
        r5.LIZ(r4.LIZ);
        if (r4.LJFF != null) {
            805.LJ(r4.LJFF.getExtractFramesDir());
            805.LIZJ(r4.LJFF.getExtractFramesDir());
        }
        FW6 LJIIL = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
        LJIIL.LIZ("extract_frame", "cleanup frame, awemeId: " + r4.LIZ);
    }

    private 0Gp<C47126sY> LIZ(final C47126sY r9, JrZ jrZ) {
        final AbstractImageUploader LIZ2;
        if (LIZ()) {
            return 0Gp.LIZ(new IllegalStateException("stop task manually for specified case"));
        }
        if (!TextUtils.isEmpty(r9.LIZLLL)) {
            FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL().LIZ("extract_frame", "skip upload");
            return 0Gp.LIZ(r9);
        }
        final 0Gq r4 = new 0Gq();
        try {
            if (r9.LIZIZ == 150) {
                LIZ2 = Jr2.LIZ(jrZ.LJFF);
            } else {
                LIZ2 = Jr2.LIZ(jrZ.LIZLLL);
            }
            LIZ2.LIZ((Jr5) new Jr5() {
                static {
                    Covode.recordClassIndex(119809);
                }

                public final void LIZ(String str) {
                }

                public final void LIZ(int i, long j, AbstractImageUploader.ImageUploadInfo imageUploadInfo) {
                    long j2;
                    String str;
                    if (i == 6) {
                        LIZ2.LIZIZ();
                        C47126sY r1 = r9;
                        if (imageUploadInfo != null) {
                            str = imageUploadInfo.getMImageToskey();
                        } else {
                            str = null;
                        }
                        r1.LIZLLL = str;
                        r4.LIZIZ(r9);
                    } else if (i == 7) {
                        JSONArray jSONArray = new JSONArray();
                        LIZ2.LIZIZ();
                        VideoFramesUploadService videoFramesUploadService = VideoFramesUploadService.this;
                        C47126sY r6 = r9;
                        long j3 = 0;
                        if (imageUploadInfo != null) {
                            j2 = imageUploadInfo.getMErrorCode();
                        } else {
                            j2 = 0;
                        }
                        StringBuilder sb = new StringBuilder("what : ");
                        sb.append(i);
                        sb.append(", code: ");
                        if (imageUploadInfo != null) {
                            j3 = imageUploadInfo.getMErrorCode();
                        }
                        sb.append(j3);
                        sb.append(", events: ");
                        sb.append(jSONArray.toString());
                        videoFramesUploadService.LIZ(r6, j2, sb.toString());
                        r4.LIZIZ(new IllegalStateException("upload zip file failed"));
                    }
                }
            });
            KHs kHs = new KHs();
            kHs.LIZ(jrZ.LIZJ);
            LIZ2.LIZ(kHs.LIZ());
            LIZ2.LIZ(FWb.LIZIZ.LIZ().LJJI().LJIILJJIL());
            LIZ2.LIZ(1, new String[]{r9.LJ});
            C35526Zq.LIZLLL("[Original Frame] Uploading -AwemeId:" + r9.LIZ);
            LIZ2.LIZ();
        } catch (Exception e) {
            LIZ2.LIZIZ();
            LIZ(r9, 0, Log.getStackTraceString(e));
            throw e;
        } catch (Throwable th) {
            LIZ(r9, 0, Log.getStackTraceString(th));
            r4.LIZIZ(new Exception(th));
        }
        return r4.LIZ;
    }

    private void LIZ(C47126sY r5, String str, int i) {
        C46836s5 r3 = new C46836s5();
        r3.LIZ = r5.LIZ;
        r3.LJ = Boolean.valueOf(r5.LJIIJJI);
        r3.LIZLLL = Boolean.valueOf(r5.LJIIIZ);
        r3.LIZIZ = r5.LJIIIIZZ;
        r3.LIZJ = Integer.valueOf(r5.LJIIJ);
        boolean z = true;
        if (r5.LJIIL <= 1) {
            z = false;
        }
        r3.LJII = Boolean.valueOf(z);
        r3.LIZ(str);
        r3.LJI = 0;
        r3.LJFF = Integer.valueOf(i);
        C46826s4.LIZ(r3);
    }

    private 0Gp<C47146sa> LIZ(C47146sa r11, C47116sX r12, String str) {
        0Gp r7;
        FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL().LIZ("extract_frame", "ready to package zip");
        Iterator<C47126sY> it = r11.LIZ.iterator();
        C46796s1.LIZ(str, "create_package_task");
        while (it.hasNext()) {
            C47126sY next = it.next();
            if (next == null) {
                LIZ("model == null");
                r7 = 0Gp.LIZ(new IllegalStateException("the upload model is null"));
            } else {
                try {
                    if (TextUtils.isEmpty(next.LJ) || !new File(next.LJ).exists()) {
                        ExtractFramesModel extractFramesModel = next.LJFF;
                        if (extractFramesModel == null) {
                            r7 = 0Gp.LIZ(new IllegalStateException("the upload frameModel is null"));
                        } else {
                            List<FrameItem> allFrames = next.LJFF.getAllFrames();
                            if (allFrames.isEmpty()) {
                                r7 = 0Gp.LIZ(new IllegalStateException("the upload frames is empty"));
                            } else {
                                ArrayList arrayList = new ArrayList();
                                for (FrameItem next2 : allFrames) {
                                    if (!805.LIZIZ(next2.getPath())) {
                                        LIZ("extract file does not exist, video id = " + next.LIZJ + ", dir:" + next2.getPath());
                                    } else if (!arrayList.contains(next2.getPath())) {
                                        arrayList.add(next2.getPath());
                                    }
                                }
                                C35526Zq.LIZLLL("[Original Frame] Packing, total " + arrayList.size() + " files -AwemeId:" + next.LIZ);
                                next.LJ = 7zK.LIZ(extractFramesModel.getExtractFramesDir(), arrayList);
                                if (next.LJ == null || !805.LIZIZ(next.LJ)) {
                                    LIZ("upload zipPath is empty video id = " + next.LIZJ + " , zipPath: " + next.LJ);
                                    r7 = 0Gp.LIZ(new IllegalStateException("the upload zipPath is empty"));
                                }
                            }
                        }
                    }
                    r7 = 0Gp.LIZ(next);
                } catch (InterruptedException e) {
                    C46796s1.LIZ(str, "create_package_task_error");
                    FW6 LJIIL = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                    LJIIL.LIZ("extract_frame", "zip interrupted for awemeId: " + next.LIZ + ", reason: " + Log.getStackTraceString(e));
                }
            }
            r7.LJFF();
            if (r7.LIZJ() || r7.LIZIZ()) {
                it.remove();
                LIZ(next, r12);
                FW6 LJIIL2 = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                LJIIL2.LIZ("extract_frame", "zip failed for awemeId: " + next.LIZ + ", reason: " + Log.getStackTraceString(r7.LJ()));
                C46836s5 r3 = new C46836s5();
                r3.LIZ = next.LIZ;
                r3.LJ = Boolean.valueOf(next.LJIIJJI);
                r3.LIZLLL = Boolean.valueOf(next.LJIIIZ);
                r3.LIZIZ = next.LJIIIIZZ;
                r3.LIZJ = Integer.valueOf(next.LJIIJ);
                boolean z = true;
                if (next.LJIIL <= 1) {
                    z = false;
                }
                r3.LJII = Boolean.valueOf(z);
                r3.LJI = 0;
                r3.LIZ(Log.getStackTraceString(r7.LJ()));
                r3.LJFF = -2001;
                C46826s4.LIZ(r3);
            } else {
                FW6 LJIIL3 = FWb.LIZIZ.LIZ().LJIILLIIL().LJIIL();
                LJIIL3.LIZ("extract_frame", "zip path: " + next.LJ + ", awemeId: " + next.LIZ + " success");
            }
        }
        return 0Gp.LIZ(r11);
    }
}
