package com.ss.android.ugc.aweme.ml.impl;

import X.2nK;
import X.AnonymousClass7Q8;
import X.AnonymousClass8BX;
import X.AnonymousClass8BY;
import X.AnonymousClass8XV;
import X.AnonymousClass8XW;
import X.AnonymousClass9EH;
import X.C00227Qp;
import X.C00397Rg;
import X.Lnj;
import X.M12;
import android.os.SystemClock;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.pitaya.api.PitayaCoreFactory;
import com.bytedance.pitaya.api.bean.PTYTaskConfig;
import com.bytedance.pitaya.api.bean.PTYTaskData;
import com.ss.android.ugc.aweme.ml.ab.SmartCDNRankMLModel;
import com.ss.android.ugc.aweme.ml.ab.SmartCDNStrategyConfig;
import com.ss.android.ugc.aweme.ml.api.SmartCDNRankService;
import java.util.ArrayList;
import java.util.List;
import kotlin.h.b.n;
import org.json.JSONArray;
import org.json.JSONObject;

public final class SmartCDNRankServiceImpl implements SmartCDNRankService {
    public static final String LIZIZ = "smart_cdn_rank_result";
    public static final AnonymousClass8XV LIZJ = new AnonymousClass8XV((byte) 0);
    public List<AnonymousClass7Q8> LIZLLL = new ArrayList();

    static {
        Covode.recordClassIndex(92976);
    }

    public static SmartCDNRankService LIZ() {
        MethodCollector.i(16637);
        SmartCDNRankService smartCDNRankService = (SmartCDNRankService) Lnj.LIZ(SmartCDNRankService.class, false);
        if (smartCDNRankService != null) {
            MethodCollector.o(16637);
            return smartCDNRankService;
        }
        Object LIZIZ2 = Lnj.LIZIZ(SmartCDNRankService.class, false);
        if (LIZIZ2 != null) {
            SmartCDNRankService smartCDNRankService2 = (SmartCDNRankService) LIZIZ2;
            MethodCollector.o(16637);
            return smartCDNRankService2;
        }
        if (Lnj.n == null) {
            synchronized (SmartCDNRankService.class) {
                try {
                    if (Lnj.n == null) {
                        Lnj.n = new SmartCDNRankServiceImpl();
                    }
                } catch (Throwable th) {
                    MethodCollector.o(16637);
                    throw th;
                }
            }
        }
        SmartCDNRankServiceImpl smartCDNRankServiceImpl = (SmartCDNRankServiceImpl) Lnj.n;
        MethodCollector.o(16637);
        return smartCDNRankServiceImpl;
    }

    public final AnonymousClass8BX acquireCdnScores() {
        SmartCDNRankMLModel LIZ = C00397Rg.LIZIZ.LIZ();
        if (LIZ != null && LIZ.getEnabled()) {
            String LIZ2 = AnonymousClass8XW.LIZ.LIZ(LIZIZ);
            if (LIZ2.length() == 0) {
                return null;
            }
            try {
                JSONObject jSONObject = new JSONObject(LIZ2);
                JSONArray jSONArray = jSONObject.getJSONArray("rank_wifi");
                JSONArray jSONArray2 = jSONObject.getJSONArray("rank_other");
                n.LIZIZ(jSONArray, "");
                List<AnonymousClass8BY> LIZ3 = LIZ(jSONArray);
                n.LIZIZ(jSONArray2, "");
                return new AnonymousClass8BX(LIZ3, LIZ(jSONArray2));
            } catch (Exception unused) {
            }
        }
        return null;
    }

    private final JSONObject LIZIZ() {
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        jSONObject.put("cdn_qos", jSONArray);
        for (AnonymousClass7Q8 next : this.LIZLLL) {
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("network_type", next.LJFF);
            jSONObject2.put("ff_duration", next.LIZ);
            jSONObject2.put("block_count", next.LIZIZ);
            jSONObject2.put("block_duration", next.LIZJ);
            jSONObject2.put("video_duration", next.LIZLLL);
            JSONArray jSONArray2 = new JSONArray();
            for (C00227Qp next2 : next.LJ) {
                JSONObject jSONObject3 = new JSONObject();
                jSONObject3.put("cdn_code", next2.LIZJ);
                jSONObject3.put("size", next2.LIZ);
                jSONObject3.put("dur", next2.LIZIZ);
                jSONArray2.put(jSONObject3);
            }
            jSONObject2.put("download_info", jSONArray2);
            jSONArray.put(jSONObject2);
        }
        return jSONObject;
    }

