package com.bwfcwalshy.flarebot.commands.general;

import com.bwfcwalshy.flarebot.FlareBot;
import com.bwfcwalshy.flarebot.MessageUtils;
import com.bwfcwalshy.flarebot.commands.Command;
import com.bwfcwalshy.flarebot.commands.CommandType;
import com.bwfcwalshy.flarebot.objects.PlayerCache;
import net.dv8tion.jda.core.entities.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Collectors;

public class UserInfoCommand implements Command {

    private FlareBot flareBot = FlareBot.getInstance();
    private DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("MMMM yyyy HH:mm:ss");

    @Override
    public void onCommand(User sender, TextChannel channel, Message message, String[] args, Member member) {
        String id;
        if (args.length != 1)
            id = sender.getId();
        else id = args[0].replaceAll("[^0-9]", "");
        User user = FlareBot.getInstance().getUserByID(id);
        if (user == null) {
            MessageUtils.sendErrorMessage("We cannot find that user!", channel);
            return;
        }
        PlayerCache cache = flareBot.getPlayerCache(id);
        channel.sendMessage(MessageUtils.getEmbed(sender).addField("User Info", "User: " + user.getName() + "#" + user.getDiscriminator()
                    + "\nID: " + user.getId() + "\n" +
                    "Avatar: " + (user.getEffectiveAvatarUrl() != null ? "[`link`](" + user.getEffectiveAvatarUrl() + ')' : "None") + "\n" +
                    "Default Avatar: [`link`](" + MessageUtils.getDefaultAvatar(sender) + ')', true)
                    .addField("General Info",
                            "Servers: " + FlareBot.getInstance().getGuilds().stream().filter(guild -> guild.getMemberById(id) != null).count() + " shared\n" +
                                    "Roles: " + channel.getGuild().getMember(user).getRoles().stream()
                                    .map(Role::getName).collect(Collectors.joining(", ")), true)
                    .addField("Time Data", "Created: " + formatTime(LocalDateTime.from(user.getCreationTime())) + "\n" +
                            "Joined: " + formatTime(LocalDateTime.from(channel.getGuild().getMember(user).getJoinDate())) + "\n" +
                            "Last Seen: " + (cache.getLastSeen() == null ? "Unknown" : formatTime(cache.getLastSeen())) + "\n" +
                            "Last Spoke: " + (cache.getLastMessage() == null ? "Unknown" : formatTime(cache.getLastMessage())), false)
                    .setThumbnail(MessageUtils.getAvatar(user)).build()).queue();
    }

    @Override
    public String getCommand() {
        return "userinfo";
    }

    @Override
    public String getDescription() {
        return "Get information about a user";
    }

    @Override
    public CommandType getType() {
        return CommandType.GENERAL;
    }

    private String formatTime(LocalDateTime time) {
        return time.getDayOfMonth() + getDayOfMonthSuffix(time.getDayOfMonth()) + " " + time.format(timeFormat) + " UTC";
    }

    private String getDayOfMonthSuffix(final int n) {
        if (n < 1 || n > 31) throw new IllegalArgumentException("illegal day of month: " + n);
        if (n >= 11 && n <= 13) {
            return "th";
        }
        switch (n % 10) {
            case 1:
                return "st";
            case 2:
                return "nd";
            case 3:
                return "rd";
            default:
                return "th";
        }
    }
}

@@ -1087,32 +1087,85 @@
 turn;%0A        %7D%0A
+        member = channel.getGuild().getMember(user);%0A
         PlayerCa
@@ -1201,24 +1201,24 @@
 rCache(id);%0A
-
         chan
@@ -1359,20 +1359,16 @@
         
-    
 + %22%5CnID:
@@ -1386,36 +1386,32 @@
 etId() + %22%5Cn%22 +%0A
-    
                 
@@ -1547,20 +1547,16 @@
         
- 
-   
 %22Default
@@ -1636,36 +1636,32 @@
                 
-    
 .addField(%22Gener
@@ -1694,20 +1694,16 @@
         
- 
-   
 %22Servers
@@ -1857,20 +1857,16 @@
         
-    
 %22Roles: 
@@ -1956,20 +1956,16 @@
         
-    
 .map(Rol
@@ -2009,24 +2009,513 @@
 g(%22, %22))
+ +%0A                                (member.getGame() != null ? %22%5CnStatus%22 +%0A                                        (member.getUser().isBot() ? %22 (Current Shard)%22 : %22%22) + %22: %22 +%0A                                        (member.getGame().getUrl() == null ? %22%60%22 + member.getGame().getName() + %22%60%22 :%0A                                                String.format(%22%5B%60%25s%60%5D(%25s)%22, member.getGame().getName(),%0A                                                        member.getGame().getUrl())) : %22%22)
 , true)%0A
         
@@ -2498,36 +2498,32 @@
 )) : %22%22), true)%0A
-    
                 
@@ -2647,20 +2647,16 @@
         
-    
 %22Joined:
@@ -2742,36 +2742,32 @@
 te())) + %22%5Cn%22 +%0A
-    
                 
@@ -2895,28 +2895,24 @@
             
-    
 %22Last Spoke:
@@ -3006,20 +3006,16 @@
  false)%0A
-    
         

Add status to user info