package com.bytedance.vcloud.networkpredictor;

import X.0HH;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.json.JSONArray;
import org.json.JSONObject;

public class DefaultSpeedPredictor implements Handler.Callback, ISpeedPredictor {
    public ReentrantReadWriteLock lock;
    public long mHandle;
    public Handler mSpeedUpdateHander;
    public final ReentrantReadWriteLock.ReadLock readLock;
    public Thread subThread;
    public final ReentrantReadWriteLock.WriteLock writeLock = this.lock.writeLock();

    static {
        Covode.recordClassIndex(40184);
    }

    private native void _close(long j);

    private native long _create(int i);

    private native float _getAverageDownloadSpeed(long j, int i, int i2, boolean z);

    private native double _getDoubleValue(long j, int i, double d);

    private native Map<String, String> _getDownloadSpeed(long j, int i);

    private native int _getIntValue(long j, int i, int i2);

    private native float _getLastPredictConfidence(long j);

    private native long _getLongValue(long j, int i, long j2);

    private native SpeedPredictorResultCollection _getMultidimensionalDownloadSpeeds(long j);

    private native SpeedPredictorResultCollection _getMultidimensionalPredictSpeeds(long j);

    private native float _getPredictSpeed(long j, int i);

    private native String _getVersion();

    private native void _prepare(long j);

    private native void _release(long j);

    private native void _setConfigSpeedInfo(long j, Map<String, String> map);

    private native void _setDoubleValue(long j, int i, double d);

    private native void _setIntValue(long j, int i, int i2);

    private native void _setLongValue(long j, int i, long j2);

    private native void _setSpeedQueueSize(long j, int i);

    private native void _setStringValue(long j, int i, String str);

    private native void _start(long j);

    private native void _update(long j, ArrayList<SpeedPredictorRecord> arrayList, Map<String, Integer> map);

    private native void _updateOldWithStreamId(long j, ISpeedRecordOld iSpeedRecordOld, Map<String, Integer> map);

    private native void _updateWithSizeTime(long j, long j2, long j3, long j4);

    public float getPredictSpeed() {
        return getPredictSpeed(0);
    }

    private void resetSpeedPredictor() {
        Handler handler = this.mSpeedUpdateHander;
        if (handler != null) {
            handler.removeCallbacksAndMessages((Object) null);
            this.mSpeedUpdateHander.getLooper().quit();
            this.mSpeedUpdateHander = null;
            this.subThread = null;
        }
    }

    public String getVersion() {
        MethodCollector.i(9947);
        if (this.mHandle == 0) {
            MethodCollector.o(9947);
            return "j_1.6.2";
        }
        String _getVersion = _getVersion();
        MethodCollector.o(9947);
        return _getVersion;
    }

