package com.facebook.login;

import X.C4115Lek;
import X.C4116Lel;
import X.C5478M1t;
import X.C5480M1v;
import X.C5555M4s;
import X.C5568M5f;
import X.C5581M5s;
import X.C5585M5w;
import X.M12;
import X.M44;
import X.M5M;
import X.M5R;
import X.M5W;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.facebook.AccessToken;
import com.facebook.login.LoginClient;
import com.facebook.s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import kotlin.h.b.n;

public final class GetTokenLoginMethodHandler extends LoginMethodHandler {
    public static final Parcelable.Creator<GetTokenLoginMethodHandler> CREATOR = new M5R();
    public static final C5581M5s LIZ = new C5581M5s((byte) 0);
    public M5W LIZIZ;
    public final String LJFF = "get_token";

    public final int describeContents() {
        return 0;
    }

    public final String LIZ() {
        return this.LJFF;
    }

    public final void P_() {
        M5W m5w = this.LIZIZ;
        if (m5w != null) {
            m5w.LIZJ = false;
            m5w.LIZIZ = null;
            this.LIZIZ = null;
        }
    }

    static {
        Covode.recordClassIndex(41922);
    }

    /* JADX INFO: super call moved to the top of the method (can break code semantics) */
    public GetTokenLoginMethodHandler(Parcel parcel) {
        super(parcel);
        M12.LIZ((Object) parcel);
    }

    /* JADX INFO: super call moved to the top of the method (can break code semantics) */
    public GetTokenLoginMethodHandler(LoginClient loginClient) {
        super(loginClient);
        M12.LIZ((Object) loginClient);
    }

    public final int LIZ(LoginClient.Request request) {
        M12.LIZ((Object) request);
        Context LIZ2 = LJFF().LIZ();
        if (LIZ2 == null) {
            LIZ2 = s.LJFF();
        }
        M5W m5w = new M5W(LIZ2, request);
        this.LIZIZ = m5w;
        if (n.LIZ(Boolean.valueOf(m5w.LIZ()), false)) {
            return 0;
        }
        LJFF().LJ();
        C5568M5f m5f = new C5568M5f(this, request);
        M5W m5w2 = this.LIZIZ;
        if (m5w2 == null) {
            return 1;
        }
        m5w2.LIZIZ = m5f;
        return 1;
    }

    public final void LIZ(LoginClient.Request request, Bundle bundle) {
        LoginClient.Result result;
        AccessToken accessToken;
        LoginClient.Request request2 = request;
        Bundle bundle2 = bundle;
        M12.LIZ((Object) request2, (Object) bundle2);
        try {
            M44 m44 = M44.FACEBOOK_APPLICATION_SERVICE;
            String str = request2.LJ;
            M12.LIZ((Object) bundle2, (Object) str);
            Date LIZ2 = C5478M1t.LIZ(bundle2, "com.facebook.platform.extra.EXPIRES_SECONDS_SINCE_EPOCH", new Date(0));
            ArrayList<String> stringArrayList = bundle2.getStringArrayList("com.facebook.platform.extra.PERMISSIONS");
            String string = bundle2.getString("com.facebook.platform.extra.ACCESS_TOKEN");
            Date LIZ3 = C5478M1t.LIZ(bundle2, "com.facebook.platform.extra.EXTRA_DATA_ACCESS_EXPIRATION_TIME", new Date(0));
            if (string != null) {
                if (string.length() != 0) {
                    String string2 = bundle2.getString("com.facebook.platform.extra.USER_ID");
                    if (!(string2 == null || string2.length() == 0)) {
                        accessToken = new AccessToken(string, str, string2, stringArrayList, (Collection<String>) null, (Collection<String>) null, m44, LIZ2, new Date(), LIZ3, bundle2.getString("graph_domain"));
                        result = LoginClient.Result.LIZ.LIZ(request2, accessToken, LoginMethodHandler.LIZJ.LIZ(bundle2, request2.LJIILL));
                        LJFF().LIZ(result);
                    }
                }
            }
            accessToken = null;
            result = LoginClient.Result.LIZ.LIZ(request2, accessToken, LoginMethodHandler.LIZJ.LIZ(bundle2, request2.LJIILL));
        } catch (M5M e) {
            result = LoginClient.Result.LIZ.LIZ(LJFF().LJII, (String) null, e.getMessage(), (String) null);
        }
        LJFF().LIZ(result);
    }

    public static final /* synthetic */ void LIZ(GetTokenLoginMethodHandler getTokenLoginMethodHandler, LoginClient.Request request, Bundle bundle) {
        M12.LIZ((Object) getTokenLoginMethodHandler, (Object) request);
        M12.LIZ((Object) request);
        M5W m5w = getTokenLoginMethodHandler.LIZIZ;
        if (m5w != null) {
            m5w.LIZIZ = null;
        }
        getTokenLoginMethodHandler.LIZIZ = null;
        C5585M5w m5w2 = getTokenLoginMethodHandler.LJFF().LJFF;
        if (m5w2 != null) {
            m5w2.LIZIZ();
        }
        if (bundle != null) {
            List stringArrayList = bundle.getStringArrayList("com.facebook.platform.extra.PERMISSIONS");
            if (stringArrayList == null) {
                stringArrayList = C4116Lel.INSTANCE;
            }
            Set set = request.LIZJ;
            if (set == null) {
                set = C4115Lek.INSTANCE;
            }
            String string = bundle.getString("com.facebook.platform.extra.ID_TOKEN");
            if (!set.contains("openid") || !(string == null || string.length() == 0)) {
                if (stringArrayList.containsAll(set)) {
                    M12.LIZ((Object) request, (Object) bundle);
                    String string2 = bundle.getString("com.facebook.platform.extra.USER_ID");
                    if (string2 == null || string2.length() == 0) {
                        getTokenLoginMethodHandler.LJFF().LJ();
                        String string3 = bundle.getString("com.facebook.platform.extra.ACCESS_TOKEN");
                        if (string3 != null) {
                            C5478M1t.LIZ(string3, (C5480M1v) new C5555M4s(bundle, getTokenLoginMethodHandler, request));
                            return;
                        }
                        throw new IllegalStateException("Required value was null.".toString());
                    }
                    getTokenLoginMethodHandler.LIZ(request, bundle);
                    return;
                }
                HashSet hashSet = new HashSet();
                for (Object next : set) {
                    if (!stringArrayList.contains(next)) {
                        hashSet.add(next);
                    }
                }
                if (!hashSet.isEmpty()) {
                    getTokenLoginMethodHandler.LIZ("new_permissions", TextUtils.join(",", hashSet));
                }
                M12.LIZ((Object) hashSet);
                request.LIZJ = hashSet;
            }
        }
        getTokenLoginMethodHandler.LJFF().LIZLLL();
    }
}