    private final JSONArray LIZ(List<Integer> list) {
        JSONArray jSONArray = new JSONArray();
        for (Integer intValue : list) {
            jSONArray.put(intValue.intValue());
        }
        return jSONArray;
    }

    private final List<AnonymousClass8BY> LIZ(JSONArray jSONArray) {
        ArrayList arrayList = new ArrayList();
        int length = jSONArray.length();
        for (int i = 0; i < length; i++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i);
            arrayList.add(new AnonymousClass8BY(jSONObject.getInt("cdn"), jSONObject.getInt("score")));
        }
        return arrayList;
    }

    public final void appendCdnInfo(AnonymousClass7Q8 r10) {
        int i;
        String str;
        SmartCDNRankMLModel LIZ;
        SmartCDNStrategyConfig strategy_config;
        SmartCDNRankMLModel LIZ2;
        List<Integer> main_cdn_list;
        M12.LIZ(r10);
        SmartCDNRankMLModel LIZ3 = C00397Rg.LIZIZ.LIZ();
        if (LIZ3 != null && LIZ3.getEnabled() && !r10.LJ.isEmpty()) {
            SmartCDNRankMLModel LIZ4 = C00397Rg.LIZIZ.LIZ();
            if (LIZ4 != null) {
                i = LIZ4.getCalcuate_throttle();
            } else {
                i = 2;
            }
            this.LIZLLL.add(r10);
            if (this.LIZLLL.size() >= i) {
                SmartCDNRankMLModel LIZ5 = C00397Rg.LIZIZ.LIZ();
                if (!(LIZ5 == null || (str = LIZ5.packageUrl) == null || (LIZ = C00397Rg.LIZIZ.LIZ()) == null || (strategy_config = LIZ.getStrategy_config()) == null || (LIZ2 = C00397Rg.LIZIZ.LIZ()) == null || (main_cdn_list = LIZ2.getMain_cdn_list()) == null)) {
                    JSONObject jSONObject = new JSONObject();
                    jSONObject.put("raw_data", LIZIZ());
                    jSONObject.put("main_cdn_list", LIZ(main_cdn_list));
                    JSONObject jSONObject2 = new JSONObject();
                    jSONObject2.put("name", strategy_config.getName());
                    JSONObject jSONObject3 = new JSONObject();
                    jSONObject3.put("n_records", strategy_config.getN_records());
                    jSONObject3.put("exploration_ratio", Float.valueOf(strategy_config.getExploration_ratio()));
                    jSONObject3.put("n_samples", strategy_config.getN_samples());
                    jSONObject3.put("mu", strategy_config.getMu());
                    jSONObject3.put("sigma", strategy_config.getSigma());
                    jSONObject3.put("score_norm_ratio", Float.valueOf(strategy_config.getScore_norm_ratio()));
                    jSONObject3.put("score_min_threshold", strategy_config.getScore_min_threshold());
                    jSONObject3.put("score_max_threshold", strategy_config.getScore_max_threshold());
                    jSONObject2.put("parameter", jSONObject3);
                    jSONObject.put("strategy_config", jSONObject2);
                    PitayaCoreFactory.getCore(String.valueOf(AnonymousClass9EH.LJIILJJIL)).runTask(str, new PTYTaskData(jSONObject), new PTYTaskConfig(false, (String) null, 3600.0f), new SmartCDNRankServiceImpl$calculateCdnScores$1(this, SystemClock.uptimeMillis(), str));
                }
                this.LIZLLL = new ArrayList();
            }
        }
    }

    public final void LIZ(int i, int i2, String str, long j, String str2) {
        JSONObject jSONObject = new JSONObject();
        jSONObject.put("scene", "cdn_rank");
        jSONObject.put("model_name", str);
        jSONObject.put("duration", j);
        jSONObject.put("success", i);
        jSONObject.put("sdk_success", i2);
        if (str2 != null) {
            jSONObject.put("s_result", str2);
        }
        2nK.LIZ("ml_scene_run", jSONObject);
    }
}
