import { randomBytes } from "crypto";
import * as fs from "fs";
import { LoggingBase } from "@hibas123/logging";
import Logging, { DefaultFileAdapter, FileAdapter } from ".";

const deleteFolderRecursive = function (path: string) {
   if (fs.existsSync(path)) {
      fs.readdirSync(path).forEach(function (file, index) {
         var curPath = path + "/" + file;
         if (fs.lstatSync(curPath).isDirectory()) {
            // recurse
            deleteFolderRecursive(curPath);
         } else {
            // delete file
            fs.unlinkSync(curPath);
         }
      });
      fs.rmdirSync(path);
   }
};

deleteFolderRecursive("./logs");

Logging.log("test");
Logging.log("i", "am", { a: "an" }, 1000);
Logging.error(new Error("fehler 001"));
Logging.debug("Some Debug infos");
Logging.error("i", "am", "an", "error");

Logging.log(
   "\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m"
);

let err = new Error();
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack);

let cus = new LoggingBase({ name: "test" });
cus.log("Hello from custom Logger");

let cus2 = Logging.getChild("test2");
cus2.log("Hello from custom Logger 2");

let cus22 = Logging.getChild("test2");
cus22.log("Hello from custom Logger 22");
cus2.log("Hello from custom Logger 2");
cus22.log("Hello from custom Logger 22");
cus2.log("Hello from custom Logger 2");
cus22.log("Hello from custom Logger 22");
cus2.log("Hello from custom Logger 2");
cus22.log("Hello from custom Logger 22");
cus2.log("Hello from custom Logger 2");
cus22.log("Hello from custom Logger 22");
cus2.log("Hello from custom Logger 2");

async function benchmark(count: number, message_size: number) {
   const BenchmarkLogger = new LoggingBase({
      console: false,
      name: "bench",
   });

   if (fs.existsSync("logs/benchmark")) {
      fs.unlinkSync("logs/benchmark");
   }

   if (fs.existsSync("logs/benchmark.old")) {
      fs.unlinkSync("logs/benchmark.old");
   }

   const BenchmarkFile = new FileAdapter("logs/benchmark");

   BenchmarkLogger.addAdapter(BenchmarkFile);

   const randData = randomBytes(message_size / 2).toString("hex");
   const t = process.hrtime();
   for (let i = 0; i < count; i++) {
      BenchmarkLogger.log(randData);
   }
   await BenchmarkFile.flush(false);
   await BenchmarkLogger.close();

   const diff = process.hrtime(t);
   const NS_PER_SEC = 1e9;
   const ns = diff[0] * NS_PER_SEC + diff[1];
   console.log(
      `Benchmark took ${
         ns / 1000000
      }ms for ${count} messages with a size of ${message_size} characters`
   );
   console.log(`This is equal to ${ns / 1000000 / count} ms per message`);
}

const benchTimer = Logging.time("benchmark");
Promise.resolve().then(async () => {
   console.log("Large data benchmark:");
   await benchmark(70000, 50000);

   console.log("Realdata data benchmark:");
   await benchmark(100000, 100);
   benchTimer.end();

   const timer = Logging.time("Test Timer");
   setTimeout(() => timer.end(), 1000);
});
