package com.ydl.ydlcommon.utils;

import android.content.Context;

import com.ydl.burypointlib.BuryPointHelper;
import com.ydl.devicesidlib.DeviceIDHelper;
import com.ydl.pushserver.pushagent.YdlPushAgent;
import com.ydl.pushserver.pushagent.network.pack.RegisterEventBean;
import com.ydl.ydlcommon.BuildConfig;
import com.ydl.ydlcommon.base.BaseApp;
import com.ydl.ydlcommon.base.config.YDLConstants;
import com.ydl.ydlcommon.modular.ModularServiceManager;
import com.ydl.ydlcommon.router.YdlCommonOut;
import com.yidianling.common.tools.RxAppTool;
import com.yidianling.common.tools.RxDeviceTool;
import com.yidianling.common.tools.RxNetTool;
//import com.ydl.burypointlib.BuryPointHelper;

//埋点工具类
public class YdlBuryPointUtil {

    private static Context context;

    //初始化
    public static void init(Context context1){
        try {
            context = context1;
            String appEnv = BaseApp.instance.getGlobalConfig().getAppEnv();
            boolean isTest = YDLConstants.ENV_TEST.equals(appEnv);
            if (isTest){
                YdlPushAgent.setDebugAdree("47.97.49.44",8094);
            }

            RegisterEventBean registerEventBean = new RegisterEventBean();
            registerEventBean.setAppId(context.getPackageName());
            registerEventBean.setAppVersion(RxAppTool.getAppVersionName(context));
            String deviceId = DeviceIDHelper.getInstance().getDeviceId();
            registerEventBean.setDeviceId(deviceId);
            registerEventBean.setManufacturer(RxDeviceTool.getBuildMANUFACTURER());
            registerEventBean.setChannel(YdlCommonOut.Companion.getChannelName());
            registerEventBean.setOs("Android");
            registerEventBean.setModel(RxDeviceTool.getBuildBrandModel());
            registerEventBean.setOsVersion(RxDeviceTool.getOsBuileVersion());
            registerEventBean.setUid(ModularServiceManager.INSTANCE.getPlatformUserService().getUser().getUserId()+"");
            registerEventBean.setScreenHeight(RxDeviceTool.getScreenHeight(context));
            registerEventBean.setScreenWidth(RxDeviceTool.getScreenWidth(context));
            registerEventBean.setWifi(RxNetTool.getNetWorkTypeName(context));

            LogUtil.d(registerEventBean.toString());

            YdlPushAgent.init(registerEventBean,BuildConfig.DEBUG);

            BuryPointHelper.setDebug(isTest,false,true,BuildConfig.DEBUG);
            BuryPointHelper.init();

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //重新登录
    public static void reLogin(){
        try {
            RegisterEventBean registerEventBean = new RegisterEventBean();
            registerEventBean.setAppId(context.getPackageName());
            registerEventBean.setAppVersion(RxAppTool.getAppVersionName(context));
            String deviceId = DeviceIDHelper.getInstance().getDeviceId();
            registerEventBean.setDeviceId(deviceId);
            registerEventBean.setManufacturer(RxDeviceTool.getBuildMANUFACTURER());
            registerEventBean.setChannel(YdlCommonOut.Companion.getChannelName());
            registerEventBean.setOs("Android");
            registerEventBean.setModel(RxDeviceTool.getBuildBrandModel());
            registerEventBean.setOsVersion(RxDeviceTool.getOsBuileVersion());
            registerEventBean.setUid(ModularServiceManager.INSTANCE.getPlatformUserService().getUser().getUserId()+"");
            registerEventBean.setScreenHeight(RxDeviceTool.getScreenHeight(context));
            registerEventBean.setScreenWidth(RxDeviceTool.getScreenWidth(context));
            registerEventBean.setWifi(RxNetTool.getNetWorkTypeName(context));
            YdlPushAgent.reLogin(registerEventBean);

            LogUtil.d("reLogin:"+registerEventBean.toString());
        }catch (Exception e){
            LogUtil.e("reLogin:"+e.getMessage());
        }
    }

    //pv埋点
    public static void sendPv(String pageViewId){
        YdlPushAgent.sendPvEvent(pageViewId);
        LogUtil.d(pageViewId);
    }

    //click埋点
    public static void sendClick(String eventId,String... strings){
        com.yidianling.common.tools.LogUtil.d(eventId);
        YdlPushAgent.sendClickEvent(eventId,strings);
    }
}
