package com.tencent.mm.plugin.appbrand.jsapi.auth;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.kernel.g;
import com.tencent.mm.plugin.appbrand.ipc.AppBrandMainProcessService;
import com.tencent.mm.plugin.appbrand.ipc.MainProcessTask;
import com.tencent.mm.plugin.appbrand.jsapi.h;
import com.tencent.mm.plugin.appbrand.page.u;
import com.tencent.mm.plugin.appbrand.q;
import com.tencent.mm.plugin.appbrand.report.AppBrandStatObject;
import com.tencent.mm.protocal.protobuf.buj;
import com.tencent.mm.protocal.protobuf.cxh;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.al;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.z.a.a;
import com.tencent.mm.z.a.b;
import com.tencent.ytcommon.BuildConfig;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import org.json.JSONArray;
import org.json.JSONObject;

public final class JsApiAuthorize extends f {
    private static final int CTRL_INDEX = 54;
    private static final String NAME = "authorize";

    static class AuthorizeTask extends MainProcessTask {
        public static final Creator<AuthorizeTask> CREATOR = new 3();
        public String aIm;
        public String appId;
        public String data;
        public int errCode;
        f hBQ;
        h hBR;
        d hBS;
        public ArrayList<String> hBT;
        public String hBU;
        public String hBV;
        public int hBW;
        public String hBX;
        public String hBY;
        public String hBZ;
        public String hCa;
        public String hCb;
        public String hCc;
        public int hCd;
        public Bundle hCe;
        int hCf;
        public int har;
        public int hwi;
        private int hyE;
        public String mAppName;

        static /* synthetic */ boolean a(AuthorizeTask authorizeTask) {
            AppMethodBeat.i(130844);
            boolean aCb = authorizeTask.aCb();
            AppMethodBeat.o(130844);
            return aCb;
        }

        static /* synthetic */ boolean b(AuthorizeTask authorizeTask) {
            AppMethodBeat.i(130845);
            boolean aCb = authorizeTask.aCb();
            AppMethodBeat.o(130845);
            return aCb;
        }

        static /* synthetic */ boolean c(AuthorizeTask authorizeTask) {
            AppMethodBeat.i(130846);
            boolean aCb = authorizeTask.aCb();
            AppMethodBeat.o(130846);
            return aCb;
        }

        static /* synthetic */ boolean d(AuthorizeTask authorizeTask) {
            AppMethodBeat.i(130847);
            boolean aCb = authorizeTask.aCb();
            AppMethodBeat.o(130847);
            return aCb;
        }

        static /* synthetic */ boolean e(AuthorizeTask authorizeTask) {
            AppMethodBeat.i(130848);
            boolean aCb = authorizeTask.aCb();
            AppMethodBeat.o(130848);
            return aCb;
        }

        public AuthorizeTask(Parcel parcel) {
            AppMethodBeat.i(130838);
            g(parcel);
            AppMethodBeat.o(130838);
        }

        public final void asP() {
            AppMethodBeat.i(130839);
            1 1 = new 1(this);
            if (this.hBU.equals(JsApiAuthorize.NAME)) {
                try {
                    JSONArray jSONArray = new JSONArray(new JSONObject(this.data).optString("scope"));
                    LinkedList linkedList = new LinkedList();
                    for (int i = 0; i < jSONArray.length(); i++) {
                        linkedList.add(jSONArray.optString(i));
                    }
                    a aVar = new a(this.appId, linkedList, this.har, this.hyE, new 4(this, 1));
                    if (aVar.Qq().wzG == null) {
                        aVar.Qq().wzG = new cxh();
                    }
                    aVar.Qq().wzG.xsJ = this.hCf;
                    g.Rg().a(aVar, 0);
                    AppMethodBeat.o(130839);
                    return;
                } catch (Exception e) {
                    ab.e("MicroMsg.JsApiAuthorize", "Exception %s", new Object[]{e.getMessage()});
                    this.hBV = "fail";
                    aCb();
                    AppMethodBeat.o(130839);
                    return;
                }
            }
            if (this.hBU.equals("authorizeConfirm")) {
                String str = this.appId;
                ArrayList arrayList = this.hBT;
                int i2 = this.har;
                int i3 = this.hBW;
                b bVar = new b(str, f.k(arrayList), i2, i3, this.hyE, new 5(this, 1, i3));
                if (bVar.Qs().wzG == null) {
                    bVar.Qs().wzG = new cxh();
                }
                bVar.Qs().wzG.xsJ = this.hCf;
                g.Rg().a(bVar, 0);
            }
            AppMethodBeat.o(130839);
        }

