import { format } from "util";
let debugMode = !1;
let winScore = 4999;
let playScore = 99;
export async function before(m) {
  let ok;
  let isWin = !1;
  let isTie = !1;
  let isSurrender = !1;
  if (!db.data.game.tictactoe) db.data.game.tictactoe = {};
  let room = Object.values(db.data.game.tictactoe).find(
    (room) =>
      room.id &&
      room.game &&
      room.state &&
      room.id.startsWith("tictactoe") &&
      [room.game.playerX, room.game.playerO].includes(m.sender) &&
      room.state == "PLAYING",
  );
  if (room) {
    if (!/^([1-9]|(me)?nyerah|surr?ender)$/i.test(m.text)) return !0;
    isSurrender = !/^[1-9]$/.test(m.text);
    if (m.sender !== room.game.currentTurn) {
      if (!isSurrender) return !0;
    }
    if (debugMode)
      m.reply(
        "[DEBUG]\n" +
          require("util").format({
            isSurrender: isSurrender,
            text: m.text,
          }),
      );
    if (
      !isSurrender &&
      1 >
        (ok = room.game.turn(
          m.sender === room.game.playerO,
          parseInt(m.text) - 1,
        ))
    ) {
      m.reply(
        {
          "-3": "Game has ended",
          "-2": "Invalid",
          "-1": "Position Invalid",
          0: "Position Invalid",
        }[ok],
      );
      return !0;
    }
    if (m.sender === room.game.winner) isWin = true;
    else if (room.game.board === 511) isTie = true;
    let arr = room.game.render().map((v) => {
      return {
        X: "❌",
        O: "⭕",
        1: "1️⃣",
        2: "2️⃣",
        3: "3️⃣",
        4: "4️⃣",
        5: "5️⃣",
        6: "6️⃣",
        7: "7️⃣",
        8: "8️⃣",
        9: "9️⃣",
      }[v];
    });
    if (isSurrender) {
      room.game._currentTurn = m.sender === room.game.playerX;
      isWin = true;
    }
    let winner = isSurrender ? room.game.currentTurn : room.game.winner;
    let str = `
${arr.slice(0, 3).join("")}
${arr.slice(3, 6).join("")}
${arr.slice(6).join("")}
${isWin ? `@${winner.split("@")[0]} Win! (+${winScore} XP)` : isTie ? `Game over (+${playScore} XP)` : `turn ${["❌", "⭕"][1 * room.game._currentTurn]} (@${room.game.currentTurn.split("@")[0]})`}
❌: @${room.game.playerX.split("@")[0]}
⭕: @${room.game.playerO.split("@")[0]}
Type *giveup* for giveup
Room ID: ${room.id}
`.trim();
    let users = db.data.users;
    if ((room.game._currentTurn ^ isSurrender ? room.x : room.o) !== m.chat)
      room[room.game._currentTurn ^ isSurrender ? "x" : "o"] = m.chat;
    const btn = isTie ? ["TicTacToe", "/ttt"] : ["giveup", "giveup"];
    if (room.x !== room.o)
      await this.reply(room.x, str, m, {
        mentions: this.parseMention(str),
      });
    await this.reply(room.o, str, m, {
      mentions: this.parseMention(str),
    });
    if (isTie || isWin) {
      users[room.game.playerX].exp += playScore;
      users[room.game.playerO].exp += playScore;
      if (isWin) users[winner].exp += winScore - playScore;
      if (debugMode) m.reply("[DEBUG]\n" + format(room));
      delete db.data.game.tictactoe[room.id];
    }
  }
  return !0;
}
