package io.github.aquerr.eaglefactions.commands;

import io.github.aquerr.eaglefactions.EagleFactions;
import io.github.aquerr.eaglefactions.PluginInfo;
import io.github.aquerr.eaglefactions.entities.Faction;
import io.github.aquerr.eaglefactions.logic.FactionLogic;
import io.github.aquerr.eaglefactions.services.PlayerService;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.User;
import org.spongepowered.api.service.pagination.PaginationList;
import org.spongepowered.api.service.pagination.PaginationService;
import org.spongepowered.api.service.user.UserStorageService;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * Created by Aquerr on 2017-08-03.
 */
public class InfoCommand implements CommandExecutor
{
    @Override
    public CommandResult execute(CommandSource source, CommandContext context) throws CommandException
    {
        String factionName = context.<String>getOne("faction name").get();

            if(FactionLogic.getFactions().contains(factionName))
            {
                Faction faction = FactionLogic.getFaction(factionName);

                List<Text> factionInfo = new ArrayList<>();

                String membersList = "";

                if(!faction.Members.isEmpty() && faction.Members != null)
                {
                    for (String member: faction.Members)
                    {
                        EagleFactions.getEagleFactions().getLogger().info("Adding a member...");

                        membersList += member + ", ";
                    }
                }

                String officersList = "";

                if(!faction.Officers.isEmpty() && faction.Officers != null)
                {
                    for (String officer: faction.Officers)
                    {
                        EagleFactions.getEagleFactions().getLogger().info("Adding an officer...");
                        officersList += officer + ", ";
                    }
                }

                String alliancesList = "";

                if(!faction.Alliances.isEmpty() && faction.Alliances != null)
                {
                    for (String alliance: faction.Alliances)
                    {
                        alliancesList += alliance + ", ";
                    }
                }

                String enemiesList = "";

                if(!faction.Enemies.isEmpty() && faction.Enemies != null)
                {
                    for (String enemy: faction.Enemies)
                    {
                        enemiesList += enemy + ", ";
                    }
                }



                Text info = Text.builder()
                        .append(Text.of(TextColors.AQUA, "Name: ", TextColors.GOLD, faction.Name + "\n"))
                        .append(Text.of(TextColors.AQUA, "Leader: ", TextColors.GOLD, PlayerService.getPlayerName(faction.Leader).get() + "\n"))
                        .append(Text.of(TextColors.AQUA, "Officers: ", TextColors.GOLD, officersList + "\n"))
                        .append(Text.of(TextColors.AQUA, "Alliances: ", TextColors.BLUE, alliancesList + "\n"))
                        .append(Text.of(TextColors.AQUA, "Enemies: ", TextColors.RED, enemiesList + "\n"))
                        .append(Text.of(TextColors.AQUA, "Members: ", TextColors.GREEN, membersList + "\n"))
                        .append(Text.of(TextColors.AQUA, "Power: ", TextColors.GOLD, faction.Power))
                        .build();

                factionInfo.add(info);

                PaginationService paginationService = Sponge.getServiceManager().provide(PaginationService.class).get();
                PaginationList.Builder paginationBuilder = paginationService.builder().title(Text.of(TextColors.GREEN, "Faction Info")).contents(factionInfo);
                paginationBuilder.sendTo(source);

                CommandResult.success();

            }
            else
            {
                source.sendMessage(Text.of(PluginInfo.ErrorPrefix, TextColors.RED, "There is no faction called ", TextColors.GOLD, factionName));
            }

        return CommandResult.success();
    }
}

@@ -1681,33 +1681,32 @@
 mbersList = %22%22;%0A
-%0A
                 
@@ -1888,87 +1888,60 @@
     
-EagleFactions.getEagleFactions().getLogger().info(%22Adding a member...%22);%0A%0A     
+membersList += member + %22, %22;%0A                    %7D%0A
     
@@ -1963,33 +1963,32 @@
 membersList 
-+
 = member
  + %22, %22;%0A   
@@ -1979,38 +1979,53 @@
 mber
- + %22, %22;%0A                    %7D
+sList.substring(0, membersList.length() - 2);
 %0A   
@@ -2074,33 +2074,32 @@
 icersList = %22%22;%0A
-%0A
                 
@@ -2285,76 +2285,31 @@
     
-EagleFactions.getEagleFactions().getLogger().info(%22Adding an
+officersList +=
  officer
 ...%22
@@ -2308,19 +2308,39 @@
 icer
-...%22)
+ + %22, %22
 ;%0A    
+                %7D%0A
     
@@ -2360,33 +2360,32 @@
    officersList 
-+
 = officer + %22, %22
@@ -2381,38 +2381,54 @@
 icer
- + %22, %22;%0A                    %7D
+sList.substring(0, officersList.length() - 2);
 %0A   
@@ -2478,33 +2478,32 @@
 ancesList = %22%22;%0A
-%0A
                 
@@ -2737,32 +2737,124 @@
               %7D%0A
+                    alliancesList = alliancesList.substring(0, alliancesList.length() - 2);%0A
                 
@@ -2897,17 +2897,16 @@
 t = %22%22;%0A
-%0A
         
@@ -3158,18 +3158,102 @@
         
+   enemiesList = enemiesList.substring(0, enemiesList.length() - 2);%0A                
 %7D
-%0A
 %0A%0A%0A     

Remove last comma from all lists in InfoCommand