package com.gem.media.splash.base.matrix;

import android.app.Application;
import android.util.Log;

import com.gem.media.splash.base.utils.ALog;
import com.tencent.matrix.Matrix;
import com.tencent.matrix.trace.TracePlugin;
import com.tencent.matrix.trace.config.TraceConfig;

public class MatrixUtils {

    private static final String TAG = "MatrixUtils";

    public static void init(Application application) {
        try {
            MdDynamicConfigImpl dynamicConfig = new MdDynamicConfigImpl();

            ALog.d(TAG, "Start Matrix configurations start ");

            // Builder. Not necessary while some plugins can be configured separately.
            Matrix.Builder builder = new Matrix.Builder(application);
            builder.pluginListener(new MdPluginListener(application));

            TracePlugin tracePlugin = configureTracePlugin(dynamicConfig);
            builder.plugin(tracePlugin);
            Matrix.init(builder.build());
            tracePlugin.start();

            ALog.d(TAG, "Start Matrix configurations end ");
        } catch (Exception e) {
            ALog.e(TAG, "matrix init e :" + Log.getStackTraceString(e));
        }
    }

    private static TracePlugin configureTracePlugin(MdDynamicConfigImpl dynamicConfig) {

        boolean fpsEnable = dynamicConfig.isFPSEnable();
        boolean traceEnable = dynamicConfig.isTraceEnable();
        boolean signalAnrTraceEnable = dynamicConfig.isSignalAnrTraceEnable();
        ALog.d(TAG, "fpsEnable :" + fpsEnable + ",,traceEnable :" + traceEnable
                + ", signalAnrTraceEnable :" + signalAnrTraceEnable);

        TraceConfig traceConfig = new TraceConfig.Builder()
                .dynamicConfig(dynamicConfig)
                .enableAnrTrace(traceEnable)
                .enableSignalAnrTrace(signalAnrTraceEnable)             // Introduced in Matrix 2.0
                .enableEvilMethodTrace(false)
                .isDebug(false)
                .isDevEnv(false)
                .build();

        return new TracePlugin(traceConfig);
    }
}
