package com.gh.common.util;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.tencent.stat.MtaSDkException;
import com.tencent.stat.StatConfig;
import com.tencent.stat.StatReportStrategy;
import com.tencent.stat.StatService;
import com.tendcloud.tenddata.TCAgent;

import java.util.Map;
import java.util.Properties;

/**
 * Created by LGT on 2016/6/15.
 */
public class DataUtils {

    public static void init(Application application) {
        //TalkingData
        //dubug true release false
        TCAgent.LOG_ON = true;
        TCAgent.init(application);
        TCAgent.setReportUncaughtExceptions(true);

        //MTA
        // 打开debug开关，可查看mta上报日志或错误
        // dubug true release false
        StatConfig.setDebugEnable(true);
        // 收集未处理的异常
        StatConfig.setAutoExceptionCaught(true);
        // 设置数据上报策略
        StatConfig.setStatSendStrategy(StatReportStrategy.PERIOD);
        StatConfig.setSendPeriodMinutes(5);
        // 开启收集服务
        String TA_APPKEY = (String) PackageUtils.getMetaData(application, application.getPackageName(), "TA_APPKEY");
        try {
            StatService.startStatService(application, TA_APPKEY, com.tencent.stat.common.StatConstants.VERSION);
        } catch (MtaSDkException e) {
            e.printStackTrace();
        }
    }

    public static void onEvent(Context var0, String var1, String var2) {
        TCAgent.onEvent(var0, var1, var2);
        StatService.trackCustomEvent(var0, var1, var2);
    }

    public static void onEvent(Context var0, String var1, String var2, Map<String, Object> var3) {
        TCAgent.onEvent(var0, var1, var2, var3);
        Properties prop = new Properties();
        prop.setProperty("label", var2);
        for (String key : var3.keySet()) {
            prop.setProperty(key, var3.get(key) + "");
        }
        StatService.trackCustomKVEvent(var0, var1, prop);
    }

    public static void onPause(Activity var0) {
        TCAgent.onPause(var0);
        StatService.onPause(var0);
    }

    public static void onResume(Activity var0) {
        TCAgent.onResume(var0);
        StatService.onResume(var0);
    }

}
