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

import X.0Gf;
import X.0Gp;
import X.0HH;
import X.2OR;
import X.2XC;
import X.2kf;
import X.9EH;
import X.BAu;
import X.BCd;
import X.Co2;
import X.EJk;
import X.FKS;
import X.Ff3;
import X.FfJ;
import X.FfM;
import X.FfN;
import X.FfO;
import X.FfP;
import X.FfQ;
import X.FfW;
import X.JMA;
import X.Lea;
import X.Lel;
import X.M12;
import android.os.Looper;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.ies.ugc.aweme.network.RetrofitFactory;
import com.bytedance.keva.Keva;
import com.google.gson.Gson;
import com.ss.android.ugc.aweme.global.config.settings.pojo.IESSettingsProxy;
import com.ss.android.ugc.aweme.services.RetrofitService;
import com.ss.android.ugc.aweme.shortvideo.model.AnchorPublishStruct;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.h.b.n;

public final class AnchorListManager {
    public static final AnchorListApi LIZ;
    public static final Keva LIZIZ;
    public static boolean LIZJ;
    public static String LIZLLL;
    public static int LJ;
    public static final AtomicBoolean LJFF = new AtomicBoolean(false);
    public static boolean LJI;
    public static final AnchorListManager LJII = new AnchorListManager();
    public static final String LJIIIIZZ;
    public static final 2XC LJIIIZ;
    public static final GetLeadsAnchorStateApi LJIIJ;
    public static final Keva LJIIJJI;
    public static final Gson LJIIL = new Gson();
    public static Map<String, AnchorPublishStruct> LJIILIIL;

    public interface AnchorListApi {
        static {
            Covode.recordClassIndex(54093);
        }

        @JMA(LIZ = "/aweme/v1/anchor/list/")
        0Gp<FfJ> getAnchorList();
    }

    public interface GetLeadsAnchorStateApi {
        static {
            Covode.recordClassIndex(54094);
        }

        @JMA(LIZ = "/aweme/v1/ad/ba/leadsgen/anchor/status/")
        0Gp<EJk> getAnchorState();
    }

    public final boolean LIZIZ() {
        if (LJ > 0) {
            return true;
        }
        return false;
    }

    public final Map<String, AnchorPublishStruct> LIZLLL() {
        return LIZJ();
    }

    public final boolean LJ() {
        try {
            IESSettingsProxy iESSettingsProxy = 2OR.LIZ.LIZIZ;
            n.LIZIZ(iESSettingsProxy, "");
            Boolean enableAnchorCache = iESSettingsProxy.getEnableAnchorCache();
            n.LIZIZ(enableAnchorCache, "");
            return enableAnchorCache.booleanValue();
        } catch (Exception unused) {
            return false;
        }
    }

    private final Map<String, AnchorPublishStruct> LJII() {
        try {
            Keva keva = LIZIZ;
            LIZLLL = keva.getString("anchor_region_keva", (String) null);
            String string = keva.getString("anchor_map_key", (String) null);
            if (!TextUtils.isEmpty(string)) {
                return (Map) LJIIL.LIZ(string, new FfO().type);
            }
        } catch (Exception e) {
            2kf.LIZ(3, (String) null, "AnchorListManager getLocalData Exception: ".concat(String.valueOf(e)));
            2kf.LIZ(e);
            0HH.LIZ(e);
        }
        return null;
    }

    public final Map<String, AnchorPublishStruct> LIZJ() {
        Map<String, AnchorPublishStruct> map;
        if (LJ() && (((map = LJIILIIL) == null || map.isEmpty()) && !LIZJ)) {
            Map<String, AnchorPublishStruct> LJII2 = LJII();
            LIZLLL(LJII2);
            LIZJ(LJII2);
            LIZIZ(LJII2);
            LJ(LJII2);
            LJIILIIL = LJII2;
        }
        return LJIILIIL;
    }

    static {
        Covode.recordClassIndex(54091);
        String str = "https://" + 9EH.LJIIJJI.LIZ;
        LJIIIIZZ = str;
        LIZ = (AnchorListApi) RetrofitService.createIRetrofitServicebyMonsterPlugin(false).createNewRetrofit(str).create(AnchorListApi.class);
        LJIIIZ = RetrofitFactory.LIZ().LIZIZ(str).LIZJ();
        LJIIJ = (GetLeadsAnchorStateApi) RetrofitFactory.LIZ().LIZIZ(str).LIZJ().LIZ(GetLeadsAnchorStateApi.class);
        Keva repo = Keva.getRepo("anchor_data_keva");
        n.LIZIZ(repo, "");
        LIZIZ = repo;
        Keva repo2 = Keva.getRepo("fe-storage");
        n.LIZIZ(repo2, "");
        LJIIJJI = repo2;
    }

    public final List<AnchorPublishStruct> LJFF() {
        List list;
        List list2;
        Collection<AnchorPublishStruct> values;
        Collection<AnchorPublishStruct> values2;
        Map<String, AnchorPublishStruct> LIZJ2 = LIZJ();
        if (LIZJ2 == null || (values2 = LIZJ2.values()) == null) {
            list = Lel.INSTANCE;
        } else {
            ArrayList arrayList = new ArrayList();
            for (T next : values2) {
                if (((AnchorPublishStruct) next).isNew) {
                    arrayList.add(next);
                }
            }
            list = arrayList;
        }
        Map<String, AnchorPublishStruct> LIZLLL2 = LIZLLL();
        if (LIZLLL2 == null || (values = LIZLLL2.values()) == null) {
            list2 = Lel.INSTANCE;
        } else {
            ArrayList arrayList2 = new ArrayList();
            for (T next2 : values) {
                if (!((AnchorPublishStruct) next2).isNew) {
                    arrayList2.add(next2);
                }
            }
            list2 = arrayList2;
        }
        return Lea.LIZLLL(list, list2);
    }

