/*
 * Copyright (C) 2008-2020, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.inject.Inject;

import com.juick.model.CommandResult;
import com.juick.model.Message;
import com.juick.model.Tag;
import com.juick.model.TagStats;
import com.juick.model.User;
import com.juick.service.MessagesService;
import com.juick.service.ChatService;
import com.juick.service.PrivacyQueriesService;
import com.juick.service.ShowQueriesService;
import com.juick.service.StorageService;
import com.juick.service.SubscriptionService;
import com.juick.service.TagService;
import com.juick.service.UserService;
import com.juick.service.activities.DeleteMessageEvent;
import com.juick.service.component.PingEvent;
import com.juick.service.component.SystemEvent;
import com.juick.util.HttpUtils;
import com.juick.util.MessageUtils;
import com.juick.util.TagUtils;
import com.juick.util.annotation.UserCommand;
import com.juick.util.formatters.PlainTextFormatter;
import com.juick.www.WebApp;
import com.juick.www.api.SystemActivity;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;

/**
 *
 * @author ugnich
 */
public class CommandsManager {
    private static final Logger logger = LoggerFactory.getLogger(CommandsManager.class);
    @Inject
    private MessagesService messagesService;
    @Inject
    private UserService userService;
    @Inject
    private TagService tagService;
    @Inject
    private ChatService chatService;
    @Inject
    private ShowQueriesService showQueriesService;
    @Inject
    private PrivacyQueriesService privacyQueriesService;
    @Inject
    private SubscriptionService subscriptionService;
    @Inject
    private StorageService storageService;
    @Inject
    private ApplicationEventPublisher applicationEventPublisher;
    @Inject
    private WebApp webApp;
    @Value("${web_domain:localhost}")
    private String webDomain;
    @Inject
    private ActivityPubManager activityPubManager;

