package com.ss.android.ugc.aweme.contentlanguage.viewmodel;

import X.0Bt;
import X.1Bn;
import X.7he;
import X.878;
import X.9bx;
import X.AnonymousClass4FD;
import X.C15852sr;
import X.C216735j;
import X.C29393Zb;
import X.C29413Zd;
import X.C29423Ze;
import X.C29433Zf;
import X.C29443Zg;
import X.C29453Zh;
import X.C29463Zi;
import X.C29493Zl;
import X.C29503Zm;
import X.C30053af;
import X.C474046i;
import X.Crj;
import X.DzH;
import X.JMC;
import X.JT0;
import X.M12;
import X.Mk2;
import X.Mk3;
import X.PhE;
import X.PlI;
import X.SfZ;
import android.app.Activity;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.aweme.IAccountUserService;
import com.ss.android.ugc.aweme.app.api.Api;
import com.ss.android.ugc.aweme.compliance.api.a;
import com.ss.android.ugc.aweme.contentlanguage.ContentLanguageServiceImpl;
import com.ss.android.ugc.aweme.contentlanguage.api.IContentLanguageService;
import com.ss.android.ugc.aweme.contentlanguage.api.LanguageApi;
import com.ss.android.ugc.aweme.services.RetrofitService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import kotlin.h.b.n;
import org.json.JSONObject;

public final class ContentPreferenceViewModel extends 0Bt implements C29413Zd {
    public int LIZ = -1;
    public boolean LIZIZ;
    public final Crj LIZJ = 7he.LIZ(new C29423Ze(this));
    public final Crj LIZLLL = 7he.LIZ(C29463Zi.LIZ);
    public final Crj LJ = 7he.LIZ(C29493Zl.LIZ);
    public final Crj LJFF = 7he.LIZ(C29503Zm.LIZ);

    static {
        Covode.recordClassIndex(62277);
    }

    public final 1Bn<Integer> LIZIZ() {
        return (1Bn) this.LIZLLL.getValue();
    }

    public final 1Bn<ArrayList<C216735j>> LIZJ() {
        return (1Bn) this.LJ.getValue();
    }

    public final 1Bn<ArrayList<C15852sr>> LIZLLL() {
        return (1Bn) this.LJFF.getValue();
    }

    public final void LJ() {
        IAccountUserService LJFF2 = C474046i.LJFF();
        n.LIZIZ(LJFF2, "");
        if (!LJFF2.isLogin()) {
            LJFF();
        } else {
            ContentLanguageServiceImpl.LJFF().LIZ((AnonymousClass4FD) new C29443Zg(this));
        }
    }

    public ContentPreferenceViewModel() {
        LJ();
    }

    private final void LJFF() {
        ((LanguageApi) RetrofitService.createIRetrofitServicebyMonsterPlugin(false).createNewRetrofit(Api.LIZLLL).create(LanguageApi.class)).getUnloginContentLanguage("content_language_region", ContentLanguageServiceImpl.LJFF().LIZLLL()).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZIZ(new C29453Zh(this, ContentLanguageServiceImpl.LJFF().LIZJ()));
    }

