content: /*
   * BedWars1058 - A bed wars mini-game.
   * Copyright (C) 2021 Andrei Dascălu
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
   *
   * Contact e-mail: andrew.dascalu@gmail.com
   */
  
  package com.andrei1058.bedwars.commands.bedwars.subcmds.regular;
  
  import com.andrei1058.bedwars.BedWars;
  import com.andrei1058.bedwars.api.arena.IArena;
  import com.andrei1058.bedwars.api.command.ParentCommand;
  import com.andrei1058.bedwars.api.command.SubCommand;
  import com.andrei1058.bedwars.api.configuration.ConfigPath;
  import com.andrei1058.bedwars.api.language.Messages;
  import com.andrei1058.bedwars.arena.Arena;
  import com.andrei1058.bedwars.arena.SetupSession;
  import com.andrei1058.bedwars.configuration.Sounds;
  import org.bukkit.command.CommandSender;
  import org.bukkit.command.ConsoleCommandSender;
  import org.bukkit.entity.Player;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import static com.andrei1058.bedwars.api.language.Language.getMsg;
  
  public class CmdJoin extends SubCommand {
  
      public CmdJoin(ParentCommand parent, String name) {
          super(parent, name);
          setPriority(19);
          showInList(false);
          setDisplayInfo(com.andrei1058.bedwars.commands.bedwars.MainCommand.createTC("§6 ▪ §7/"+ com.andrei1058.bedwars.commands.bedwars.MainCommand.getInstance().getName()+" join §e<random/ arena/ groupName>",
                  "/"+getParent().getName()+" "+getSubCommandName(), "§fJoin an arena by name or by group.\n§f/bw join random - join random arena."));
      }
  
      @Override
      public boolean execute(String[] args, CommandSender s) {
          if (s instanceof ConsoleCommandSender) return false;
          Player p = (Player) s;
          if (args.length < 1) {
              s.sendMessage(getMsg(p, Messages.COMMAND_JOIN_USAGE));
              return true;
          }
          if (args[0].equalsIgnoreCase("random")){
              if (!Arena.joinRandomArena(p)){
                  s.sendMessage(getMsg(p, Messages.COMMAND_JOIN_NO_EMPTY_FOUND));
                  Sounds.playSound("join-denied", p);
              } else {
                  Sounds.playSound("join-allowed", p);
              }
              return true;
          }
          if (com.andrei1058.bedwars.commands.bedwars.MainCommand.isArenaGroup(args[0]) || args[0].contains("+")) {
              if (!Arena.joinRandomFromGroup(p, args[0])) {
                  s.sendMessage(getMsg(p, Messages.COMMAND_JOIN_NO_EMPTY_FOUND));
                  Sounds.playSound("join-denied", p);
              } else {
                  Sounds.playSound("join-allowed", p);
              }
              return true;
          } else if (Arena.getArenaByName(args[0]) != null) {
              if (Arena.getArenaByName(args[0]).addPlayer(p, false)){
                  Sounds.playSound("join-allowed", p);
              } else {
                  Sounds.playSound("join-denied", p);
              }
              return true;
          } else if (Arena.getArenaByIdentifier(args[0]) != null) {
              if (Arena.getArenaByIdentifier(args[0]).addPlayer(p, false)){
                  Sounds.playSound("join-allowed", p);
              } else {
                  Sounds.playSound("join-denied", p);
              }
              return true;
          }
          s.sendMessage(getMsg(p, Messages.COMMAND_JOIN_GROUP_OR_ARENA_NOT_FOUND).replace("{name}", args[0]));
          return true;
      }
  
      @Override
      public List<String> getTabComplete() {
          List<String> tab = new ArrayList<>(BedWars.config.getYml().getStringList(ConfigPath.GENERAL_CONFIGURATION_ARENA_GROUPS));
          for (IArena arena : Arena.getArenas()){
              tab.add(arena.getArenaName());
          }
          return tab;
      }
  
      @Override
      public boolean canSee(CommandSender s, com.andrei1058.bedwars.api.BedWars api) {
          if (s instanceof ConsoleCommandSender) return false;
  
          Player p = (Player) s;
          if (Arena.isInArena(p)) return false;
  
          if (SetupSession.isInSetupSession(p.getUniqueId())) return false;
          return hasPermission(s);
      }
  }
  