package com.tencent.mm.plugin.freewifi.e;

import android.net.Uri;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.freewifi.a.a.a;
import com.tencent.mm.plugin.freewifi.e.j.2;
import com.tencent.mm.plugin.freewifi.k.b;
import com.tencent.mm.plugin.freewifi.m;
import com.tencent.mm.plugin.freewifi.ui.FreeWifiFrontPageUI;
import com.tencent.mm.plugin.freewifi.ui.FreeWifiFrontPageUI.d;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.smtt.sdk.TbsMediaPlayer$TbsMediaPlayerListener;
import com.tencent.ytcommon.BuildConfig;
import java.net.HttpURLConnection;
import java.net.UnknownHostException;

class j$2$1 implements a {
    int mxe = 0;
    private final int mxf = 3;
    private int mxg = 0;
    private a mxh = new a() {
        public final void g(HttpURLConnection httpURLConnection) {
            AppMethodBeat.i(20846);
            int responseCode = httpURLConnection.getResponseCode();
            FreeWifiFrontPageUI freeWifiFrontPageUI;
            d dVar;
            FreeWifiFrontPageUI.a aVar;
            if (responseCode == 200) {
                ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.black, desc=it still cannot get authurl and extend (now http returns 200), so it fails to connect wifi. ", new Object[]{m.V(j$2$1.this.mxj.mxd.intent), Integer.valueOf(m.W(j$2$1.this.mxj.mxd.intent))});
                freeWifiFrontPageUI = j$2$1.this.mxj.mxd.mwF;
                dVar = d.mzk;
                aVar = new FreeWifiFrontPageUI.a();
                aVar.myI = m.a(j$2$1.this.mxj.mxd.mwG, b.muI, 35);
                freeWifiFrontPageUI.a(dVar, aVar);
                j.a(j$2$1.this.mxj.mxd, 35, "CANNOT_GET_AUTHURL_AFTER_BLACK_URL");
                AppMethodBeat.o(20846);
            } else if (responseCode == 302) {
                j$2$1.a(j$2$1.this, httpURLConnection.getHeaderField("Location"));
                AppMethodBeat.o(20846);
            } else {
                ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.black, desc=http response status code is neither 200 nor 302, so it fails to connect wifi. ", new Object[]{m.V(j$2$1.this.mxj.mxd.intent), Integer.valueOf(m.W(j$2$1.this.mxj.mxd.intent))});
                freeWifiFrontPageUI = j$2$1.this.mxj.mxd.mwF;
                dVar = d.mzk;
                aVar = new FreeWifiFrontPageUI.a();
                aVar.myI = m.a(j$2$1.this.mxj.mxd.mwG, b.muI, 32);
                freeWifiFrontPageUI.a(dVar, aVar);
                j.a(j$2$1.this.mxj.mxd, 32, "INVALID_HTTP_RESP_CODE");
                AppMethodBeat.o(20846);
            }
        }

        public final void j(Exception exception) {
            AppMethodBeat.i(20847);
            ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.black, desc=exception happens during http, so it fails to connect wifi. e.getMessage()=%s,stacktrace=%s", new Object[]{m.V(j$2$1.this.mxj.mxd.intent), Integer.valueOf(m.W(j$2$1.this.mxj.mxd.intent)), exception.getMessage(), m.h(exception)});
            FreeWifiFrontPageUI freeWifiFrontPageUI = j$2$1.this.mxj.mxd.mwF;
            d dVar = d.mzk;
            FreeWifiFrontPageUI.a aVar = new FreeWifiFrontPageUI.a();
            aVar.myI = m.a(j$2$1.this.mxj.mxd.mwG, b.muI, m.i(exception));
            freeWifiFrontPageUI.a(dVar, aVar);
            j.a(j$2$1.this.mxj.mxd, m.i(exception), m.g(exception));
            AppMethodBeat.o(20847);
        }
    };
    final /* synthetic */ String mxi;
    final /* synthetic */ 2 mxj;

    j$2$1(2 2, String str) {
        this.mxj = 2;
        this.mxi = str;
        AppMethodBeat.i(20848);
        AppMethodBeat.o(20848);
    }

    static /* synthetic */ void a(j$2$1 j_2_1, String str) {
        AppMethodBeat.i(20852);
        j_2_1.MI(str);
        AppMethodBeat.o(20852);
    }

    public final void g(HttpURLConnection httpURLConnection) {
        AppMethodBeat.i(20849);
        int responseCode = httpURLConnection.getResponseCode();
        if (responseCode == 200) {
            ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.handleBlack302, desc=Access to blackurl returns 200 directly, so we believe that the device is already authenticated. Authentication ended.", new Object[]{m.V(this.mxj.mxd.intent), Integer.valueOf(m.W(this.mxj.mxd.intent))});
            j.a(this.mxj.mxd, 0, BuildConfig.FLAVOR);
            m.a(this.mxj.mxd.intent, this.mxj.mxd.cuH, this.mxj.mxd.mwG, this.mxj.mxd.cdf, this.mxj.mxd.mwF, "MicroMsg.FreeWifi.Protocol32");
            AppMethodBeat.o(20849);
        } else if (responseCode == 302) {
            MI(httpURLConnection.getHeaderField("Location"));
            AppMethodBeat.o(20849);
        } else {
            ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.handleBlack302, desc=http response status code is neither 200 nor 302, so it fails to connect wifi. ", new Object[]{m.V(this.mxj.mxd.intent), Integer.valueOf(m.W(this.mxj.mxd.intent))});
            FreeWifiFrontPageUI freeWifiFrontPageUI = this.mxj.mxd.mwF;
            d dVar = d.mzk;
            FreeWifiFrontPageUI.a aVar = new FreeWifiFrontPageUI.a();
            aVar.myI = m.a(this.mxj.mxd.mwG, b.muI, 32);
            freeWifiFrontPageUI.a(dVar, aVar);
            j.b(this.mxj.mxd, 32, "INVALID_HTTP_RESP_CODE");
            AppMethodBeat.o(20849);
        }
    }

