package com.ss.android.ugc.aweme.services;

import X.7he;
import X.7xA;
import X.AnonymousClass2HN;
import X.AnonymousClass46T;
import X.C12422nK;
import X.Crj;
import X.JtF;
import X.Lnj;
import X.S93;
import X.S9B;
import android.os.Bundle;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.keva.Keva;
import com.ss.android.common.applog.AppLog;
import com.ss.android.ugc.aweme.AccountService;
import com.ss.android.ugc.aweme.IAccountService;
import com.ss.android.ugc.aweme.IAccountUserService;
import com.ss.android.ugc.aweme.IMandatoryLoginService;
import com.ss.android.ugc.aweme.account.experiment.service.NonPersonalizationService;
import com.ss.android.ugc.aweme.account.guestmode.GuestModeServiceImpl;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import kotlin.h.b.n;

public final class MandatoryLoginService implements IMandatoryLoginService {
    public static final Companion Companion = new Companion((AnonymousClass2HN) null);
    public static final Crj unregisteredExperienceListener$delegate = 7he.LIZ(MandatoryLoginService$Companion$unregisteredExperienceListener$2.INSTANCE);
    public boolean hasRequestComplianceApi;
    public final Keva keva;
    public boolean loggedInitialMandatoryLoginDecision;
    public boolean skipLoginForTest;

    public final boolean disableForTest() {
        return false;
    }

    public final void skipLoginForTest() {
        this.skipLoginForTest = true;
    }

    public static final class Companion {
        static {
            Covode.recordClassIndex(105127);
        }

        public final AnonymousClass46T getUnregisteredExperienceListener() {
            return (AnonymousClass46T) MandatoryLoginService.unregisteredExperienceListener$delegate.getValue();
        }

        public Companion() {
        }

        public /* synthetic */ Companion(AnonymousClass2HN r1) {
            this();
        }
    }

    public final boolean getHasRequestComplianceApi() {
        return this.hasRequestComplianceApi;
    }

    static {
        Covode.recordClassIndex(105126);
    }

    public MandatoryLoginService() {
        Keva repo = Keva.getRepo("mandatory_login_repo");
        n.LIZIZ(repo, "");
        this.keva = repo;
    }