    public final void LJI() {
        Collection<AnchorPublishStruct> values;
        List LJIIJJI2;
        AnchorPublishStruct anchorPublishStruct;
        try {
            Keva keva = LIZIZ;
            keva.storeString("anchor_map_key", LJIIL.LIZIZ(LIZJ(), new FfP().type));
            Map<String, AnchorPublishStruct> LIZJ2 = LIZJ();
            if (!(LIZJ2 == null || (values = LIZJ2.values()) == null || (LJIIJJI2 = Lea.LJIIJJI(values)) == null || (anchorPublishStruct = (AnchorPublishStruct) LJIIJJI2.get(0)) == null)) {
                LJIIJJI.storeString("ECOMMERCE_i18N_STORAGE_PROMPT_STRING", anchorPublishStruct.anchorTips);
            }
            keva.storeBoolean("anchor_local_map_success", true);
            String str = LIZLLL;
            if (str != null && str.length() > 0) {
                keva.storeString("anchor_region_keva", LIZLLL);
            }
        } catch (Exception e) {
            2kf.LIZ(3, (String) null, "AnchorListManager saveData Exception: ".concat(String.valueOf(e)));
            2kf.LIZ(e);
            0HH.LIZ(e);
        }
    }

    public final void LIZ() {
        Executor executor;
        if (LJFF.get() || !n.LIZ(Looper.myLooper(), Looper.getMainLooper()) || !Co2.LIZIZ()) {
            executor = FfQ.LIZ;
        } else {
            executor = 0Gp.LIZ;
        }
        0Gp.LIZ(FfN.LIZ, executor, (0Gf) null);
    }

    public final void LIZ(Map<String, AnchorPublishStruct> map) {
        LJIILIIL = map;
        0Gp.LIZ(FfM.LIZ);
    }

    public final 0Gp<Object> LIZ(String str) {
        return ((AnchorImpressionApi) LJIIIZ.LIZ(AnchorImpressionApi.class)).postAnchorImpressionReport(str);
    }

    public final String LIZ(AnchorPublishStruct anchorPublishStruct) {
        M12.LIZ(anchorPublishStruct);
        String str = anchorPublishStruct.logExtra;
        if (str == null) {
            return null;
        }
        Object LIZ2 = LJIIL.LIZ(str, BCd.class);
        n.LIZIZ(LIZ2, "");
        BCd bCd = (BCd) LIZ2;
        if (bCd != null) {
            return bCd.LIZ;
        }
        return null;
    }

    public final String LIZIZ(AnchorPublishStruct anchorPublishStruct) {
        Integer num;
        StringBuilder sb = new StringBuilder();
        String str = null;
        if (anchorPublishStruct != null) {
            num = Integer.valueOf(anchorPublishStruct.type);
        } else {
            num = null;
        }
        sb.append(num);
        sb.append('(');
        if (anchorPublishStruct != null) {
            str = anchorPublishStruct.subtype;
        }
        sb.append(str);
        sb.append(')');
        return sb.toString();
    }

    public final void LIZLLL(Map<String, AnchorPublishStruct> map) {
        if (map != null) {
            for (Map.Entry<String, AnchorPublishStruct> value : map.entrySet()) {
                AnchorPublishStruct anchorPublishStruct = (AnchorPublishStruct) value.getValue();
                if (anchorPublishStruct.isNew) {
                    anchorPublishStruct.hadNew = true;
                }
            }
        }
    }

    public final void LJ(Map<String, AnchorPublishStruct> map) {
        if (map != null) {
            for (Map.Entry<String, AnchorPublishStruct> value : map.entrySet()) {
                AnchorPublishStruct anchorPublishStruct = (AnchorPublishStruct) value.getValue();
                if (anchorPublishStruct.type == BAu.GET_LEADS.getTYPE()) {
                    LJIIJ.getAnchorState().LIZ(new FKS(anchorPublishStruct));
                }
            }
        }
    }

    public final 0Gp<Object> LIZ(List<AnchorPublishStruct> list) {
        M12.LIZ(list);
        FfW ffW = FfW.TitleSeen;
        ArrayList arrayList = new ArrayList();
        for (AnchorPublishStruct next : list) {
            if (next.isNew) {
                arrayList.add(new Ff3(ffW.LIZ(), next.type, next.subtype));
            }
        }
        String LIZIZ2 = new Gson().LIZIZ(arrayList);
        n.LIZIZ(LIZIZ2, "");
        return LIZ(LIZIZ2);
    }

    public final void LIZIZ(Map<String, AnchorPublishStruct> map) {
        if (map != null) {
            for (Map.Entry<String, AnchorPublishStruct> value : map.entrySet()) {
                if (((AnchorPublishStruct) value.getValue()).type == BAu.ANCHOR_SHOP_LINK.getTYPE()) {
                    LJI = true;
                }
            }
        }
    }

    public final void LIZJ(Map<String, AnchorPublishStruct> map) {
        LJ = 0;
        if (map != null) {
            for (Map.Entry<String, AnchorPublishStruct> value : map.entrySet()) {
                if (((AnchorPublishStruct) value.getValue()).isNew) {
                    LJ++;
                }
            }
        }
    }

    public final AnchorPublishStruct LIZ(int i, String str) {
        M12.LIZ(str);
        Map<String, AnchorPublishStruct> LIZJ2 = LIZJ();
        if (LIZJ2 == null) {
            return null;
        }
        return LIZJ2.get(i + '(' + str + ')');
    }
}
