import 'package:persistent/log/persistent_log.dart';
import 'package:intl/intl.dart';

/// Created by Haoyi on 2023/3/30

class AdsType {
  static const String banner = "B";
  static const String interstitial = "I";
  static const String rewarded = "R";
  static const String native = "N";
}

class AiBi {
  static AiBi instance = AiBi();
  static const logName = "aibi";
  static final standardDateFormat = DateFormat("yyyyMMddTHHmmss");

  void init() async {
    await PersistentLog.createLogger(logName: logName, formatter: Formatter.raw);
  }

  void _log(List<String> args) {
    PersistentLog.log(logName: logName, message: args.join(","));
  }

  void adsLoad(String adsType, {String adScene = "Unknown"}) {
    _log([standardDateFormat.format(DateTime.now()), "ads_load", adsType, adScene]);
  }

  void adsLoaded(String adsType, {String adScene = "Unknown", int duration = 0}) {
    _log([
      standardDateFormat.format(DateTime.now()),
      "ads_loaded",
      adsType,
      adScene,
      duration.toString()
    ]);
  }

  void adsImp(String adsType,
      {String adScene = "Unknown", double adRevenue = 0, String network = "Unknown"}) {
    _log([
      standardDateFormat.format(DateTime.now()),
      "ads_imp",
      adsType,
      adScene,
      adRevenue.toString(),
      '"$network"'
    ]);
  }

  void adsClk(String adsType, {String adScene = "Unknown"}) {
    _log([
      standardDateFormat.format(DateTime.now()),
      "ads_clk",
      adsType,
      adScene,
    ]);
  }

  void adsFailed(String adsType, {String adScene = "Unknown", String reason = "Unknown"}) {
    _log([standardDateFormat.format(DateTime.now()), "ads_failed", adsType, adScene, '"$reason"']);
  }

  void adsHide(String adsType, {String adScene = "Unknown"}) {
    _log([standardDateFormat.format(DateTime.now()), "ads_hide", adsType, adScene]);
  }

  void adsRewarded(String adScene) {
    _log([standardDateFormat.format(DateTime.now()), "ads_rwd", adScene]);
  }

  void spendVirtualCurrency(int balance, double amount, String scene) {
    _log([
      standardDateFormat.format(DateTime.now()),
      "svc",
      "coin",
      balance.toString(),
      amount.toString(),
      '"$scene"'
    ]);
  }

  void earnVirtualCurrency(int balance, double amount, String method) {
    _log([
      standardDateFormat.format(DateTime.now()),
      "evc",
      "coin",
      balance.toString(),
      amount.toString(),
      method
    ]);
  }

  void click(String scene) {
    _log([standardDateFormat.format(DateTime.now()), "clk", '"$scene"']);
  }

  void dialog(String name) {
    _log([standardDateFormat.format(DateTime.now()), "dlg", '"$name"']);
  }

  void lifecycle(String name, bool isResumed) {
    _log([standardDateFormat.format(DateTime.now()), "lifecycle", name, isResumed ? 'R' : "P"]);
  }

  void applife(bool isForeground) {
    _log([standardDateFormat.format(DateTime.now()), "applife", isForeground ? 'F' : "B"]);
  }
}
