import 'package:airbridge_flutter_sdk/airbridge_flutter_sdk.dart';
import 'package:clean/data/consts/event_report_id.dart';
import 'package:clean/data/repositories/event_repository.dart';
import 'package:clean/sdk/umeng/umeng_helper.dart';

import '../data/consts/constants.dart';
import '../utils/toast_util.dart';

class EventHandler {
  static const bool isShowToast = false;

  EventHandler._();

  static void report(String eventId, {Map<String, dynamic>? params}) {
    if (isShowToast) {
      if (params == null) {
        ToastUtil.show(eventId);
      } else {
        ToastUtil.show('$eventId ${params.toString()}');
      }
    }
    Airbridge.trackEvent(category: eventId, customAttributes: params);
    var time = DateTime.now().millisecondsSinceEpoch;
    List<Map<String, dynamic>> es = <Map<String, dynamic>>[];
    Map<String, dynamic> esItem = <String, dynamic>{};
    esItem["ty"] = "track";
    esItem["ev"] = eventId;
    esItem["ti"] = time;
    es.add(esItem);
    if (params != null) {
      eventRepository.eventPush(time, es, pr: params);
    } else {
      eventRepository.eventPush(time, es);
    }
    // UmengHelper.onEvent(eventId, params ?? {"defaultKey": "default"});
  }

  static void reportPay(
      int priceFen, String orderId, String itemName, int payWay) {
    if (isShowToast) {
      String? payWayStr;
      switch (payWay) {
        case PayMethod.alipay:
          payWayStr = '支付宝';
          break;
        case PayMethod.wechat:
          payWayStr = '微信';
          break;
        case PayMethod.apple:
          payWayStr = '苹果';
          break;
      }
      ToastUtil.show(
          'PAY: 金额：$priceFen 订单号：$orderId 商品名：$itemName 支付方式：$payWayStr');
    }
    // UmengHelper.onEvent(EventId.event_02007,  {"price": priceFen, "orderId": orderId, "itemName": itemName, "payWay": payWay});

    var time = DateTime.now().millisecondsSinceEpoch;
    List<Map<String, dynamic>> es = <Map<String, dynamic>>[];
    Map<String, dynamic> esItem = <String, dynamic>{};
    esItem["ty"] = "track";
    esItem["ev"] = "\$payment";
    esItem["ti"] = time;
    es.add(esItem);
    Map<String, dynamic> params = <String, dynamic>{};
    params["\$pitem"] = itemName;
    params["\$pmon"] = orderId;
    params["\$pcur"] = "USD";
    params["\$pamt"] = priceFen;
    params["\$pmtd"] = "appstore";
    eventRepository.eventPush(time, es, pr: params);
  }

  // 首次启动应用
  static void pushInstall() {

    if (isFirstIntoApp()) {
      var time = DateTime.now().millisecondsSinceEpoch;
      List<Map<String, dynamic>> es = <Map<String, dynamic>>[];
      Map<String, dynamic> esItem = <String, dynamic>{};
      esItem["ty"] = "track";
      esItem["ev"] = "\$install";
      esItem["ti"] = time;
      es.add(esItem);
      eventRepository.eventPush(time, es);
    }
  }
}