    public final void j(Exception exception) {
        FreeWifiFrontPageUI freeWifiFrontPageUI;
        d dVar;
        FreeWifiFrontPageUI.a aVar;
        AppMethodBeat.i(20850);
        ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.handleBlack302, desc=exception happens during http, so it fails to connect wifi. e.getMessage()=%s, stacktrace=%s", new Object[]{m.V(this.mxj.mxd.intent), Integer.valueOf(m.W(this.mxj.mxd.intent)), exception.getMessage(), m.h(exception)});
        if (exception instanceof UnknownHostException) {
            j.b(this.mxj.mxd, TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_SUBTITLE_INDEX, m.g(exception));
            ab.i("MicroMsg.FreeWifi.Protocol32", "countBlackHttpRequest=" + this.mxe);
            int i = this.mxe + 1;
            this.mxe = i;
            if (i <= 3) {
                try {
                    Thread.sleep(3000);
                    com.tencent.mm.plugin.freewifi.a.a.byw();
                    com.tencent.mm.plugin.freewifi.a.a.a(this.mxi, this);
                    AppMethodBeat.o(20850);
                    return;
                } catch (InterruptedException e) {
                    ab.e("MicroMsg.FreeWifi.Protocol32", "InterruptedException e stacktrace=%s", new Object[]{m.h(exception)});
                    j.b(this.mxj.mxd, TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_AUDIOTRACK_TITLES, m.g(exception));
                    freeWifiFrontPageUI = this.mxj.mxd.mwF;
                    dVar = d.mzk;
                    aVar = new FreeWifiFrontPageUI.a();
                    aVar.myI = m.a(this.mxj.mxd.mwG, b.muI, TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_AUDIOTRACK_TITLES);
                    freeWifiFrontPageUI.a(dVar, aVar);
                    AppMethodBeat.o(20850);
                    return;
                }
            }
            freeWifiFrontPageUI = this.mxj.mxd.mwF;
            dVar = d.mzk;
            aVar = new FreeWifiFrontPageUI.a();
            aVar.myI = m.a(this.mxj.mxd.mwG, b.muI, m.i(exception));
            freeWifiFrontPageUI.a(dVar, aVar);
            j.b(this.mxj.mxd, m.i(exception), m.g(exception));
            AppMethodBeat.o(20850);
            return;
        }
        freeWifiFrontPageUI = this.mxj.mxd.mwF;
        dVar = d.mzk;
        aVar = new FreeWifiFrontPageUI.a();
        aVar.myI = m.a(this.mxj.mxd.mwG, b.muI, m.i(exception));
        freeWifiFrontPageUI.a(dVar, aVar);
        j.b(this.mxj.mxd, m.i(exception), m.g(exception));
        AppMethodBeat.o(20850);
    }

    private void MI(String str) {
        AppMethodBeat.i(20851);
        Uri parse = Uri.parse(str);
        String queryParameter = parse.getQueryParameter("authUrl");
        String Mw = m.Mw(parse.getQueryParameter("extend"));
        ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.handleBlack302, desc=Access to blackUrl returns 302 and now trying to  get authurl and extend from location. location=%s, authUrl=%s, extend=%s", new Object[]{m.V(this.mxj.mxd.intent), Integer.valueOf(m.W(this.mxj.mxd.intent)), str, queryParameter, Mw});
        if (m.isEmpty(queryParameter)) {
            this.mxg++;
            if (this.mxg < 3) {
                com.tencent.mm.plugin.freewifi.a.a.byw();
                com.tencent.mm.plugin.freewifi.a.a.a(str, this.mxh);
                AppMethodBeat.o(20851);
                return;
            }
            ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, method=Protocol32UI.handleBlack302, desc=Connection fail. Too many 302, exceeding 3 times", new Object[]{m.V(this.mxj.mxd.intent), Integer.valueOf(m.W(this.mxj.mxd.intent))});
            FreeWifiFrontPageUI freeWifiFrontPageUI = this.mxj.mxd.mwF;
            d dVar = d.mzk;
            FreeWifiFrontPageUI.a aVar = new FreeWifiFrontPageUI.a();
            aVar.myI = m.a(this.mxj.mxd.mwG, b.muI, 36);
            freeWifiFrontPageUI.a(dVar, aVar);
            j.a(this.mxj.mxd, 36, "BLACK_302_TIMES_EXCESS");
            AppMethodBeat.o(20851);
            return;
        }
        StringBuilder stringBuilder = new StringBuilder(queryParameter);
        if (queryParameter.indexOf("?") != -1) {
            stringBuilder.append("&extend=").append(Mw);
        } else {
            stringBuilder.append("?extend=").append(Mw);
        }
        stringBuilder.append("&openId=").append(m.Mw(this.mxj.mxd.openId)).append("&tid=").append(m.Mw(this.mxj.mxd.mxc)).append("&timestamp=").append(this.mxj.mxd.cvO).append("&sign=").append(this.mxj.mxd.sign);
        j.a(this.mxj.mxd, stringBuilder.toString());
        ab.i("MicroMsg.FreeWifi.Protocol32", "sessionKey=%s, step=%d, desc=Data retrieved. http authentication url = %s", new Object[]{m.V(this.mxj.mxd.intent), Integer.valueOf(m.W(this.mxj.mxd.intent)), stringBuilder.toString()});
        AppMethodBeat.o(20851);
    }
}
