package stream.flarebot.flarebot.commands.general;

import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import stream.flarebot.flarebot.FlareBot;
import stream.flarebot.flarebot.commands.Command;
import stream.flarebot.flarebot.commands.CommandType;
import stream.flarebot.flarebot.objects.GuildWrapper;
import stream.flarebot.flarebot.objects.PlayerCache;
import stream.flarebot.flarebot.util.GeneralUtils;
import stream.flarebot.flarebot.util.MessageUtils;

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

public class UserInfoCommand implements Command {

    private FlareBot flareBot = FlareBot.getInstance();

    @Override
    public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
        User user;
        if (args.length == 0)
            user = sender;
        else {
            if (getPermissions(channel).hasPermission(member, "flarebot.userinfo.other"))
                user = GeneralUtils.getUser(MessageUtils.getMessage(args, 0), true);
            else {
                MessageUtils.sendErrorMessage("You need the `flarebot.userinfo.other` permission to userinfo other users!",
                        channel);
                return;
            }
        }

        if (user == null) {
            MessageUtils.sendErrorMessage("We cannot find that user!", channel);
            return;
        }
        String id = user.getId();
        if (channel.getGuild().getMember(user) == null) {
            Guild memberGuild = FlareBot.getInstance().getGuilds().stream().filter(g -> g.getMemberById(user.getId()) != null)
                    .findFirst().orElse(null);
            if (memberGuild == null)
                member = null;
            else
                memberGuild.getMember(user);
        }
        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(g -> g.getMemberById(id) != null)
                                .count() + " shared\n" +
                                "Roles: " + (channel.getGuild()
                                .getMemberById(id) == null ? "The user is not in this server." : channel
                                .getGuild().getMember(user).getRoles().stream()
                                .map(Role::getName).collect(Collectors.joining(", "))) +
                                (member != null && member.getGame() != null ? "\nStatus" +
                                        (member.getUser()
                                                .isBot() ? " (Current Shard)" : "") + ": " +
                                        (member.getGame().getUrl() == null ? "`" + member
                                                .getGame().getName() + "`" :
                                                String.format("[`%s`](%s)", member.getGame()
                                                                .getName(),
                                                        member.getGame().getUrl())) : ""), true)
                .addField("Time Data", "Created: " + GeneralUtils
                        .formatTime(LocalDateTime.from(user.getCreationTime())) + "\n" +
                        "Joined: " + (channel.getGuild()
                        .getMember(user) == null ? "The user is not in this server."
                        : GeneralUtils.formatTime(LocalDateTime
                        .from(channel.getGuild().getMember(user).getJoinDate()))) + "\n" +
                        "Last Seen: " + (cache.getLastSeen() == null ? "Unknown" : GeneralUtils
                        .formatTime(cache.getLastSeen())) + "\n" +
                        "Last Spoke: " + (cache.getLastMessage() == null ? "Unknown" : GeneralUtils
                        .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 String getUsage() {
        return "`{%}userinfo [userID]` - Views your user info [or info for another user].";
    }

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

    @Override
    public String[] getAliases() {
        return new String[]{"stalk"};
    }
}

@@ -1669,31 +1669,18 @@
     
-if (channel.getGuild().
+Member tar
 getM
@@ -1688,37 +1688,25 @@
 mber
-(user)
  =
-=
  null
-) %7B%0A   
+;%0A
         
  Gui
@@ -1705,51 +1705,19 @@
     
- Guild memberGuild = FlareBot.getInstance()
+if (channel
 .get
@@ -1725,35 +1725,11 @@
 uild
-s
 ().
-stream().filter(g -%3E g.
 getM
@@ -1737,28 +1737,16 @@
 mber
-ById
 (user
-.getId()
 ) 
-!
+=
 = nu
@@ -1765,155 +1765,41 @@
     
-        .findFirst().orElse(null);%0A            if (memberGuild == null)%0A                member = null;%0A            else%0A                member
+targetMember = channel.get
 Guild
+()
 .get
@@ -1812,33 +1812,32 @@
 (user);%0A        
-%7D
 %0A        PlayerC
@@ -2696,60 +2696,11 @@
  + (
-channel.getGuild()%0A                                .
+tar
 getM
@@ -2704,24 +2704,16 @@
 etMember
-ById(id)
  == null
@@ -2751,23 +2751,16 @@
 ver.%22 : 
-channel
 %0A       
@@ -2784,28 +2784,32 @@
         
-.getGuild().
+             tar
 getMembe
@@ -2805,30 +2805,24 @@
 targetMember
-(user)
 .getRoles().
@@ -2948,25 +2948,31 @@
            (
-m
+targetM
 ember != nul
@@ -2976,17 +2976,23 @@
 null && 
-m
+targetM
 ember.ge
@@ -3055,33 +3055,39 @@
                (
-m
+targetM
 ember.getUser()%0A
@@ -3220,17 +3220,23 @@
        (
-m
+targetM
 ember.ge
@@ -3428,17 +3428,23 @@
 %5D(%25s)%22, 
-m
+targetM
 ember.ge
@@ -3583,17 +3583,23 @@
         
-m
+targetM
 ember.ge
@@ -3827,52 +3827,11 @@
  + (
-channel.getGuild()%0A                        .
+tar
 getM
@@ -3827,38 +3827,32 @@
  + (targetMember
-(user)
  == null ? %22The 

Fix userinfo? Maybe