    /* JADX WARNING: Code restructure failed: missing block: B:3:0x0015, code lost:
        if (r0 == null) goto L_0x0017;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void LIZ() {
        /*
            r7 = this;
            X.1Bn r0 = r7.LIZJ()
            java.lang.Object r0 = r0.getValue()
            java.util.ArrayList r0 = (java.util.ArrayList) r0
            r6 = 0
            if (r0 == 0) goto L_0x0055
            int r0 = r0.size()
            java.lang.Integer r0 = java.lang.Integer.valueOf(r0)
            if (r0 != 0) goto L_0x001a
        L_0x0017:
            kotlin.h.b.n.LIZIZ()
        L_0x001a:
            int r5 = r0.intValue()
            java.lang.String r4 = ""
            r3 = 0
            r2 = r4
        L_0x0022:
            if (r3 >= r5) goto L_0x0057
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            r1.<init>()
            r1.append(r2)
            X.1Bn r0 = r7.LIZJ()
            java.lang.Object r0 = r0.getValue()
            java.util.ArrayList r0 = (java.util.ArrayList) r0
            if (r0 == 0) goto L_0x0053
            java.lang.Object r0 = r0.get(r3)
            X.35j r0 = (X.C216735j) r0
            if (r0 == 0) goto L_0x0053
            java.lang.String r0 = r0.getLanguageCode()
        L_0x0044:
            r1.append(r0)
            java.lang.String r0 = ","
            r1.append(r0)
            java.lang.String r2 = r1.toString()
            int r3 = r3 + 1
            goto L_0x0022
        L_0x0053:
            r0 = r6
            goto L_0x0044
        L_0x0055:
            r0 = r6
            goto L_0x0017
        L_0x0057:
            com.ss.android.ugc.aweme.app.SharePrefCache r0 = com.ss.android.ugc.aweme.app.SharePrefCache.inst()
            kotlin.h.b.n.LIZIZ(r0, r4)
            X.2sK r0 = r0.getUserAddLanguages()
            kotlin.h.b.n.LIZIZ(r0, r4)
            r0.LIZIZ(r2)
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.contentlanguage.viewmodel.ContentPreferenceViewModel.LIZ():void");
    }

    public final void LIZIZ(int i) {
        a.LJ().LIZ(i);
        Activity LJIIIZ = SfZ.LJIJ.LJIIIZ();
        if (LJIIIZ != null) {
            C30053af r1 = new C30053af(LJIIIZ);
            r1.LJ(2131832881);
            C30053af.LIZ(r1);
        }
        9bx.LIZ("opt_out_failed_settings", "", (JSONObject) null);
    }

    public final void LIZ(int i) {
        int LIZLLL2 = a.LJ().LIZLLL();
        LIZIZ().setValue(Integer.valueOf(i));
        a.LJ().LIZ(i);
        IAccountUserService LJFF2 = C474046i.LJFF();
        n.LIZIZ(LJFF2, "");
        if (LJFF2.isLogin()) {
            a.LJ().LIZIZ(i).LIZIZ(PhE.LIZIZ(PlI.LIZJ)).LIZ(Mk2.LIZ(Mk3.LIZ)).LIZIZ(new C29433Zf(this, LIZLLL2));
            return;
        }
        Integer num = (Integer) LIZIZ().getValue();
        if (num != null && num.intValue() == 3) {
            ContentLanguageServiceImpl.LJFF().LIZ(false);
        }
    }

    public final void LIZ(C216735j r6) {
        if (r6 != null) {
            ArrayList arrayList = (ArrayList) LIZJ().getValue();
            if (arrayList == null) {
                arrayList = new ArrayList();
            }
            arrayList.add(r6);
            LIZJ().setValue(arrayList);
            this.LIZIZ = false;
            ((C29393Zb) this.LIZJ.getValue()).LIZ(r6, 1);
            ArrayList arrayList2 = (ArrayList) LIZLLL().getValue();
            if (arrayList2 != null) {
                n.LIZIZ(arrayList2, "");
                Iterator it = arrayList2.iterator();
                n.LIZIZ(it, "");
                while (it.hasNext()) {
                    Object next = it.next();
                    n.LIZIZ(next, "");
                    if (n.LIZ(((C15852sr) next).LIZ(), r6.getLocalName())) {
                        it.remove();
                    }
                }
                LIZLLL().setValue(arrayList2);
            }
        }
    }

    public final void LIZ(Throwable th) {
        M12.LIZ(th);
        JMC LIZ2 = DzH.LIZ(th);
        if (LIZ2 instanceof JMC) {
            JMC jmc = LIZ2;
            int errorCode = jmc.getErrorCode();
            jmc.getResponse();
            if (errorCode == 2090) {
                IContentLanguageService LJFF2 = ContentLanguageServiceImpl.LJFF();
                n.LIZIZ(LJFF2, "");
                Activity LIZIZ2 = LJFF2.LIZIZ();
                if (LIZIZ2 != null) {
                    C30053af r1 = new C30053af(LIZIZ2);
                    r1.LJ(2131832881);
                    C30053af.LIZ(r1);
                }
                ArrayList arrayList = (ArrayList) LIZJ().getValue();
                if (!878.LIZ(arrayList)) {
                    if (arrayList != null) {
                        arrayList.remove(arrayList.size() - 1);
                    }
                    LIZJ().setValue(arrayList);
                }
            }
        }
    }

    public final void LIZ(List<? extends C216735j> list, List<? extends C216735j> list2) {
        if (!878.LIZ(list)) {
            LIZJ().setValue(list);
        }
        if (!878.LIZ(list2)) {
            ArrayList arrayList = new ArrayList();
            if (list2 == null) {
                n.LIZIZ();
            }
            for (C216735j r0 : list2) {
                arrayList.add(new C15852sr(new JT0("", r0.getEnglishName(), r0.getLanguageCode(), r0.getLocalName()), false));
            }
            LIZLLL().setValue(arrayList);
        }
    }
}
