package com.gh.gamecenter.common.utils;

import android.content.Context;

import com.therouter.TheRouter;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.common.exposure.meta.MetaUtil;
import com.gh.gamecenter.common.loghub.LoghubUtils;
import com.gh.gamecenter.core.provider.IAppProvider;
import com.gh.gamecenter.core.provider.IPackageUtilsProvider;
import com.lightgame.download.DownloadEntity;
import com.lightgame.utils.Utils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LGT on 2016/12/8.
 * 日志上传工具类
 */
public class DataLogUtils {

    // 轮播图
    public static void uploadLunbotuLog(Context context, String type, String text, String title, String index, String source) {
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("type", type);
        map.put("text", text);
        map.put("source", source);
        map.put("slide_title", title);
        uploadLog(context, "slide", map);
    }

    // 搜索热门标签
    public static void uploadHotTagLog(Context context, String tag) {
        Map<String, Object> map = new HashMap<>();
        map.put("tag", tag);
        uploadLog(context, "search-hot-tag", map);
    }

    // 点击下载按钮或进入游戏详情
    public static void uploadGameLog(Context context, String gameId, String gameName, String entrance) {
        Map<String, Object> map = new HashMap<>();
        map.put("game_id", gameId);
        map.put("game_name", gameName);
        map.put("entrance", entrance);
        uploadLog(context, "game", map);
    }

    // 上传日志
    public static void uploadLog(Context context, String topic, Map<String, Object> map) {
        IPackageUtilsProvider packageUtilsConfig = TheRouter.get(IPackageUtilsProvider.class);
        IAppProvider appProvider = TheRouter.get(IAppProvider.class);
        String version = packageUtilsConfig.getGhVersionName();
        String user = Installation.getUUID(context);
        String channel = appProvider.getChannel();
        map.put("version", version);
        map.put("resu", user);
        map.put("jnfj", MetaUtil.getBase64EncodedIMEI());
        map.put("channel", channel);

        Map<String, String> params = new HashMap<>();
        if ("search-hot-tag".equals(topic) || "game".equals(topic) || "slide".equals(topic)) {
            params.put("meta", LogUtils.getMetaObject().toString());
        }
        params.put("topic", topic);
        params.put("source", "GH-ASSIST-Client");
        params.put("time", String.valueOf(Utils.getTime(context)));
        params.put("content", new JSONObject(map).toString());

        LoghubUtils.log(new JSONObject(params), "common", true);
    }

    // 网络错误
    public static void uploadNeterrorLog(Context context, DownloadEntity downloadEntity) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", downloadEntity.getUrl());
        map.put("game", downloadEntity.getName());
        map.put("game_id", downloadEntity.getGameId());
        map.put("platform", downloadEntity.getPlatform());
        map.put("error", downloadEntity.getError());
        uploadLog(context, "neterror", map);
    }

    // 助手更新
    public static void uploadUpgradeLog(Context context, String step) {
        Map<String, Object> map = new HashMap<>();
        map.put("step", step);
        uploadLog(context, "upgrade", map);
    }

    // 链接劫持
    public static void uploadHijack(Context context, DownloadEntity downloadEntity) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", downloadEntity.getUrl());
        map.put("game", downloadEntity.getName());
        map.put("game_id", downloadEntity.getGameId());
        map.put("platform", downloadEntity.getPlatform());
        map.put("hijack_url", downloadEntity.getError());
        uploadLog(context, "hijack", map);
    }

}
