package com.tencent.mm.plugin.wepkg.version;

import android.os.Parcel;
import android.os.Parcelable.Creator;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.ai.b;
import com.tencent.mm.ai.b.a;
import com.tencent.mm.ai.w;
import com.tencent.mm.plugin.report.service.h;
import com.tencent.mm.plugin.wepkg.ipc.WepkgMainProcessService;
import com.tencent.mm.plugin.wepkg.model.BaseWepkgProcessTask;
import com.tencent.mm.plugin.wepkg.model.WepkgVersion;
import com.tencent.mm.plugin.wepkg.utils.WepkgRunCgi;
import com.tencent.mm.plugin.wepkg.utils.WepkgRunCgi.1;
import com.tencent.mm.plugin.wepkg.utils.WepkgRunCgi.RemoteCgiTask;
import com.tencent.mm.plugin.wepkg.utils.d;
import com.tencent.mm.protocal.protobuf.sh;
import com.tencent.mm.protocal.protobuf.si;
import com.tencent.mm.protocal.protobuf.ss;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ah;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.ytcommon.BuildConfig;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public final class WepkgVersionUpdater {

    static final class WepkgNetSceneProcessTask extends BaseWepkgProcessTask {
        public static final Creator<WepkgNetSceneProcessTask> CREATOR = new 2();
        private int cTb;
        private List<WepkgCheckReq> uYi;
        private boolean uYj;

        /* synthetic */ WepkgNetSceneProcessTask(Parcel parcel, byte b) {
            this(parcel);
        }

        public WepkgNetSceneProcessTask() {
            AppMethodBeat.i(63674);
            this.uYi = new ArrayList();
            AppMethodBeat.o(63674);
        }

        private WepkgNetSceneProcessTask(Parcel parcel) {
            AppMethodBeat.i(63675);
            g(parcel);
            AppMethodBeat.o(63675);
        }

        public final void asP() {
            AppMethodBeat.i(63676);
            if (bo.ek(this.uYi)) {
                AppMethodBeat.o(63676);
                return;
            }
            a aVar = new a();
            aVar.fsI = 1313;
            aVar.fsL = 0;
            aVar.fsM = 0;
            aVar.uri = "/cgi-bin/mmgame-bin/checkwepkgversion";
            sh shVar = new sh();
            for (WepkgCheckReq wepkgCheckReq : this.uYi) {
                ss ssVar = new ss();
                ssVar.mZV = WepkgCheckReq.a(wepkgCheckReq);
                ssVar.jCh = WepkgCheckReq.b(wepkgCheckReq);
                ssVar.Scene = WepkgCheckReq.c(wepkgCheckReq);
                shVar.vZD.add(ssVar);
            }
            aVar.fsJ = shVar;
            aVar.fsK = new si();
            h.pYm.a(859, 15, 1, false);
            b acD = aVar.acD();
            AnonymousClass1 anonymousClass1 = new WepkgRunCgi.a() {
                public final void a(int i, int i2, String str, b bVar) {
                    AppMethodBeat.i(63668);
                    if (i == 0 && i2 == 0 && bVar.fsH.fsP != null) {
                        try {
                            d.aNS().aa(new 1(this, (si) bVar.fsH.fsP));
                            AppMethodBeat.o(63668);
                            return;
                        } catch (Exception e) {
                            ab.e("MicroMsg.Wepkg.WepkgVersionUpdater", "get checkwepkgversion error");
                            AppMethodBeat.o(63668);
                            return;
                        }
                    }
                    ab.e("MicroMsg.Wepkg.WepkgVersionUpdater", "check wepkg version, cgi failed, errType = %d, errCode = %d, errMsg = %s, rr.resp = %s", new Object[]{Integer.valueOf(i), Integer.valueOf(i2), str, bVar.fsH.fsP});
                    h.pYm.a(859, 16, 1, false);
                    AppMethodBeat.o(63668);
                }
            };
            if (ah.bqo()) {
                w.a(acD, new 1(anonymousClass1));
                AppMethodBeat.o(63676);
                return;
            }
            RemoteCgiTask remoteCgiTask = new RemoteCgiTask();
            d.bB(remoteCgiTask);
            remoteCgiTask.uYe = acD;
            remoteCgiTask.uYf = anonymousClass1;
            remoteCgiTask.uYc = 1;
            WepkgMainProcessService.a(remoteCgiTask);
            AppMethodBeat.o(63676);
        }

        public final void asQ() {
        }

        public final void m(Parcel parcel) {
            AppMethodBeat.i(63677);
            if (this.uYi == null) {
                this.uYi = new ArrayList();
            }
            parcel.readList(this.uYi, WepkgCheckReq.class.getClassLoader());
            this.cTb = parcel.readInt();
            this.uYj = parcel.readByte() != (byte) 0;
            AppMethodBeat.o(63677);
        }

        public final void a(Parcel parcel, int i) {
            AppMethodBeat.i(63678);
            parcel.writeList(this.uYi);
            parcel.writeInt(this.cTb);
            parcel.writeByte((byte) (this.uYj ? 1 : 0));
            AppMethodBeat.o(63678);
        }

        static {
            AppMethodBeat.i(63679);
            AppMethodBeat.o(63679);
        }
    }

    public static void r(List<String> list, int i) {
        AppMethodBeat.i(63680);
        if (bo.ek(list)) {
            AppMethodBeat.o(63680);
            return;
        }
        HashSet hashSet = new HashSet();
        hashSet.addAll(list);
        a(hashSet, i, false);
        AppMethodBeat.o(63680);
    }

    public static void a(Set<String> set, int i, boolean z) {
        AppMethodBeat.i(63681);
        a(set, i, -1, z);
        AppMethodBeat.o(63681);
    }

    public static void a(Set<String> set, int i, int i2, boolean z) {
        AppMethodBeat.i(63682);
        if (set == null) {
            AppMethodBeat.o(63682);
            return;
        }
        LinkedList linkedList = new LinkedList();
        StringBuffer stringBuffer = new StringBuffer();
        StringBuffer stringBuffer2 = new StringBuffer();
        for (String str : set) {
            if (!bo.isNullOrNil(str)) {
                stringBuffer.append(str);
                stringBuffer.append(";");
                ss ssVar = new ss();
                ssVar.mZV = str;
                ssVar.Scene = i;
                WepkgVersion ahW = com.tencent.mm.plugin.wepkg.model.h.ahW(str);
                if (ahW == null) {
                    ssVar.jCh = BuildConfig.FLAVOR;
                    linkedList.add(ssVar);
                } else {
                    ssVar.jCh = ahW.version;
                    if (d.aNY() >= ahW.uXL) {
                        linkedList.add(ssVar);
                        com.tencent.mm.plugin.wepkg.model.h.ahZ(str);
                    } else {
                        stringBuffer2.append(str);
                        stringBuffer2.append(";");
                    }
                }
            }
        }
        ab.i("MicroMsg.Wepkg.WepkgVersionUpdater", "All wepkg list[%s] to update, excluded wepkg list[%s], scene:%d, downloadTriggerType:%d, isReportSize:%b", new Object[]{stringBuffer.toString(), stringBuffer2.toString(), Integer.valueOf(i), Integer.valueOf(i2), Boolean.valueOf(z)});
        if (!bo.ek(linkedList)) {
            b(linkedList, i2, z);
        }
        AppMethodBeat.o(63682);
    }

    private static void b(LinkedList<ss> linkedList, int i, boolean z) {
        AppMethodBeat.i(63683);
        if (linkedList.size() == 0) {
            AppMethodBeat.o(63683);
            return;
        }
        WepkgNetSceneProcessTask wepkgNetSceneProcessTask = new WepkgNetSceneProcessTask();
        wepkgNetSceneProcessTask.cTb = i;
        wepkgNetSceneProcessTask.uYj = z;
        Iterator it = linkedList.iterator();
        while (it.hasNext()) {
            ss ssVar = (ss) it.next();
            WepkgCheckReq wepkgCheckReq = new WepkgCheckReq((byte) 0);
            WepkgCheckReq.a(wepkgCheckReq, ssVar.mZV);
            WepkgCheckReq.b(wepkgCheckReq, ssVar.jCh);
            WepkgCheckReq.a(wepkgCheckReq, ssVar.Scene);
            wepkgNetSceneProcessTask.uYi.add(wepkgCheckReq);
        }
        if (ah.bqo()) {
            d.aNS().aa(new 1(wepkgNetSceneProcessTask));
            AppMethodBeat.o(63683);
            return;
        }
        WepkgMainProcessService.a(wepkgNetSceneProcessTask);
        AppMethodBeat.o(63683);
    }
}