    public void close() {
        MethodCollector.i(9952);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9952);
            return;
        }
        _close(j);
        this.readLock.unlock();
        MethodCollector.o(9952);
    }

    public float getLastPredictConfidence() {
        MethodCollector.i(9954);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9954);
            return -1.0f;
        }
        float _getLastPredictConfidence = _getLastPredictConfidence(j);
        this.readLock.unlock();
        MethodCollector.o(9954);
        return _getLastPredictConfidence;
    }

    public String getMultidimensionalDownloadSpeeds() {
        MethodCollector.i(10232);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(10232);
            return null;
        }
        String speedPredictorResultArrayToString = speedPredictorResultArrayToString(_getMultidimensionalDownloadSpeeds(j).getResultCollection());
        this.readLock.unlock();
        MethodCollector.o(10232);
        return speedPredictorResultArrayToString;
    }

    public SpeedPredictorResultCollection getMultidimensionalDownloadSpeedsObj() {
        MethodCollector.i(10234);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(10234);
            return null;
        }
        SpeedPredictorResultCollection _getMultidimensionalDownloadSpeeds = _getMultidimensionalDownloadSpeeds(j);
        this.readLock.unlock();
        MethodCollector.o(10234);
        return _getMultidimensionalDownloadSpeeds;
    }

    public String getMultidimensionalPredictSpeeds() {
        MethodCollector.i(10233);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(10233);
            return null;
        }
        SpeedPredictorResultCollection _getMultidimensionalPredictSpeeds = _getMultidimensionalPredictSpeeds(j);
        if (_getMultidimensionalPredictSpeeds != null) {
            this.readLock.unlock();
            String speedPredictorResultArrayToString = speedPredictorResultArrayToString(_getMultidimensionalPredictSpeeds.getResultCollection());
            MethodCollector.o(10233);
            return speedPredictorResultArrayToString;
        }
        this.readLock.unlock();
        MethodCollector.o(10233);
        return null;
    }

    public SpeedPredictorResultCollection getMultidimensionalPredictSpeedsObj() {
        MethodCollector.i(10235);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(10235);
            return null;
        }
        SpeedPredictorResultCollection _getMultidimensionalPredictSpeeds = _getMultidimensionalPredictSpeeds(j);
        this.readLock.unlock();
        MethodCollector.o(10235);
        return _getMultidimensionalPredictSpeeds;
    }

    public void prepare() {
        MethodCollector.i(9950);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9950);
            return;
        }
        _prepare(j);
        this.readLock.unlock();
        MethodCollector.o(9950);
    }

    public void release() {
        MethodCollector.i(10459);
        this.writeLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.writeLock.unlock();
            MethodCollector.o(10459);
            return;
        }
        _close(j);
        _release(this.mHandle);
        this.mHandle = 0;
        resetSpeedPredictor();
        this.writeLock.unlock();
        MethodCollector.o(10459);
    }

    public void start() {
        MethodCollector.i(9951);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9951);
            return;
        }
        _start(j);
        this.readLock.unlock();
        MethodCollector.o(9951);
    }

    public boolean handleMessage(Message message) {
        MethodCollector.i(10460);
        if (message.what == 0) {
            SpeedPredictorNotifyInfo speedPredictorNotifyInfo = (SpeedPredictorNotifyInfo) message.obj;
            _updateOldWithStreamId(this.mHandle, speedPredictorNotifyInfo.mSpeedRecord, speedPredictorNotifyInfo.mStreamIdMediaTypeMap);
        }
        MethodCollector.o(10460);
        return true;
    }

    public Map<String, String> getDownloadSpeed(int i) {
        MethodCollector.i(9955);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9955);
            return null;
        }
        Map<String, String> _getDownloadSpeed = _getDownloadSpeed(j, i);
        this.readLock.unlock();
        MethodCollector.o(9955);
        return _getDownloadSpeed;
    }

    public float getPredictSpeed(int i) {
        MethodCollector.i(9953);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9953);
            return -1.0f;
        }
        float _getPredictSpeed = _getPredictSpeed(j, i);
        this.readLock.unlock();
        MethodCollector.o(9953);
        return _getPredictSpeed;
    }

    public void setConfigInfo(Map map) {
        MethodCollector.i(9948);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9948);
            return;
        }
        _setConfigSpeedInfo(j, map);
        this.readLock.unlock();
        MethodCollector.o(9948);
    }

    public void setSpeedQueueSize(int i) {
        MethodCollector.i(9949);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(9949);
            return;
        }
        _setSpeedQueueSize(j, i);
        this.readLock.unlock();
        MethodCollector.o(9949);
    }

    public DefaultSpeedPredictor(int i) {
        MethodCollector.i(9946);
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(false);
        this.lock = reentrantReadWriteLock;
        this.readLock = reentrantReadWriteLock.readLock();
        SpeedPredictorJniLoader.loadLibrary();
        if (!SpeedPredictorJniLoader.isLibraryLoaded) {
            SpeedPredictorLog.e("SpeedPredictor", "[SpeedPredictor] no predictor native loaded");
            MethodCollector.o(9946);
            return;
        }
        this.mHandle = _create(i);
        SpeedPredictorConfig.setLoglevel(0);
        _setIntValue(this.mHandle, 0, SpeedPredictorConfig.getLogLevel());
        MethodCollector.o(9946);
    }

    private String speedPredictorResultArrayToString(ArrayList<SpeedPredictorResult> arrayList) {
        JSONObject jsonObject;
        if (arrayList == null) {
            try {
                throw new NullPointerException("size");
            } catch (Exception e) {
                0HH.LIZ(e);
            }
        } else {
            JSONArray jSONArray = new JSONArray();
            Iterator<SpeedPredictorResult> it = arrayList.iterator();
            while (it.hasNext()) {
                SpeedPredictorResult next = it.next();
                if (!(next == null || (jsonObject = next.toJsonObject()) == null)) {
                    jSONArray.put(jsonObject);
                }
            }
            if (jSONArray.length() > 0) {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("data", jSONArray);
                return jSONObject.toString();
            }
            return null;
        }
    }

    public void update(ISpeedRecordOld iSpeedRecordOld, Map<String, Integer> map) {
        MethodCollector.i(10230);
        this.readLock.lock();
        if (this.mHandle == 0) {
            this.readLock.unlock();
            MethodCollector.o(10230);
            return;
        }
        if (Looper.getMainLooper() == Looper.myLooper()) {
            if (this.subThread == null) {
                AnonymousClass1 r1 = new Thread() {
                    static {
                        Covode.recordClassIndex(40185);
                    }

                    public void run() {
                        if (Looper.myLooper() == null) {
                            Looper.prepare();
                        }
                        DefaultSpeedPredictor.this.mSpeedUpdateHander = new Handler(this);
                        Looper.loop();
                    }
                };
                this.subThread = r1;
                r1.setName("speed_predict_update_thread");
                this.subThread.start();
            }
            if (this.mSpeedUpdateHander != null) {
                SpeedPredictorNotifyInfo speedPredictorNotifyInfo = new SpeedPredictorNotifyInfo();
                speedPredictorNotifyInfo.what = 0;
                speedPredictorNotifyInfo.mSpeedRecord = iSpeedRecordOld;
                speedPredictorNotifyInfo.mStreamIdMediaTypeMap = map;
                Message obtainMessage = this.mSpeedUpdateHander.obtainMessage();
                obtainMessage.what = 0;
                obtainMessage.obj = speedPredictorNotifyInfo;
                obtainMessage.sendToTarget();
            }
        } else {
            _updateOldWithStreamId(this.mHandle, iSpeedRecordOld, map);
        }
        this.readLock.unlock();
        MethodCollector.o(10230);
    }

    public void update(String str, Map<String, Integer> map) {
        MethodCollector.i(9957);
        this.readLock.lock();
        if (this.mHandle == 0 || str == null || str.length() < 0 || map == null || map.size() < 0) {
            this.readLock.unlock();
            MethodCollector.o(9957);
            return;
        }
        this.readLock.unlock();
        try {
            JSONArray optJSONArray = new JSONObject(str).optJSONArray("data");
            ArrayList arrayList = new ArrayList();
            for (int i = 0; i < optJSONArray.length(); i++) {
                try {
                    JSONObject jSONObject = optJSONArray.getJSONObject(i);
                    SpeedPredictorRecord speedPredictorRecord = new SpeedPredictorRecord();
                    speedPredictorRecord.extractFields(jSONObject);
                    arrayList.add(speedPredictorRecord);
                } catch (Throwable unused) {
                }
            }
            this.readLock.lock();
            _update(this.mHandle, arrayList, map);
            this.readLock.unlock();
            MethodCollector.o(9957);
        } catch (Exception unused2) {
            MethodCollector.o(9957);
        }
    }

    public float getAverageDownloadSpeed(int i, int i2, boolean z) {
        MethodCollector.i(10236);
        this.readLock.lock();
        long j = this.mHandle;
        if (j == 0) {
            this.readLock.unlock();
            MethodCollector.o(10236);
            return -1.0f;
        }
        float _getAverageDownloadSpeed = _getAverageDownloadSpeed(j, i, i2, z);
        this.readLock.unlock();
        MethodCollector.o(10236);
        return _getAverageDownloadSpeed;
    }

    public void update(long j, long j2, long j3) {
        MethodCollector.i(10231);
        this.readLock.lock();
        long j4 = this.mHandle;
        if (j4 == 0) {
            this.readLock.unlock();
            MethodCollector.o(10231);
            return;
        }
        _updateWithSizeTime(j4, j, j2, j3);
        this.readLock.unlock();
        MethodCollector.o(10231);
    }
}