    public CommandResult processCommand(@Nonnull User user, String data, @Nonnull URI attachment) throws Exception {
        if (!user.isAnonymous()) {
            userService.updateLastSeen(user);
        }
        String strippedData = StringUtils.stripStart(data, null);
        if (strippedData.startsWith("?OTR")) {
            return CommandResult.fromString("?OTR Error: we are not using OTR");
        }
        String input = StringEscapeUtils.unescapeHtml4(MessageUtils.stripNonSafeUrls(strippedData));
        Optional<Method> cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream()
                .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(),
                        m.getAnnotation(UserCommand.class).patternFlags()).matcher(input).matches())
                .findFirst();
        if (cmd.isPresent()) {
            Matcher matcher = Pattern.compile(cmd.get().getAnnotation(UserCommand.class).pattern(),
                    cmd.get().getAnnotation(UserCommand.class).patternFlags()).matcher(input);
            List<String> groups = new ArrayList<>();
            while (matcher.find()) {
                for (int i = 1; i <= matcher.groupCount(); i++) {
                    groups.add(matcher.group(i));
                }
            }
            CommandResult commandResult = (CommandResult) getClass().getMethod(cmd.get().getName(), User.class, URI.class, String[].class)
                    .invoke(this, user, attachment, groups.toArray(new String[0]));
            if (StringUtils.isNotEmpty(commandResult.getText())) {
                return commandResult;
            }
        }
        Pair<String, Set<Tag>> tags = tagService.fromString(input);
        if (tags.getRight().size() > 5) {
            return CommandResult.fromString("Sorry, 5 tags maximum.");
        }
        // new message
        String body = tags.getLeft().trim();
        if (body.length() > 4096) {
            return CommandResult.fromString("Sorry, 4096 characters maximum.");
        }
        boolean haveAttachment = StringUtils.isNotEmpty(attachment.toString());
        String attachmentFName = null;
        String attachmentType = null;
        if (haveAttachment) {
             attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost()
                    : HttpUtils.downloadImage(attachment.toURL(), storageService.getTemporaryDirectory()).getHost();
             attachmentType = attachmentFName.substring(attachmentFName.length() - 3);
        }
        if (StringUtils.isEmpty(body) && !haveAttachment) {
            return CommandResult.fromString("Empty message");
        }
        int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags.getRight());
        if (haveAttachment) {
            String fname = String.format("%d.%s", mid, attachmentType);
            storageService.saveImageWithPreviews(attachmentFName, fname);
        }
        Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new);
        msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser()));
        if (!user.isAnonymous()) {
            subscriptionService.subscribeMessage(msg, user);
        }

        applicationEventPublisher.publishEvent(new SystemEvent(this, SystemActivity.read(user, msg)));
        applicationEventPublisher.publishEvent(new SystemEvent(this,
                SystemActivity.message(user, msg, subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg))));
        return CommandResult.build(msg,
                "New message posted.\n#" + msg.getMid() + " https://juick.com/m/" + msg.getMid(),
                String.format("<a href='%s'>New message</a> posted", PlainTextFormatter.formatUrl(msg)));
    }

    @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "PING - returns you a PONG")
    public CommandResult commandPing(User user, URI attachment, String[] input) {
        applicationEventPublisher.publishEvent(new PingEvent(this, user));
        return CommandResult.fromString("PONG");
    }

    @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "HELP - returns this help message")
    public CommandResult commandHelp(User user, URI attachment, String[] input) {
        return CommandResult.fromString(Arrays.stream(getClass().getDeclaredMethods())
                .filter(m -> m.isAnnotationPresent(UserCommand.class))
                .map(m -> m.getAnnotation(UserCommand.class).help())
                .collect(Collectors.joining("\n")));
    }

    @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "LOGIN - log in to Juick website")
    public CommandResult commandLogin(User user_from, URI attachment, String[] input) {
        return CommandResult.fromString("http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid()));
    }
    @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username")
    public CommandResult commandPM(User user_from, URI attachment, String... arguments) {
        String body = arguments[1];

        User user_to = userService.getUserByName(arguments[0]);
        user_to.setAvatar(webApp.getAvatarUrl(user_to));
        if (!user_to.isAnonymous()) {
            if (!userService.isInBLAny(user_to.getUid(), user_from.getUid())) {
                if (chatService.createMessage(user_from.getUid(), user_to.getUid(), body)) {
                    Message jmsg = new Message();
                    jmsg.setUser(user_from);
                    jmsg.setTo(user_to);
                    jmsg.setText(body);
                    applicationEventPublisher.publishEvent(new SystemEvent(this,
                            SystemActivity.message(user_from, jmsg, Collections.singletonList(user_to))));
                    return CommandResult.fromString("Private message sent");
                }
            }
        }
        return CommandResult.fromString("Error");
    }
    @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "BL - Show your blacklist")
    public CommandResult commandBLShow(User user_from, URI attachment, String... arguments) {
        List<User> blusers = userService.getUserBLUsers(user_from.getUid());
        List<String> bltags = tagService.getUserBLTags(user_from.getUid());

        StringBuilder txt = new StringBuilder(StringUtils.EMPTY);
        if (bltags.size() > 0) {
            for (String bltag : bltags) {
                txt.append("*").append(bltag).append("\n");
            }

            if (blusers.size() > 0) {
                txt.append("\n");
            }
        }
        if (blusers.size() > 0) {
            for (User bluser : blusers) {
                txt.append("@").append(bluser.getName()).append("\n");
            }
        }
        if (txt.length() == 0) {
            txt = new StringBuilder("You don't have any users or tags in your blacklist.");
        }

        return CommandResult.fromString(txt.toString());
    }

    @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages")
    public CommandResult commandLast(User user_from, URI attachment, String... arguments) {
        return CommandResult.fromString("Last messages:\n"
                + printMessages(user_from, messagesService.getAll(user_from.getUid(), 0), true));
    }

    @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs")
    public CommandResult commandUsers(User user_from, URI attachment, String... arguments) {
        StringBuilder msg = new StringBuilder();
        msg.append("Recommended blogs");
        List<String> recommendedUsers = showQueriesService.getRecommendedUsers(user_from);
        if (recommendedUsers.size() > 0) {
            for (String user : recommendedUsers) {
                msg.append("\n@").append(user);
            }
        } else {
            msg.append("\nNo recommendations now. Subscribe to more blogs. ;)");
        }
        msg.append("\n\nTop 10 personal blogs:");
        List<String> topUsers = showQueriesService.getTopUsers();
        if (topUsers.size() > 0) {
            for (String user : topUsers) {
                msg.append("\n@").append(user);
            }
        } else {
            msg.append("\nNo top users. Empty DB? ;)");
        }
        return CommandResult.fromString(msg.toString());
    }
    @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "BL @username - add @username to your blacklist")
    public CommandResult blacklistUser(User user_from, URI attachment, String... arguments) {
        User blUser = userService.getUserByName(arguments[0]);
        if (!blUser.isAnonymous()) {
            PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(user_from, blUser);
            if (result == PrivacyQueriesService.PrivacyResult.Added) {
                return CommandResult.fromString("User added to your blacklist");
            } else {
                return CommandResult.fromString("User removed from your blacklist");
            }
        }
        return CommandResult.fromString("User not found");
    }
    @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "BL *tag - add *tag to your blacklist")
    public CommandResult blacklistTag(User user_from, URI attachment, String... arguments) {
        if (!user_from.isAnonymous()) {
            Tag tag = tagService.getTag(arguments[0], false);
            if (tag != null) {
                PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistTag(user_from, tag);
                if (result == PrivacyQueriesService.PrivacyResult.Added) {
                    return CommandResult.fromString("Tag added to your blacklist");
                } else {
                    return CommandResult.fromString("Tag removed from your blacklist");
                }
            }
        }
        return CommandResult.fromString("Tag not found");
    }
    @UserCommand(pattern = "\\*", help = "* - Show your tags")
    public CommandResult commandTags(User currentUser, URI attachment, String... args) {
        List<TagStats> tags = tagService.getUserTagStats(currentUser.getUid());
        String msg = "Your tags: (tag - messages)\n" +
                tags.stream()
                        .map(t -> String.format("\n*%s - %d", t.getTag().getName(), t.getUsageCount())).collect(Collectors.joining());
        return CommandResult.fromString(msg);
    }
    @UserCommand(pattern = "S", help = "S - Show your subscriptions", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandSubscriptions(User currentUser, URI attachment, String... args) {
        List<User> friends = userService.getUserFriends(currentUser.getUid());
        List<String> tags = subscriptionService.getSubscribedTags(currentUser);
        String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName())
                .collect(Collectors.joining())
                : "You are not subscribed to any user.";
        msg += tags.size() > 0 ? "\nYou are subscribed to tags:" + tags.stream().map(t -> "\n*" + t)
                .collect(Collectors.joining())
                : "\nYou are not subscribed to any tag.";
        return CommandResult.fromString(msg);
    }
    @UserCommand(pattern = "!", help = "! - Show your favorite messages")
    public CommandResult commandFavorites(User currentUser, URI attachment, String... args) {
        List<Integer> mids = messagesService.getUserRecommendations(currentUser.getUid(), 0);
        if (mids.size() > 0) {
            return CommandResult.fromString("Favorite messages: \n" + printMessages(currentUser, mids, false));
        }
        return CommandResult.fromString("No favorite messages, try to \"like\" something ;)");
    }
    @UserCommand(pattern = "^\\!\\s+#(\\d+)", help = "! #12345 - recommend message")
    public CommandResult commandRecommend(User user, URI attachment, String... arguments) {
        int mid = NumberUtils.toInt(arguments[0], 0);
        if (mid > 0) {
            Optional<Message> msg = messagesService.getMessage(mid);
            if (msg.isPresent()) {
                if (msg.get().getUser() == user) {
                    return CommandResult.fromString("You can't recommend your own messages.");
                }
                MessagesService.RecommendStatus status = messagesService.recommendMessage(mid, user.getUid(), user.getUri().toASCIIString());
                switch (status) {
                    case Added:
                        applicationEventPublisher.publishEvent(new SystemEvent(this, SystemActivity.like(user, msg.get(),
                                subscriptionService.getUsersSubscribedToUserRecommendations(
                                        user.getUid(), msg.get()))));
                        return CommandResult.fromString("Message is added to your recommendations");
                    case Deleted:
                        return CommandResult.fromString("Message deleted from your recommendations.");
                    default:
                        return CommandResult.fromString("Unknown error.");
                }
            }
            return CommandResult.fromString("Message not found");
        }
        return CommandResult.fromString("Message not found");
    }
    // TODO: target notification
    @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @username - subscribe to user" +
            "\nU @username - unsubscribe from user", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandSubscribeUser(User user, URI attachment, String... args) {
        boolean subscribe = args[0].equalsIgnoreCase("s");
        User toUser = userService.getUserByName(args[1]);
        if (toUser.isAnonymous()) {
            return CommandResult.fromString("User not found");
        }
        if (subscribe) {
            if (subscriptionService.subscribeUser(user, toUser)) {
                // TODO: already subscribed case
                applicationEventPublisher.publishEvent(new SystemEvent(this,
                        SystemActivity.follow(user, Collections.singletonList(toUser))));
                return CommandResult.fromString("Subscribed to @" + toUser.getName());
            }
        } else {
            if (subscriptionService.unSubscribeUser(user, toUser)) {
                return CommandResult.fromString("Unsubscribed from @" + toUser.getName());
            }
            return CommandResult.fromString("You were not subscribed to @" + toUser.getName());
        }
        return CommandResult.fromString("Error");
    }
    @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" +
            "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandSubscribeTag(User user, URI attachment, String... args) {
        boolean subscribe = args[0].equalsIgnoreCase("s");
        Tag tag = tagService.getTag(args[1], true);
        if (subscribe) {
            if (subscriptionService.subscribeTag(user, tag)) {
                return CommandResult.fromString("Subscribed");
            }
        } else {
            if (subscriptionService.unSubscribeTag(user, tag)) {
                return CommandResult.fromString("Unsubscribed from " + tag.getName());
            }
            return CommandResult.fromString("You were not subscribed to " + tag.getName());
        }
        return CommandResult.fromString("Error");
    }
    @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments" +
            "\nU #1234 - unsubscribe from comments", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandSubscribeMessage(@Nonnull User user, URI attachment, String... args) {
        boolean subscribe = args[0].equalsIgnoreCase("s");
        int mid = NumberUtils.toInt(args[1], 0);
        Optional<Message> msg = messagesService.getMessage(mid);
        if (msg.isPresent()) {
            if (subscribe) {
                if (subscriptionService.subscribeMessage(msg.get(), user)) {
                    applicationEventPublisher.publishEvent(
                            new SystemEvent(this, SystemActivity.read(user, msg.get())));
                    return CommandResult.fromString("Subscribed");
                }
            } else {
                if (subscriptionService.unSubscribeMessage(mid, user.getUid())) {
                    return CommandResult.fromString("Unsubscribed from #" + mid);
                }
                return CommandResult.fromString("You were not subscribed to #" + mid);
            }
        }
        return CommandResult.fromString("Error");
    }
    @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE,
            help = "ON/OFF - Enable/disable subscriptions delivery")
    public CommandResult commandOnOff(User user, URI attachment, String[] input) {
        UserService.ActiveStatus newStatus;
        String retValUpdated;
        if (input[0].toLowerCase().equals("on")) {
            newStatus = UserService.ActiveStatus.Active;
            retValUpdated = "XMPP notifications are activated";
        } else {
            newStatus = UserService.ActiveStatus.Inactive;
            retValUpdated = "XMPP notifications are disabled";
        }
        if (userService.getAllJIDs(user).stream().allMatch(jid -> userService.setActiveStatusForJID(jid, newStatus))) {
            return CommandResult.fromString(retValUpdated);
        }
        return CommandResult.fromString("Error");
    }
    @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$",
            help = "@username+ - Show user's info and last 20 messages")
    public CommandResult commandUser(User user, URI attachment, String... arguments) {
        User blogUser = userService.getUserByName(arguments[0]);
        int page = arguments[1].length();
        if (!blogUser.isAnonymous() && !blogUser.isBanned()) {
            List<Integer> mids = messagesService.getUserBlog(blogUser.getUid(), 0, 0);
            return CommandResult.fromString(String.format("Last messages from @%s:\n%s", arguments[0],
                    printMessages(user, mids, false)));
        }
        return CommandResult.fromString("User not found");
    }
    @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)")
    public CommandResult commandShow(@Nonnull User user, URI attachment, String... arguments) {
        boolean showReplies = arguments[1].length() > 0;
        int mid = NumberUtils.toInt(arguments[0], 0);
        if (mid == 0) {
            return CommandResult.fromString("Error");
        }
        Optional<Message> msg = messagesService.getMessage(mid);
        if (msg.isPresent()) {
            if (showReplies) {
                List<Message> replies = messagesService.getReplies(user, mid);
                applicationEventPublisher.publishEvent(
                        new SystemEvent(this, SystemActivity.read(user, msg.get())));
                replies.add(0, msg.get());
                return CommandResult.fromString(replies.stream()
                        .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n")));
            }
            return CommandResult.fromString(PlainTextFormatter.formatPost(msg.get(), webDomain));
        }
        return CommandResult.fromString("Message not found");
    }
    @UserCommand(pattern = "^#(\\d+)\\/(\\d+)$", help = "#1234/5 - Show reply")
    public CommandResult commandShowReply(User user, URI attachment, String... arguments) {
        int mid = NumberUtils.toInt(arguments[0], 0);
        int rid = NumberUtils.toInt(arguments[1], 0);
        Message reply = messagesService.getReply(mid, rid);
        if (reply != null) {
            return CommandResult.fromString(PlainTextFormatter.formatPost(reply, webDomain));
        }
        return CommandResult.fromString("Reply not found");
    }
    @UserCommand(pattern = "^\\*(\\S+)(\\+?)$", help = "*tag - Show last messages with tag")
    public CommandResult commandShowTag(User user, URI attachment, String... arguments) {
        if (StringUtils.isNotEmpty(attachment.toString())) {
            // new message with tag
            return CommandResult.fromString(StringUtils.EMPTY);
        }
        Tag tag = tagService.getTag(arguments[0], false);
        if (tag != null) {
            // TODO: synonyms
            List<Integer> mids = messagesService.getTag(tag.getId(), user.getUid(), 0, 10);
            return CommandResult.fromString("Last messages with *" + tag.getName() + ":\n" + printMessages(user, mids, true));
        }
        return CommandResult.fromString("Tag not found");
    }
    @UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandDeletePost(User user, URI attachment, String... args) {
        int mid = Integer.valueOf(args[0]);
        Optional<Message> message = messagesService.getMessage(mid);
        if (message.isPresent() && messagesService.deleteMessage(user.getUid(), mid)) {
            applicationEventPublisher.publishEvent(new DeleteMessageEvent(this, message.get()));
            return CommandResult.fromString("Message deleted");
        }
        return CommandResult.fromString("This is not your message");
    }
    @UserCommand(pattern = "^D #(\\d+)(\\.|\\-|\\/)(\\d+)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandDeleteReply(User user, URI attachment, String... args) {
        int mid = Integer.parseInt(args[0]);
        int rid = Integer.parseInt(args[2]);
        if (messagesService.deleteReply(user.getUid(), mid, rid)) {
            return CommandResult.fromString("Reply deleted");
        } else {
            return CommandResult.fromString("This is not your reply");
        }
    }
    @UserCommand(pattern = "^(D L|DL|D LAST)$", help = "D L - Delete last message", patternFlags = Pattern.CASE_INSENSITIVE)
    public CommandResult commandDeleteLast(User user, URI attachment, String... args) {
        return CommandResult.fromString("Temporarily unavailable");
    }
    @UserCommand(pattern = "^\\?\\s+\\@([a-zA-Z0-9\\-\\.\\@]+)\\s+([\\s\\S]+)$", help = "? @user string - search in user messages")
    public CommandResult commandSearch(User user, URI attachment, String... args) {
        return CommandResult.fromString("Temporarily unavailable");
    }
    @UserCommand(pattern = "^\\?\\s+([\\s\\S]+)$", help = "? string - search in all messages")
    public CommandResult commandSearchAll(User user, URI attachment, String... args) {
        return CommandResult.fromString("Temporarily unavailable");
    }
    @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)")
    public CommandResult commandMyFeed(User user, URI attachment, String... arguments) {
        // number of # is the page count
        int page = arguments[0].length() - 1;
        List<Integer> mids = messagesService.getMyFeed(user.getUid(), page, false);
        if (mids.size() > 0) {
            return CommandResult.fromString("Your feed: \n" + printMessages(user, mids, true));
        }
        return CommandResult.fromString("Your feed is empty");
    }
    @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)?",
            help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message")
    public CommandResult EditOrReply(@Nonnull User user, @Nonnull URI attachment, String... args) throws Exception {
        int mid = NumberUtils.toInt(args[1]);
        int rid = NumberUtils.toInt(args[4], 0);
        String txt = StringUtils.defaultString(args[5]);
        Optional<Message> msg = messagesService.getMessage(mid);
        if (!msg.isPresent()) {
            return CommandResult.fromString("Message not found");
        }
        if (rid > 0) {
            Message reply = messagesService.getReply(mid, rid);
            if (reply == null) {
                return CommandResult.fromString("Reply not found");
            }
        }
        Pair<String, Set<Tag>> messageTags = tagService.fromString(txt);
        if (user.getUid() == msg.get().getUser().getUid() && rid == 0 && messageTags.getRight().size() > 0) {
            var updatedTags = tagService.updateTags(mid, messageTags.getRight());
            if (!CollectionUtils.isEqualCollection(updatedTags, msg.get().getTags())) {
                messagesService.setReadOnly(msg.get().getMid(), TagUtils.hasTag(updatedTags, "readonly"));
                return CommandResult.fromString("Tags are updated");
            } else {
                return CommandResult.fromString("Tags are NOT updated (5 tags maximum?)");
            }
        } else {
            if (txt.length() > 4096) {
                return CommandResult.fromString("Sorry, 4096 characters maximum.");
            }
            boolean haveAttachment = StringUtils.isNotEmpty(attachment.toString());
            String attachmentFName = null;
            String attachmentType = null;
            if (haveAttachment) {
                if (attachment.getScheme().equals("juick")) {
                    attachmentFName = attachment.getHost();
                    attachmentType = attachmentFName.substring(attachmentFName.length() - 3);
                } else {
                    try {
                        attachmentFName = HttpUtils.downloadImage(attachment.toURL(), storageService.getTemporaryDirectory()).getHost();
                        attachmentType = attachmentFName.substring(attachmentFName.length() - 3);
                    } catch (IOException e) {
                        logger.warn("Can not download {}", attachment.toURL());
                    }
                }
            }
            boolean attachmentProcessed = !haveAttachment || StringUtils.isNotEmpty(attachmentType);
            String messageText = attachmentProcessed ? txt : String.format("%s %s", txt, attachment.toASCIIString());
            int newrid = messagesService.createReply(mid, rid, user, messageText, attachmentType);
            if (newrid > 0) {
                if (haveAttachment && attachmentProcessed) {
                    String fname = String.format("%d-%d.%s", mid, newrid, attachmentType);
                    storageService.saveImageWithPreviews(attachmentFName, fname);
                }
                applicationEventPublisher.publishEvent(
                        new SystemEvent(this, SystemActivity.read(user, msg.get())));
                Message original = messagesService.getMessage(user.getUid(), mid).orElseThrow(IllegalStateException::new);
                if (!user.isAnonymous() && !original.isSubscribed()) {
                    subscriptionService.subscribeMessage(original, user);
                }
                Message reply = messagesService.getReply(mid, newrid);
                if (reply.getUser().isAnonymous()) {
                    reply.setUser(activityPubManager.actorToUser(reply.getUser().getUri()));
                } else {
                    reply.getUser().setAvatar(webApp.getAvatarUrl(reply.getUser()));
                }
                applicationEventPublisher.publishEvent(
                        new SystemEvent(this,
                        SystemActivity.message(reply.getUser(), reply,
                                subscriptionService.getUsersSubscribedToComments(original, reply))));
                return CommandResult.build(reply, "Reply posted.\n#" + mid + "/" + newrid + " "
                                + "https://juick.com/m/" + mid + "#" + newrid,
                        String.format("<a href='%s'>Reply</a> posted", PlainTextFormatter.formatUrl(reply)));
            } else {
                return CommandResult.fromString("Message is read-only");
            }
        }
    }

    String printMessages(User visitor, List<Integer> mids, boolean crop) {
        return messagesService.getMessages(visitor.getUid(), mids).stream()
                .sorted(Collections.reverseOrder())
                .map(PlainTextFormatter::formatPostSummary).collect(Collectors.joining("\n\n"));
    }
}