import { EventEmitter } from "events";
import { KomandNaExport, Komand, RunFunkce, SRecord } from "./types";
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { oddiakritikovat } from "./utils";

const ex = module.exports;
const cesta = join(__dirname, "../../res/");
const customKomandi: SRecord<{ text: string; owner: string; }> = JSON.parse(readFileSync(`${cesta}komandi.json`).toString());
const customAliasy: SRecord<{ cmd: string; owner: string; }> = JSON.parse(readFileSync(`${cesta}aliasi.json`).toString());
const baseNaSend: KomandNaExport[] = [
  { name: "naucse", arg: "<jméno nového komandu> <obsah nového komandu>" },
  { name: "zapomen", arg: "jméno komandu" },
  { name: "naucsealias", arg: "<jméno nového aliasu> <jméno existujícího komandu nebo aliasu>", als: ["nausea"] },
  { name: "zapomenalias", arg: "jméno aliasu", als: ["zapomena"] }
];
export let [cKomandy, cAliasy, naSend] = save(true);
export const emitter = new EventEmitter();

function save(): void;
function save(nesend: true): [SRecord<Komand>, SRecord<string>, KomandNaExport[]];
function save(nesend?: true) {
  const cKomandi: SRecord<Komand> = {};
  const cAliasi: SRecord<string> = { naucsea: "naucsealias", zepomena: "zapomenalias" };
  const naSend = [...baseNaSend];
  Object.keys(customKomandi).forEach(c => {
    cKomandi[c] = { run: customKomandi[c].text };
    naSend.push({ name: c });
  });
  Object.keys(customAliasy).forEach(a => {
    cAliasi[a] = customAliasy[a].cmd;
  });
  if (nesend) return [cKomandi, cAliasi, naSend];
  cKomandy = cKomandi;
  emitter.emit("komandi", cKomandy, cAliasi, naSend);
  writeFileSync(`${cesta}komandi.json`, JSON.stringify(customKomandi));
  writeFileSync(`${cesta}aliasi.json`, JSON.stringify(customAliasy));
}

export const naucse: RunFunkce = (mes, arg) => {
  const args = arg.split(" ").filter(v => v != "");
  if (args.length == 0) return "a co se mam jako naucit";
  if (args.length == 1) return "a co bich nato mnel rict????";

  args[0] = oddiakritikovat(args[0].toLowerCase());
  if (ex.realKomandy[args[0]] || customKomandi[args[0]] || ex.realAliasy[args[0]] || customAliasy[args[0]]) return "tuten komand uz ale egzistuje";

  customKomandi[args[0]] = { text: args.splice(1).join(" "), owner: mes.author.id };
  save();
  return "jo";
};

export const zapomen: RunFunkce = (mes, arg) => {
  const com = oddiakritikovat(arg.toLowerCase());

  if (ex.realKomandy[com]) return "tuten komand se neda smazat ti smazko";
  const cmd = customKomandi[com];
  if (!cmd) return `komand "${arg}" neznam`;
  if (cmd.owner != mes.author.id) return "tuto ael neni tvuj komand toxikale zkurvenej";

  delete customKomandi[com];
  save();
  return "jo";
};

export const kohoje: RunFunkce = (_, arg) => {
  const cmdName = oddiakritikovat(arg.toLowerCase());
  const cmd = customKomandi[cmdName] ?? customAliasy[cmdName];

  if (!cmd) return `"${cmdName}" nen ani komand an alijas`;

  return `vlastnitel je <@${cmd.owner}>`;
};

export const naucsealias: RunFunkce = (mes, arg) => {
  const args = arg.split(" ").filter(v => v != "").map(e => oddiakritikovat(e).toLowerCase());

  if (args.length == 0) return "a co se mam jako naucit";
  if (args.length == 1) return "a co to ma znamenat????";
  if (ex.realKomandy[args[0]] || customKomandi[args[0]] || ex.realAliasy[args[0]] || customAliasy[args[0]]) return "tuto uz ale egzistuje";

  let jmeno: string;
  if (!ex.realKomandy[args[1]] && !ex.realAliasy[args[1]]) {
    if (!customKomandi[args[1]] && !customAliasy[args[1]]) return `nejze "${args[1]}" neni realnej ani vlastni komand ani alias`;
    jmeno = customKomandi[args[1]] ? args[1] : customAliasy[args[1]].cmd;
  }
  else jmeno = (ex.realKomandy[args[1]]) ? args[1] : ex.realAliasy[args[1]];

  customAliasy[args[0]] = { cmd: jmeno, owner: mes.author.id };
  save();
  return "jo";
};

export const zapomenalias: RunFunkce = (mes, arg) => {
  const al = oddiakritikovat(arg.toLowerCase());

  if (ex.realAliasy[al]) return "tutn alijas se ale neda smazat ty smazenice";
  const als = customAliasy[al];
  if (!als) return `alijas "${al}" neznam`;
  if (als.owner != mes.author.id) return "tuto ael neni tvuj alijas toxikale zkurvenej";

  delete customAliasy[al];
  save();
  return "jo";
};
