package com.gh.common.util;

import android.content.Context;
import android.os.Build;

import com.gh.gamecenter.common.constant.Constants;
import com.gh.gamecenter.common.constant.EntranceConsts;
import com.gh.gamecenter.common.utils.NetworkUtils;
import com.gh.gamecenter.entity.GameEntity;
import com.gh.gamecenter.entity.NewsDetailEntity;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.PackagesManager;
import com.lightgame.download.DownloadEntity;

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

import kotlin.text.StringsKt;

/**
 * Created by LGT on 2016/12/9.
 * 数据收集 工具类（data.ghzs666.com）
 */
public class DataCollectionUtils {

    // 上传助手奔溃数据
    public static void uploadError(Context context, String msg) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", msg);
        map.put("type", android.os.Build.MODEL);
        map.put("system", android.os.Build.VERSION.SDK_INT + "=" + android.os.Build.VERSION.RELEASE);
        // WIFI实时
        DataCollectionManager.onEvent(context, "error", map, NetworkUtils.isWifiConnected(context));
    }

    // 上传下载数据（开始、完成）
    public static void uploadDownload(Context context, DownloadEntity downloadEntity, String status) {
        Map<String, Object> map = new HashMap<>();
        map.put("game", StringsKt.removeSuffix(downloadEntity.getName(), Constants.GAME_NAME_DECORATOR));
        map.put("game_id", downloadEntity.getGameId());
        if (downloadEntity.isPluggable()) {
            map.put("method", "插件化");
            map.put("btn_status", "插件化");
        } else if (downloadEntity.isUpdate()) {
            map.put("method", "更新");
            map.put("btn_status", "更新");
        } else {
            map.put("method", "正常");
            map.put("btn_status", "下载");
        }
        map.put("platform", PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform()));
        map.put("status", status);
        map.put("location", downloadEntity.getLocation());
        map.put(EntranceConsts.KEY_ENTRANCE, downloadEntity.getEntrance());
        map.put("installed", downloadEntity.getInstalled());
        map.put("network", NetworkUtils.getConnectedType(context));
        DataCollectionManager.onEvent(context, "download", map);
    }

    // 上传点击数据
    public static void uploadClick(Context context, String... args) {
        if (args.length < 2) {
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("location", args[0]);
        map.put("page", args[1]);
        if (args.length == 3) {
            map.put("title", args[2]);
        }
        DataCollectionManager.onEvent(context, "click-item", map);
    }

    // 上传游戏数据
    public static void uploadGame(Context context, GameEntity gameEntity, int seconds, String from) {
        Map<String, Object> map = new HashMap<>();
        map.put("game", gameEntity.getName());
        map.put("game_id", gameEntity.getId());
        map.put("time", seconds);
        map.put("from", from);
        DataCollectionManager.onEvent(context, "game", map);
    }

    // 上传新闻数据
    public static void uploadNews(Context context, NewsDetailEntity newsDetailEntity,
                                  GameEntity gameEntity, int seconds, String from) {
        Map<String, Object> map = new HashMap<>();
        map.put("title", newsDetailEntity.getTitle());
        map.put("type", newsDetailEntity.getType());
        map.put("author", newsDetailEntity.getAuthor());
        if (gameEntity != null) {
            map.put("game", gameEntity.getName());
            map.put("game_id", gameEntity.getId());
        }
        map.put("time", seconds);
        map.put("from", from);
        DataCollectionManager.onEvent(context, "news", map);
    }

    // 上传安装或卸载数据
    public static void uploadInorunstall(Context context, String type, String packageName) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("packageName", packageName);
        DataCollectionManager.onEvent(context, "inorunstall", 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());
        DataCollectionManager.onEvent(context, "hijack", map);
    }

    // 上传用户数据
    public static void uploadUser(Context context) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", Build.MODEL);
        map.put("system", Build.VERSION.SDK_INT + "=" + Build.VERSION.RELEASE);
        map.put("install", PackagesManager.INSTANCE.getInstalledList());
        DataCollectionManager.upsert(context, "user", map);
    }

    // 上传搜索数据
    public static void uploadSearch(Context context, String... args) {
        if (args.length != 5) {
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("from", args[2]);
        map.put("search_type", args[1]);
        map.put("key", args[0]);
        map.put("game_id", args[3]);
        map.put("game_name", args[4]);
        map.put("netword", NetworkUtils.getConnectedType(context));
        map.put("type", "search");
        map.put("device_type", android.os.Build.MODEL);
        map.put("device_system", android.os.Build.VERSION.SDK_INT + "=" + android.os.Build.VERSION.RELEASE);
        DataCollectionManager.onEvent(context, "search", map);
    }

    // 上传搜索点击数据
    public static void uploadSearchClick(Context context, String... args) {
        if (args.length != 5) {
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("from", args[2]);
        map.put("search_type", args[1]);
        map.put("key", args[0]);
        map.put("game_id", args[3]);
        map.put("game_name", args[4]);
        map.put("netword", NetworkUtils.getConnectedType(context));
        map.put("type", "click");
        map.put("device_type", android.os.Build.MODEL);
        map.put("device_system", android.os.Build.VERSION.SDK_INT + "=" + android.os.Build.VERSION.RELEASE);
        DataCollectionManager.onEvent(context, "search", map);
    }

    // 上传关注数据
    public static void uploadConcern(Context context, String... args) {
        if (args.length < 3) {
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("game", args[0]);
        map.put("game_id", args[1]);
        map.put("type", args[2]);
        DataCollectionManager.onEvent(context, "concern", map);
    }
}
