import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

/// Created by Haoyi on 2023/3/29
///

class Formatter {
  static const int main = 0;
  static const int raw = 1;
}

class MethodNames {
  static const String create = "create_log";
  static const String clear = "clear_log";
  static const String log = "log";
  static const String getLogRootDir = "get_log_root_dir";
}

class PersistentLog {
  static const channel = MethodChannel("app.guru.persistent.LOG");

  static const String logName = "log_name";
  static const String fileSizeLimit = "file_size";
  static const String fileCount = "file_count";
  static const String level = "level";
  static const String formatter = "formatter";
  static const String echo = "echo";
  static const String message = "msg";

  static Future<bool> createLogger(
      {required String logName,
      int fileSizeLimit = 1024 * 1024 * 10,
      int fileCount = 7,
      int formatter = Formatter.main,
      bool echo = false}) async {
    if (kIsWeb) {
      return false;
    }
    return await channel.invokeMethod(MethodNames.create, {
      PersistentLog.logName: logName,
      PersistentLog.fileSizeLimit: fileSizeLimit,
      PersistentLog.fileCount: fileCount,
      PersistentLog.formatter: formatter,
      PersistentLog.echo: echo,
    });
  }

  static Future clearLogger({required String logName}) async {
    if (kIsWeb) {
      return false;
    }
    return await channel.invokeMethod(MethodNames.clear, {
      PersistentLog.logName: logName,
    });
  }

  static Future log({required String logName, required String message}) async {
    if (kIsWeb) {
      return false;
    }
    return await channel.invokeMethod(MethodNames.log, {
      PersistentLog.logName: logName,
      PersistentLog.message: message,
    });
  }

  static Future<File> getLogRootDir() async {
    if (kIsWeb) {
      return File("");
    }
    final path = await channel.invokeMethod(MethodNames.getLogRootDir);
    return File(path);
  }
}