        public final void asQ() {
            AppMethodBeat.i(130840);
            aBW();
            if (!this.hBR.isRunning()) {
                this.hBS.aCB();
                AppMethodBeat.o(130840);
            } else if (this.hBV.equals("ok")) {
                this.hBQ.b(this.hBR, this.hwi, "ok");
                this.hBS.aCB();
                AppMethodBeat.o(130840);
            } else if (this.hBV.equals("fail")) {
                String format;
                if (bo.isNullOrNil(this.aIm)) {
                    format = String.format("fail:login error %s", new Object[]{Integer.valueOf(this.errCode)});
                } else {
                    format = String.format("fail:%s", new Object[]{this.aIm});
                }
                this.hBQ.b(this.hBR, this.hwi, format);
                this.hBS.aCB();
                AppMethodBeat.o(130840);
            } else {
                if (this.hBV.equals("needConfirm")) {
                    LinkedList linkedList = new LinkedList();
                    int i = 0;
                    while (i < this.hCd) {
                        byte[] byteArray = this.hCe.getByteArray(String.valueOf(i));
                        buj buj = new buj();
                        try {
                            buj.parseFrom(byteArray);
                            linkedList.add(buj);
                            i++;
                        } catch (IOException e) {
                            ab.e("MicroMsg.JsApiAuthorize", "IOException %s", new Object[]{e.getMessage()});
                            ab.printErrStackTrace("MicroMsg.JsApiAuthorize", e, BuildConfig.FLAVOR, new Object[0]);
                            this.hBQ.b(this.hBR, this.hwi, "fail:internal error scope error");
                            this.hBS.aCB();
                            AppMethodBeat.o(130840);
                            return;
                        }
                    }
                    if (linkedList.size() > 0) {
                        al.d(new 2(this, linkedList));
                        AppMethodBeat.o(130840);
                        return;
                    }
                    this.hBQ.b(this.hBR, this.hwi, "fail:internal error scope empty");
                    this.hBS.aCB();
                }
                AppMethodBeat.o(130840);
            }
        }

        private void AH(String str) {
            AppMethodBeat.i(130841);
            if (TextUtils.isEmpty(str)) {
                this.hBQ.b(this.hBR, this.hwi, "fail auth cancel");
            } else {
                this.hBQ.b(this.hBR, this.hwi, str);
            }
            this.hBS.aCB();
            AppMethodBeat.o(130841);
        }

        public final void g(Parcel parcel) {
            AppMethodBeat.i(130842);
            this.data = parcel.readString();
            this.hwi = parcel.readInt();
            this.appId = parcel.readString();
            this.hBV = parcel.readString();
            this.hBU = parcel.readString();
            this.mAppName = parcel.readString();
            this.hBX = parcel.readString();
            this.hCd = parcel.readInt();
            this.hCe = parcel.readBundle(JsApiAuthorize.class.getClassLoader());
            this.hBT = parcel.createStringArrayList();
            this.har = parcel.readInt();
            this.hBW = parcel.readInt();
            this.hyE = parcel.readInt();
            this.hCf = parcel.readInt();
            this.errCode = parcel.readInt();
            this.aIm = parcel.readString();
            this.hCa = parcel.readString();
            this.hBZ = parcel.readString();
            this.hBY = parcel.readString();
            this.hCb = parcel.readString();
            this.hCc = parcel.readString();
            AppMethodBeat.o(130842);
        }

        public void writeToParcel(Parcel parcel, int i) {
            AppMethodBeat.i(130843);
            parcel.writeString(this.data);
            parcel.writeInt(this.hwi);
            parcel.writeString(this.appId);
            parcel.writeString(this.hBV);
            parcel.writeString(this.hBU);
            parcel.writeString(this.mAppName);
            parcel.writeString(this.hBX);
            parcel.writeInt(this.hCd);
            parcel.writeBundle(this.hCe);
            parcel.writeStringList(this.hBT);
            parcel.writeInt(this.har);
            parcel.writeInt(this.hBW);
            parcel.writeInt(this.hyE);
            parcel.writeInt(this.hCf);
            parcel.writeInt(this.errCode);
            parcel.writeString(this.aIm);
            parcel.writeString(this.hCa);
            parcel.writeString(this.hBZ);
            parcel.writeString(this.hBY);
            parcel.writeString(this.hCb);
            parcel.writeString(this.hCc);
            AppMethodBeat.o(130843);
        }

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

        static /* synthetic */ void a(AuthorizeTask authorizeTask, int i, ArrayList arrayList) {
            AppMethodBeat.i(130849);
            authorizeTask.hBU = "authorizeConfirm";
            authorizeTask.hBT = arrayList;
            authorizeTask.hBW = i;
            AppBrandMainProcessService.a(authorizeTask);
            if (i == 2) {
                authorizeTask.hBQ.b(authorizeTask.hBR, authorizeTask.hwi, "fail auth deny");
                authorizeTask.hBS.aCB();
            }
            AppMethodBeat.o(130849);
        }

        static /* synthetic */ void f(AuthorizeTask authorizeTask) {
            AppMethodBeat.i(130850);
            authorizeTask.AH(BuildConfig.FLAVOR);
            AppMethodBeat.o(130850);
        }
    }

    public final void a(h hVar, JSONObject jSONObject, int i, d dVar) {
        AppMethodBeat.i(130853);
        ab.d("MicroMsg.JsApiAuthorize", "stev JsApiAuthorize!");
        AuthorizeTask authorizeTask = new AuthorizeTask();
        authorizeTask.appId = hVar.getAppId();
        authorizeTask.hBU = NAME;
        com.tencent.mm.plugin.appbrand.config.h ye = hVar.getRuntime().ye();
        if (ye != null) {
            authorizeTask.har = ye.hhd.gVt;
        }
        AppBrandStatObject wK = com.tencent.mm.plugin.appbrand.a.wK(hVar.getAppId());
        if (wK != null) {
            authorizeTask.hyE = wK.scene;
        }
        String jSONObject2 = jSONObject.toString();
        authorizeTask.hBQ = this;
        authorizeTask.hBR = hVar;
        authorizeTask.data = jSONObject2;
        authorizeTask.hwi = i;
        authorizeTask.hBS = dVar;
        authorizeTask.hCe = new Bundle();
        if (hVar instanceof q) {
            authorizeTask.hCf = 1;
        } else if (hVar instanceof u) {
            authorizeTask.hCf = 2;
        }
        authorizeTask.aBV();
        AppBrandMainProcessService.a(authorizeTask);
        AppMethodBeat.o(130853);
    }
}
