package com.datarecovery.master.handler;

import android.os.Handler;
import android.os.Looper;

import com.atmob.analytics.AtmobAnalytics;
import com.atmob.sdk.gravity_engine.GravityEngine;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.sdk.central.AtmobCentralHelper;
import com.datarecovery.master.utils.ToastUtil;

import org.json.JSONObject;

import java.util.Map;

public class EventHelper {

    private static final boolean isToastReport = false;

    private static Handler MAIN_HANDLER;

    static {
        if (isToastReport) {
            MAIN_HANDLER = new Handler(Looper.getMainLooper());
        }
    }


    public static void timeEvent(String eventId) {
        GravityEngine.trackTime(eventId);
    }

    public static void report(String eventId) {
        report(eventId, null);
    }

    public static void report(String eventId, Map<String, Object> params) {
        if (isToastReport) {
            MAIN_HANDLER.post(() -> {
                StringBuilder sb = new StringBuilder();
                sb.append(eventId);
                if (params != null) {
                    sb.append(" ");
                    sb.append(new JSONObject(params));
                }
                ToastUtil.show(sb.toString(), ToastUtil.LENGTH_SHORT);
            });
        }
        GravityEngine.track(eventId, params);
        AtmobCentralHelper.trackEvent(eventId, params);
    }

    public static void reportPay(int priceFen, String orderId, int payWay, String itemName) {
        String payWayName = "未知";
        switch (payWay) {
            case Constants.PAYMENT_WAY_WECHAT:
            case Constants.PAYMENT_WAY_WECHAT_SCAN:
                payWayName = "微信";
                break;
            case Constants.PAYMENT_WAY_ALIPAY:
            case Constants.PAYMENT_WAY_ALIPAY_SCAN:
                payWayName = "支付宝";
                break;
        }
        if (isToastReport) {
            MAIN_HANDLER.post(() -> ToastUtil.show("payment: priceFen = [" + priceFen + "], orderId = ["
                            + orderId + "], payWay = [" + payWay + "], itemName = [" + itemName + "]",
                    ToastUtil.LENGTH_SHORT));
        }
        GravityEngine.trackPay(priceFen,"CNY", orderId, itemName, payWayName);
        AtmobCentralHelper.trackPay(itemName, orderId, AtmobAnalytics.CURRENCY_CNY, priceFen, payWay);
    }
}