package com.gh.gamecenter.common.utils;

import android.annotation.SuppressLint;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gh.gamecenter.common.BuildConfig;
import com.gh.gamecenter.common.constant.RouteConsts;
import com.gh.gamecenter.common.entity.ShareResultEntity;
import com.gh.gamecenter.common.exposure.meta.Meta;
import com.gh.gamecenter.common.exposure.meta.MetaUtil;
import com.gh.gamecenter.common.loghub.LoghubUtils;
import com.gh.gamecenter.common.retrofit.EmptyResponse;
import com.gh.gamecenter.common.retrofit.RetrofitManager;
import com.gh.gamecenter.core.provider.IAppProvider;
import com.gh.gamecenter.core.utils.GsonUtils;
import com.lightgame.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class LogUtils {

    private static final String KEY_ACTION = "action";
    private static final String KEY_SHARE_TYPE = "share_type";
    private static final String KEY_ENTRANCE = "entrance";
    private static final String KEY_URL = "url";
    private static final String KEY_TITLE = "title";
    private static final String KEY_SUMMARY = "summary";
    private static final String KEY_RESOURCE_ID = "resource_id";
    private static final String KEY_PAYLOAD = "payload";

    public static JSONObject getMetaObject() {
        Meta meta = MetaUtil.INSTANCE.getMeta();
        JSONObject metaObject = new JSONObject();
        IAppProvider appProvider = (IAppProvider) ARouter.getInstance().build(RouteConsts.provider.app).navigation();
        try {
            metaObject.put("dia", MetaUtil.getBase64EncodedAndroidId());
            metaObject.put("android_sdk", meta.getAndroid_sdk());
            metaObject.put("android_version", meta.getAndroid_version());
            metaObject.put("appVersion", meta.getAppVersion());
            metaObject.put("channel", meta.getChannel());
            metaObject.put("gid", meta.getGid());
            metaObject.put("jnfj", MetaUtil.getBase64EncodedIMEI());
            metaObject.put("mac", meta.getMac());
            metaObject.put("manufacturer", meta.getManufacturer());
            metaObject.put("model", meta.getModel());
            metaObject.put("network", meta.getNetwork());
            metaObject.put("os", meta.getOs());
            metaObject.put("userId", meta.getUserId());
            metaObject.put("oaid", appProvider.getOaid());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return metaObject;
    }

    public static JSONObject getNewMetaObject() {
        Meta meta = MetaUtil.INSTANCE.getMeta();
        JSONObject metaObject = new JSONObject();
        IAppProvider appProvider = (IAppProvider) ARouter.getInstance().build(RouteConsts.provider.app).navigation();
        try {
            metaObject.put("dia", MetaUtil.getBase64EncodedAndroidId());
            metaObject.put("android_sdk", meta.getAndroid_sdk());
            metaObject.put("android_version", meta.getAndroid_version());
            metaObject.put("app_version", meta.getAppVersion());
            metaObject.put("channel", meta.getChannel());
            metaObject.put("gid", meta.getGid());
            metaObject.put("jnfj", MetaUtil.getBase64EncodedIMEI());
            metaObject.put("mac", meta.getMac());
            metaObject.put("manufacturer", meta.getManufacturer());
            metaObject.put("model", meta.getModel());
            metaObject.put("network", meta.getNetwork());
            metaObject.put("os", meta.getOs());
            metaObject.put("user_id", meta.getUserId());
            metaObject.put("oaid", appProvider.getOaid());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return metaObject;
    }

    @SuppressLint("CheckResult")
    public static void uploadShareResult(String shareType, String entrance, String shareResult, String url, String title, String summary, String resourceId) {
        JSONObject object = new JSONObject();
        JSONObject payloadObject = new JSONObject();
        try {
            object.put(KEY_ACTION, "share_result");
            payloadObject.put(KEY_SHARE_TYPE, shareType);
            payloadObject.put(KEY_ENTRANCE, entrance);
            payloadObject.put("share_result", shareResult);
            payloadObject.put(KEY_URL, url);
            payloadObject.put(KEY_TITLE, title);
            payloadObject.put(KEY_SUMMARY, summary);
            payloadObject.put(KEY_RESOURCE_ID, resourceId);
            object.put(KEY_PAYLOAD, payloadObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        uploadShare(object);

        ShareResultEntity entity = new ShareResultEntity(new ShareResultEntity.Content(url, title, summary), shareType, shareResult);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), GsonUtils.toJson(entity));

        RetrofitManager.getInstance().getApi()
                .postShareResult(body)
                .subscribeOn(Schedulers.io())
                .subscribe(new EmptyResponse<>());
    }

    private static void uploadShare(JSONObject object) {
        Meta meta = MetaUtil.INSTANCE.getMeta();
        JSONObject metaObject = new JSONObject();
        try {
            metaObject.put("dia", MetaUtil.getBase64EncodedAndroidId());
            metaObject.put("android_sdk", meta.getAndroid_sdk());
            metaObject.put("android_version", meta.getAndroid_version());
            metaObject.put("appVersion", meta.getAppVersion());
            metaObject.put("channel", meta.getChannel());
            metaObject.put("gid", meta.getGid());
            metaObject.put("jnfj", MetaUtil.getBase64EncodedIMEI());
            metaObject.put("mac", meta.getMac());
            metaObject.put("manufacturer", meta.getManufacturer());
            metaObject.put("model", meta.getModel());
            metaObject.put("network", meta.getNetwork());
            metaObject.put("os", meta.getOs());
            metaObject.put("userId", meta.getUserId());

            object.put("event", "SHARE");
            object.put("meta", metaObject);
            object.put("timestamp", System.currentTimeMillis() / 1000);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (BuildConfig.DEBUG) {
            Utils.log("LogUtils->" + object.toString());
        }

        LoghubUtils.log(object, "event", false);
    }

    public static void uploadShareEnter(String entrance, String url, String title, String summary, String resourceId) {
        JSONObject object = new JSONObject();
        JSONObject payloadObject = new JSONObject();
        try {
            object.put(KEY_ACTION, "entrance_source");
            payloadObject.put(KEY_ENTRANCE, entrance);
            payloadObject.put(KEY_URL, url);
            payloadObject.put(KEY_TITLE, title);
            payloadObject.put(KEY_SUMMARY, summary);
            payloadObject.put(KEY_RESOURCE_ID, resourceId);
            object.put(KEY_PAYLOAD, payloadObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        uploadShare(object);
    }

    public static void uploadShareType(String shareType, String entrance, String url, String title, String summary, String resourceId) {
        JSONObject object = new JSONObject();
        JSONObject payloadObject = new JSONObject();
        try {
            object.put(KEY_ACTION, KEY_SHARE_TYPE);
            payloadObject.put(KEY_SHARE_TYPE, shareType);
            payloadObject.put(KEY_ENTRANCE, entrance);
            payloadObject.put(KEY_URL, url);
            payloadObject.put(KEY_TITLE, title);
            payloadObject.put(KEY_SUMMARY, summary);
            payloadObject.put(KEY_RESOURCE_ID, resourceId);
            object.put(KEY_PAYLOAD, payloadObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        uploadShare(object);
    }

}
