/*
----------------------------------
Creator: Mafly
Discord:
https://discord.gg/BUFxackwWb
Youtube: MaFly
https://www.youtube.com/c/MaFly16

Website:
https://mcblibrary.com/user/mafly
----------------------------------
*/

import { system, world } from "@minecraft/server";
import { config } from "../command/config.js";

const prefix = config.prefix;

export class CommandBuilder {
  constructor() {
    this._registrationInformation = [];
  }

  register(register, callback) {
    let form = {
      name: register.name.toLowerCase(),
      aliases: register.aliases
        ? register.aliases.map((v) => v.toLowerCase())
        : [],
      description: register.description,
      usage: register.usage || null,
      admin: register.admin || false,
      permission: register.permission || null,
      callback,
    };
    this._registrationInformation.push(form);
  }

  unregister(name) {
    const index = this._registrationInformation.findIndex(
      (element) => element.name.toLowerCase() === name.toLowerCase()
    );
    if (index !== -1) {
      this._registrationInformation.splice(index, 1);
    }
  }

  msg(text) {
    for (const player of world.getPlayers()) {
      player.sendMessage(text);
    }
  }

  error(text) {
    for (const player of world.getPlayers()) {
      player.sendMessage(`§cSYSTEM §7>> §cError! ${text}`);
    }
  }

  valid(command, player) {
    const cmd = this.getRegistration(command.toLowerCase());
    if (!cmd) {
      system.run(() => {
        player.sendMessage(
          `§cSYSTEM §7>> §cCommand §g${prefix}${command} §cnot found. Please check that command using §g${prefix}help §ccommand.`
        );
        player.playSound("note.bass");
      });
      return false;
    }
    if (cmd.admin && !player.hasTag("admin")) {
      system.run(() => {
        player.sendMessage(
          `§cSYSTEM §7>> §cYou do not have permission to use the §g${prefix}${command} §ccommand.`
        );
        player.playSound("note.bass");
      });
      return false;
    }
    if (cmd.permission && !player.hasTag("admin")) {
      const permissions = Array.isArray(cmd.permission)
        ? cmd.permission
        : [cmd.permission];
      const hasPermission = permissions.some((perm) => player.hasTag(perm));
      if (!hasPermission) {
        system.run(() => {
          player.sendMessage(
            `§cSYSTEM §7>> §cYou do not have permission to use the §g${prefix}${command} §ccommand.`
          );
          player.playSound("note.bass");
        });
        return false;
      }
    }
    return true;
  }

  get() {
    const commands = [];
    this._registrationInformation.forEach((element) => {
      if (!element.private) {
        commands.push(element.name);
      }
    });
    return commands;
  }

  getAllRegistration() {
    return this._registrationInformation;
  }

  getRegistration(name) {
    return this._registrationInformation.find(
      (element) =>
        element.name.toLowerCase() === name ||
        (element.aliases && element.aliases.includes(name))
    );
  }
}

export const CommandBuild = new CommandBuilder();
