package com.sheep.gamegroup.statistics;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.jiuyan.samllsheep.SheepApp;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhanghai on 2018/4/3.
 */

public class AppStatistics {
    private static AppStatistics instance = null;


    private AppStatistics(){}

    public static AppStatistics getInstance(){
        if(instance == null){
            synchronized (AppStatistics.class){
                if(instance == null){
                    instance = new AppStatistics();
                }
            }
        }
        return instance;
    }


    /**
     * 向服务器发送统计的数据
     *
     * @param type    大类型 参考 AppStatisticsConfig
     *
     * @param subType
     *
     * @param subString
     */
    synchronized public void sendDataToServer(int type,int subType,String subString){
        ApiService apiService =SheepApp.getInstance().getNetComponent().getApiService();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid", NumberFormatUtils.parseInteger(DataUtil.getInstance().getUserId()));
        jsonObject.put("type",type);
        jsonObject.put("subtype",subType);
        jsonObject.put("substring",subString);
        jsonObject.put("invitation_code", DataUtil.getInstance().getUserParentCode());
        apiService.appEventTracking(jsonObject).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                    }
                });
    }

}
