import 'dart:io';

import 'package:adjust_sdk/adjust.dart';
import 'package:adjust_sdk/adjust_ad_revenue.dart';
import 'package:adjust_sdk/adjust_config.dart';
import 'package:adjust_sdk/adjust_event.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';

final Logger _log = Logger('AdjustHelper');

class AdjustHelper {
  static String get appToken {
    if (Platform.isAndroid) {
      return 'r4m6v9abjjsw';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get levelStartToken {
    if (Platform.isAndroid) {
      return 'tpb9bw';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get levelDoneToken {
    if (Platform.isAndroid) {
      return '626ngi';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get levelDone3Token {
    if (Platform.isAndroid) {
      return 'ytq750';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get levelDone10Token {
    if (Platform.isAndroid) {
      return 'jdeyc8';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get levelDone20Token {
    if (Platform.isAndroid) {
      return 'iz3fud';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get levelDone30Token {
    if (Platform.isAndroid) {
      return 'vn6ivu';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get useDailyLevelStartToken {
    if (Platform.isAndroid) {
      return 'p4ivhg';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static String get useDailyLevelDoneToken {
    if (Platform.isAndroid) {
      return 'hl6z50';
    } else if (Platform.isIOS) {
      return '';
    } else {
      return '';
    }
  }

  static Future<void> init(String uuid) async {
    _log.info('init Adjust');

    if (appToken.isEmpty) return;

    AdjustConfig adjustConfig = AdjustConfig(appToken, kDebugMode ? AdjustEnvironment.sandbox : AdjustEnvironment.production);

    adjustConfig.logLevel = kDebugMode ? AdjustLogLevel.debug : AdjustLogLevel.warn;
    adjustConfig.externalDeviceId = uuid;

    Adjust.initSdk(adjustConfig);
  }

  static void trackEvent(String eventToken) async {
    _log.info('Adjust logEvent, eventToken: $eventToken');

    if (eventToken.isEmpty) return;

    Adjust.trackEvent(AdjustEvent(eventToken));
  }

  /// placement: 广告位置（banner|interstitial|reward)
  /// count: 广告展示数量
  /// revenue: 广告收益额
  /// currency: 货币单位
  static void trackAdRevenue(String placement, int count, double revenue, String currency) {
    AdjustAdRevenue adjustAdRevenue = AdjustAdRevenue('applovin_max_sdk');
    adjustAdRevenue.setRevenue(revenue, currency);
    adjustAdRevenue.adRevenuePlacement = placement;
    adjustAdRevenue.adImpressionsCount = count;
    Adjust.trackAdRevenue(adjustAdRevenue);
  }
}
