package me.minidigger.voxelgameslib.commands;

import co.aikar.commands.BaseCommand;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Subcommand;
import co.aikar.commands.annotation.Syntax;
import java.util.Optional;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.extern.java.Log;
import me.minidigger.voxelgameslib.game.Game;
import me.minidigger.voxelgameslib.game.GameHandler;
import me.minidigger.voxelgameslib.game.GameMode;
import me.minidigger.voxelgameslib.lang.Lang;
import me.minidigger.voxelgameslib.lang.LangKey;
import me.minidigger.voxelgameslib.user.User;

@Singleton
@Log
@SuppressWarnings("JavaDoc") // commands don't need javadoc, go read the command's descriptions
@CommandAlias("game")
public class GameCommands extends BaseCommand {

  @Inject
  private GameHandler gameHandler;

  @Subcommand("help")
  @CommandAlias("game")
  @CommandPermission("%user")
  public void game(User sender) {
    // todo game command
  }

  @Subcommand("list")
  @CommandPermission("%user")
  public void gameList(User sender) {
    Lang.msg(sender, LangKey.GAME_GAMELIST_HEADER);
    for (Game game : gameHandler.getGames()) {
      Lang.msg(sender, LangKey.GAME_GAMELIST_ENTRY,
          game.getUuid().toString().split("-")[0], game.getGameMode().getName(),
          game.getActivePhase().getName(), game.getPlayers().size(), game.getSpectators().size());
    }
    Lang.msg(sender, LangKey.GAME_GAMELIST_FOOTER);
  }

  @Subcommand("modes")
  @CommandPermission("%user")
  public void gameListModes(User sender) {
    StringBuilder sb = new StringBuilder();
    gameHandler.getGameModes().forEach(m -> sb.append(m.getName()).append(", "));
    sb.replace(sb.length() - 2, sb.length(), ".");
    Lang.msg(sender, LangKey.GAME_GAMEMODE_INSTALLED, sb.toString());
  }

  @Subcommand("start")
  @CommandCompletion("@gamemodes")
  @Syntax("<mode> - the mode you want to start")
  @CommandPermission("%premium")
  public void gameStart(User sender, GameMode mode) {
    Game game = gameHandler.startGame(mode);

    if (game.getActivePhase().isRunning()) {
      game.join(sender);
      Lang.msg(sender, LangKey.GAME_GAME_STARTED);
    } else {
      Lang.msg(sender, LangKey.GAME_COULD_NOT_START);
    }
  }

  @Subcommand("join")
  @CommandCompletion("@gamemodes")
  @Syntax("<mode> - the mode you want to start")
  @CommandPermission("%user")
  public void gameJoin(User sender, GameMode mode) {
    Optional<Game> game = gameHandler.findGame(sender, mode);
    if (game.isPresent()) {
      game.get().join(sender);
    } else {
      Lang.msg(sender, LangKey.GAME_NO_GAME_FOUND);
    }
  }

  @Subcommand("leave")
  @CommandPermission("%user")
  public void gameLeave(User sender) {
    // todo game leave command
  }

  @Subcommand("skip-phase")
  @CommandPermission("%admin")
  public void skipPhase(User sender) {
    // todo skip phase command
    // need to access the User's game via object

    /*
    if (getGame().isPlaying(arguments.getSender()) || getGame()
        .isSpectating(arguments.getSender())) {
      log.finer("skip " + getName());
      getGame().endPhase();
    }
    */
  }
}

@@ -330,16 +330,39 @@
 Syntax;%0A
+import java.util.List;%0A
 import j
@@ -3013,163 +3013,288 @@
 nder
-) %7B%0A    // todo skip phase command%0A    // need to access the User's game via object%0A%0A    /*%0A    if (getGame().isPlaying(arguments.getSender()) %7C%7C
+, @co.aikar.commands.annotation.Optional Integer id) %7B%0A    // todo skip phase command%0A    List%3CGame%3E games = gameHandler.getGames(sender.getUuid(), true);%0A    if (id == null) %7B%0A      if (games.size() %3E 1) %7B%0A        //TODO msg%0A      %7D else %7B%0A        log.finer(%22skip %22 +
  get
-G
+N
 ame()
+);
 %0A   
@@ -3302,48 +3302,131 @@
     
-.isSpectating(arguments.getSender())) %7B%0A
+games.get(0).endPhase();%0A      %7D%0A    %7D else %7B%0A      if (games.size() %3E id %7C%7C id %3C 0) %7B%0A        //TODO msg%0A      %7D else %7B%0A  
     
@@ -3469,16 +3469,22 @@
     
-getGame(
+  games.get(id
 ).en
@@ -3501,16 +3501,17 @@
     
+  
 %7D%0A    
-*/
+%7D
 %0A  %7D

implement game skip