import { format } from "jsr:@std/datetime";
import { REST, Routes } from "npm:discord.js";

// Handing options
let VIEW: boolean = true;
for (const arg of Deno.args) {
  switch (arg) {
    case "-V":
      VIEW = false;
      break;
  }
}

// Modell
interface Log {
  type: "mathTime";
  created: string;
}

interface Data {
  title: string;
  log: Log[];
}

// Default data

let data: Data = {
  title: "Hello %c matte!",
  log: [],
};

// Reead and parse data
try {
  const text = await Deno.readTextFile("data.json");
  data = JSON.parse(text) as Data;
} catch (error) {
  if (!(error instanceof Deno.errors.NotFound)) {
    console.log("Create a new data file!");
  }
  console.log("not exists!");
}

// View data
if(VIEW) {
let result = "";
let points = 0;
let week_day = 0;

for (const _log of data.log) {
  result += "🙂";
  points++;
  week_day++;
  if (week_day === 7) {
    result += "\n";
  }
}

result += "\n" + points;
console.log(result);
}

// Manipulate data
console.log(data.title, "color:red");

const answer = prompt("Har du gjort din halvtime matematik? (y)") ?? "";

if (answer === "y") {
  console.log("🎉🎉🎉");
  data.log.push({
    type: "mathTime",
    created: format(new Date(), "yyyy-MM-dd HH:mm:ss"),
  });
} else {
  console.log("🙁");
}

// Save an exit program
const data_text = JSON.stringify(data);
await Deno.writeTextFile("data.json", data_text);
