package com.gh.gamecenter.common.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gh.gamecenter.common.BuildConfig;
import com.gh.gamecenter.common.constant.Constants;
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 {

    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("action", "share_result");
            payloadObject.put("share_type", shareType);
            payloadObject.put("entrance", entrance);
            payloadObject.put("share_result", shareResult);
            payloadObject.put("url", url);
            payloadObject.put("title", title);
            payloadObject.put("summary", summary);
            payloadObject.put("resource_id", resourceId);
            object.put("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<>());

        // 判断是否上报游戏单活动的"转发活动"任务
        postGameCollectionRepostActivityTaskCheck(url, shareResult);
    }

    @SuppressLint("CheckResult")
    public static void postGameCollectionRepostActivityTaskCheck(String url, String shareResult) {
        if (!TextUtils.isEmpty(url) && "success".equals(shareResult)) {
            String gameCollectionActivityUrl;
            if (EnvHelper.isDevEnv()) {
                gameCollectionActivityUrl = Constants.GAME_COLLECTION_ACTIVITY_ADDRESS_DEV;
            } else {
                gameCollectionActivityUrl = Constants.GAME_COLLECTION_ACTIVITY_ADDRESS;
            }
            if (url.contains(gameCollectionActivityUrl)) {
                RetrofitManager.getInstance().getNewApi()
                        .postGameCollectionTask("repost_activity", "0")
                        .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("action", "entrance_source");
            payloadObject.put("entrance", entrance);
            payloadObject.put("url", url);
            payloadObject.put("title", title);
            payloadObject.put("summary", summary);
            payloadObject.put("resource_id", resourceId);
            object.put("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("action", "share_type");
            payloadObject.put("share_type", shareType);
            payloadObject.put("entrance", entrance);
            payloadObject.put("url", url);
            payloadObject.put("title", title);
            payloadObject.put("summary", summary);
            payloadObject.put("resource_id", resourceId);
            object.put("payload", payloadObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        uploadShare(object);
    }

}
