package com.linecorp.linesdk.auth.internal;

import X.8gF;
import X.C5690M9x;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.sysoptimizer.EnterTransitionCrashOptimizer;
import com.linecorp.linesdk.LineApiError;
import com.linecorp.linesdk.auth.LineAuthenticationConfig;
import com.linecorp.linesdk.auth.LineLoginResult;
import com.linecorp.linesdk.auth.internal.a;
import com.linecorp.linesdk.auth.internal.c;
import com.linecorp.linesdk.auth.internal.d;

public class LineAuthenticationActivity extends Activity {
    public boolean LIZ;
    public d LIZIZ;
    public c LIZJ;

    static {
        Covode.recordClassIndex(46046);
    }

    public void onDestroy() {
        8gF.LJ(this);
        super.onDestroy();
    }

    public void onPause() {
        8gF.LIZJ(this);
        super.onPause();
    }

    public void onStart() {
        8gF.LIZ(this);
        super.onStart();
    }

    public void onStop() {
        8gF.LIZLLL(this);
        super.onStop();
        this.LIZ = true;
        if (EnterTransitionCrashOptimizer.getContext() != null) {
            try {
                getWindow().getDecorView().getViewTreeObserver().dispatchOnPreDraw();
            } catch (Throwable unused) {
            }
        }
        try {
            getWindow().getDecorView().getViewTreeObserver().dispatchOnPreDraw();
        } catch (Throwable unused2) {
        }
    }

    public void onResume() {
        8gF.LIZIZ(this);
        super.onResume();
        if (this.LIZIZ.LIZLLL == d.a.LIZ) {
            c cVar = this.LIZJ;
            cVar.LJII.LIZLLL = d.a.LIZIZ;
            new c.C0003c(cVar, (byte) 0).execute(new Void[0]);
        } else if (this.LIZIZ.LIZLLL != d.a.LIZJ) {
            new Handler(Looper.getMainLooper()).postDelayed(new c.b(this.LIZJ, (byte) 0), 1000);
        }
        this.LIZ = false;
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putParcelable("authenticationStatus", this.LIZIZ);
    }

    public final void LIZ(LineLoginResult lineLoginResult) {
        d dVar = this.LIZIZ;
        if (dVar == null) {
            finish();
        } else if ((dVar.LIZLLL == d.a.LIZIZ && !this.LIZ) || this.LIZIZ.LIZLLL == d.a.LIZLLL) {
            Intent intent = new Intent();
            intent.putExtra("authentication_result", lineLoginResult);
            setResult(-1, intent);
            finish();
        }
    }

    public void onCreate(Bundle bundle) {
        d dVar;
        8gF.LIZ(this, bundle);
        super.onCreate(bundle);
        setContentView(2131560541);
        Intent intent = getIntent();
        LineAuthenticationConfig lineAuthenticationConfig = (LineAuthenticationConfig) intent.getParcelableExtra("authentication_config");
        if (lineAuthenticationConfig == null) {
            LIZ(new LineLoginResult(C5690M9x.INTERNAL_ERROR, new LineApiError("The requested parameter is illegal.")));
            return;
        }
        if (bundle == null) {
            dVar = new d();
        } else {
            dVar = (d) bundle.getParcelable("authenticationStatus");
            if (dVar == null) {
                dVar = new d();
            }
        }
        this.LIZIZ = dVar;
        this.LIZJ = new c(this, lineAuthenticationConfig, this.LIZIZ, intent.getStringArrayExtra("permissions"));
    }

    public void onNewIntent(Intent intent) {
        a.c LIZ2;
        C5690M9x m9x;
        super.onNewIntent(intent);
        if (this.LIZIZ.LIZLLL == d.a.LIZIZ) {
            c cVar = this.LIZJ;
            cVar.LJII.LIZLLL = d.a.LIZJ;
            a aVar = cVar.LJ;
            Uri data = intent.getData();
            if (data == null) {
                LIZ2 = a.c.LIZ("Illegal redirection from external application.");
            } else {
                String str = aVar.LIZIZ.LIZJ;
                String queryParameter = data.getQueryParameter("state");
                if (str == null || !str.equals(queryParameter)) {
                    LIZ2 = a.c.LIZ("Illegal parameter value of 'state'.");
                } else {
                    String queryParameter2 = data.getQueryParameter("code");
                    if (!TextUtils.isEmpty(queryParameter2)) {
                        LIZ2 = new a.c(queryParameter2, (String) null, (String) null, (String) null);
                    } else {
                        LIZ2 = new a.c((String) null, data.getQueryParameter("error"), data.getQueryParameter("error_description"), (String) null);
                    }
                }
            }
            if (!LIZ2.LIZ()) {
                cVar.LJII.LIZLLL = d.a.LIZLLL;
                LineAuthenticationActivity lineAuthenticationActivity = cVar.LIZ;
                if (LIZ2.LIZIZ()) {
                    m9x = C5690M9x.AUTHENTICATION_AGENT_ERROR;
                } else {
                    m9x = C5690M9x.INTERNAL_ERROR;
                }
                lineAuthenticationActivity.LIZ(new LineLoginResult(m9x, LIZ2.LIZJ()));
                return;
            }
            c.a aVar2 = new c.a(cVar, (byte) 0);
            String[] strArr = new String[1];
            if (!TextUtils.isEmpty(LIZ2.LIZ)) {
                strArr[0] = LIZ2.LIZ;
                aVar2.execute(strArr);
                return;
            }
            throw new UnsupportedOperationException("requestToken is null. Please check result by isSuccess before.");
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (this.LIZIZ.LIZLLL == d.a.LIZIZ) {
            c cVar = this.LIZJ;
            if (i == 3 && cVar.LJII.LIZLLL != d.a.LIZJ) {
                new Handler(Looper.getMainLooper()).postDelayed(new c.b(cVar, (byte) 0), 1000);
            }
        }
    }
}
