package com.kakao.auth.ageauth;

import X.3i8;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.ResultReceiver;
import com.bytedance.covode.number.Covode;
import com.kakao.auth.AuthService;
import com.kakao.auth.KakaoSDK;
import com.kakao.auth.authorization.authcode.KakaoWebViewActivity;
import com.kakao.network.ServerProtocol;
import com.kakao.util.exception.KakaoException;
import com.kakao.util.helper.Utility;
import com.kakao.util.helper.log.Logger;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class DefaultAgeAuthService implements AgeAuthService {
    public static DefaultAgeAuthService instance;
    public Handler sHandler;

    static {
        Covode.recordClassIndex(45774);
    }

    public static class AgeAuthResult {
        public KakaoException exception;
        public AtomicInteger result = new AtomicInteger();

        static {
            Covode.recordClassIndex(45777);
        }

        public KakaoException getException() {
            return this.exception;
        }

        public AtomicInteger getResult() {
            return this.result;
        }

        public void setException(KakaoException kakaoException) {
            this.exception = kakaoException;
        }

        public void setResult(AtomicInteger atomicInteger) {
            this.result = atomicInteger;
        }
    }

    public static DefaultAgeAuthService getInstance() {
        if (instance == null) {
            instance = new DefaultAgeAuthService(new Handler(Looper.getMainLooper()));
        }
        return instance;
    }

    public DefaultAgeAuthService(Handler handler) {
        this.sHandler = handler;
    }

    public static void INVOKEVIRTUAL_com_kakao_auth_ageauth_DefaultAgeAuthService_com_ss_android_ugc_tiktok_security_lancet_ContextLancet_startActivity(Context context, Intent intent) {
        3i8.LIZ(intent, context);
        context.startActivity(intent);
    }

    public int requestAgeAuth(Bundle bundle, Context context) {
        final AgeAuthResult ageAuthResult = new AgeAuthResult();
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final Bundle bundle2 = bundle;
        final Context context2 = context;
        this.sHandler.post(new Runnable() {
            static {
                Covode.recordClassIndex(45775);
            }

            public void run() {
                try {
                    DefaultAgeAuthService.this.requestWebviewAuth(context2, bundle2, new ResultReceiver(DefaultAgeAuthService.this.sHandler) {
                        static {
                            Covode.recordClassIndex(45776);
                        }

                        public void onReceiveResult(int i, Bundle bundle) {
                            int value = AuthService.AgeAuthStatus.CLIENT_ERROR.getValue();
                            if (i == 0) {
                                String string = bundle.getString("key.redirect.url");
                                if (!(string == null || Uri.parse(string).getQueryParameter("status") == null)) {
                                    value = Integer.valueOf(Uri.parse(string).getQueryParameter("status")).intValue();
                                }
                            } else if (i == 1) {
                                ageAuthResult.setException(bundle.getSerializable("key.exception"));
                            }
                            ageAuthResult.getResult().set(value);
                            countDownLatch.countDown();
                        }
                    });
                } catch (Exception e) {
                    ageAuthResult.getResult().set(AuthService.AgeAuthStatus.CLIENT_ERROR.getValue());
                    ageAuthResult.setException(new KakaoException(e));
                    countDownLatch.countDown();
                }
            }
        });
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            Logger.e(e.toString());
        }
        if (ageAuthResult.getException() == null) {
            return ageAuthResult.getResult().get();
        }
        throw ageAuthResult.getException();
    }

    public boolean requestWebviewAuth(Context context, Bundle bundle, ResultReceiver resultReceiver) {
        boolean isUsingWebviewTimer = KakaoSDK.getAdapter().getSessionConfig().isUsingWebviewTimer();
        Uri buildUri = Utility.buildUri(ServerProtocol.accountAuthority(), "ageauths/main.html", bundle);
        Logger.d("AgeAuth request Url : ".concat(String.valueOf(buildUri)));
        Intent newIntent = KakaoWebViewActivity.newIntent(context);
        newIntent.putExtra("key.url", buildUri.toString());
        newIntent.putExtra("key.use.webview.timers", isUsingWebviewTimer);
        newIntent.putExtra("key.result.receiver", resultReceiver);
        INVOKEVIRTUAL_com_kakao_auth_ageauth_DefaultAgeAuthService_com_ss_android_ugc_tiktok_security_lancet_ContextLancet_startActivity(context, newIntent);
        return true;
    }
}