    public final Bundle getLoginActivityBundle() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("is_fullscreen_dialog", true);
        bundle.putString("enter_from", "cold_launch");
        bundle.putString("enter_method", "reopen");
        bundle.putBoolean("is_skippable_dialog", !enableForcedLogin());
        return bundle;
    }

    public final void setupUnregisteredModeListener() {
        if (NonPersonalizationService.LJIIJ().LIZ()) {
            AppLog.setDisablePersonalization(NonPersonalizationService.LJIIJ().LIZLLL() ? 1 : 0);
            AccountService.LIZ().LJFF().addUserChangeListener(Companion.getUnregisteredExperienceListener());
        }
    }

    public final boolean shouldShowForcedLogin() {
        if (disableForTest()) {
            return false;
        }
        IAccountUserService LJFF = AccountService.LIZ().LJFF();
        n.LIZIZ(LJFF, "");
        if (LJFF.isLogin() || !enableForcedLogin()) {
            return false;
        }
        return true;
    }

    public final boolean shouldShowLoginTabFirst() {
        IAccountService LIZ = AccountService.LIZ();
        n.LIZIZ(LIZ, "");
        if (!n.LIZ("US", LIZ.LIZJ())) {
            return false;
        }
        IAccountUserService LJFF = AccountService.LIZ().LJFF();
        n.LIZIZ(LJFF, "");
        if (!LJFF.isLogin()) {
            return true;
        }
        return false;
    }

    public final boolean enableForcedLogin() {
        String string;
        String string2;
        boolean z;
        String LIZ = S9B.LIZ();
        n.LIZIZ(LIZ, "");
        Locale locale = Locale.ROOT;
        n.LIZIZ(locale, "");
        Objects.requireNonNull(LIZ, "null cannot be cast to non-null type java.lang.String");
        String upperCase = LIZ.toUpperCase(locale);
        n.LIZIZ(upperCase, "");
        IAccountService LIZ2 = AccountService.LIZ();
        n.LIZIZ(LIZ2, "");
        String LIZJ = LIZ2.LIZJ();
        if (!this.keva.contains("first_ever_priority_region")) {
            this.keva.storeString("first_ever_priority_region", upperCase);
            string = upperCase;
        } else {
            string = this.keva.getString("first_ever_priority_region", "");
            n.LIZIZ(string, "");
        }
        if (!this.keva.contains("first_ever_store_region")) {
            n.LIZIZ(LIZJ, "");
            this.keva.storeString("first_ever_store_region", LIZJ);
            string2 = LIZJ;
        } else {
            string2 = this.keva.getString("first_ever_store_region", "");
            n.LIZIZ(string2, "");
        }
        if (7xA.LIZIZ().booleanValue() || this.keva.contains("is_second_launch")) {
            z = false;
        } else {
            this.keva.storeBoolean("is_second_launch", true);
            z = true;
        }
        n.LIZIZ(LIZJ, "");
        logMandatoryLoginDecision(string, upperCase, string2, LIZJ, z);
        if (n.LIZ(LIZJ, "IT") || n.LIZ(LIZJ, "US")) {
            return true;
        }
        GuestModeServiceImpl.LIZ();
        GuestModeServiceImpl.LIZ();
        if (n.LIZ(NonPersonalizationService.LJIIJ().LJFF(), true)) {
            return true;
        }
        if ((!(!n.LIZ(NonPersonalizationService.LJIIJ().LJFF(), false)) || !S93.LIZ.contains(LIZJ)) && !n.LIZ(LIZJ, "")) {
            return false;
        }
        return true;
    }

    public final void setHasRequestComplianceApi(boolean z) {
        this.hasRequestComplianceApi = z;
    }

    public static IMandatoryLoginService createIMandatoryLoginServicebyMonsterPlugin(boolean z) {
        MethodCollector.i(2880);
        IMandatoryLoginService iMandatoryLoginService = (IMandatoryLoginService) Lnj.LIZ(IMandatoryLoginService.class, z);
        if (iMandatoryLoginService != null) {
            MethodCollector.o(2880);
            return iMandatoryLoginService;
        }
        Object LIZIZ = Lnj.LIZIZ(IMandatoryLoginService.class, z);
        if (LIZIZ != null) {
            IMandatoryLoginService iMandatoryLoginService2 = (IMandatoryLoginService) LIZIZ;
            MethodCollector.o(2880);
            return iMandatoryLoginService2;
        }
        if (Lnj.av == null) {
            synchronized (IMandatoryLoginService.class) {
                try {
                    if (Lnj.av == null) {
                        Lnj.av = new MandatoryLoginService();
                    }
                } catch (Throwable th) {
                    MethodCollector.o(2880);
                    throw th;
                }
            }
        }
        MandatoryLoginService mandatoryLoginService = (MandatoryLoginService) Lnj.av;
        MethodCollector.o(2880);
        return mandatoryLoginService;
    }

    private final void logMandatoryLoginDecision(String str, String str2, String str3, String str4, boolean z) {
        if (!this.loggedInitialMandatoryLoginDecision) {
            this.loggedInitialMandatoryLoginDecision = true;
            JtF jtF = new JtF();
            jtF.LIZ("first_ever_priority_region", str);
            jtF.LIZ("priority_region", str2);
            jtF.LIZ("first_ever_store_region", str3);
            jtF.LIZ("store_region", str4);
            Boolean LIZIZ = 7xA.LIZIZ();
            n.LIZIZ(LIZIZ, "");
            jtF.LIZ("is_first_launch", LIZIZ.booleanValue() ? 1 : 0);
            jtF.LIZ("is_second_launch", z ? 1 : 0);
            C12422nK.LIZ("mandatory_login_decision", (Map<String, String>) jtF.LIZ);
        }
    }
}
