/*
 * 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.service;

import com.juick.model.Message;
import com.juick.model.Tag;
import com.juick.model.User;
import com.juick.model.NotifyOpts;
import com.juick.util.MessageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by aalexeev on 11/13/16.
 */
@Repository
public class SubscriptionServiceImpl extends BaseJdbcService implements SubscriptionService {
    @Inject
    private UserService userService;
    @Inject
    private MessagesService messagesService;
    @Inject
    private TagService tagService;

    @Transactional(readOnly = true)
    @Override
    public List<User> getSubscribedUsers(final int uid, final Message msg) {
        int mid = msg.getMid();
        User author = messagesService.getMessageAuthor(mid);

        List<User> subscribers = userService.getUserReaders(uid);
        List<User> mentionedUsers = userService.getUsersByName(MessageUtils.getMentions(msg).stream()
                .map(u -> u.substring(1))
                .toList()).stream()
                .filter(u -> !userService.isInBL(u.getUid(), msg.getUser().getUid()))
                .toList();
        List<User> users = ListUtils.union(subscribers, mentionedUsers);
        List<Integer> tags = tagService.getMessageTagsIDs(mid);
        List<String> tagsStr = tagService.getMessageTags(mid).stream().map(t -> t.getTag().getName())
                .toList();

        Set<Integer> set = users.stream()
                .filter(u -> !u.isBanned())
                .map(User::getUid)
                .filter(u -> Collections.disjoint(tagService.getUserBLTags(u), tagsStr))
                .collect(Collectors.toSet());

        if (!tags.isEmpty()) {
            List<Integer> tagUsers = getNamedParameterJdbcTemplate().queryForList(
                    "SELECT st.suser_id FROM subscr_tags st " +
                            "WHERE st.tag_id IN (:ids) AND st.suser_id != :uid " +
                            " AND NOT EXISTS (SELECT 1 FROM bl_users bu WHERE bu.bl_user_id = :authorUid and st.suser_id = bu.user_id)"
                            +
                            " AND NOT EXISTS (SELECT 1 FROM bl_tags bt WHERE bt.tag_id IN (:ids) and st.suser_id = bt.user_id)",
                    new MapSqlParameterSource()
                            .addValue("ids", tags)
                            .addValue("uid", uid)
                            .addValue("authorUid", author.getUid()),
                    Integer.class);
            set.addAll(tagUsers);
        }
        return userService.getUsersByID(set);
    }

    @Override
    public List<User> getUsersSubscribedToComments(@Nonnull final Message msg, @Nonnull final Message reply) {
        return getUsersSubscribedToComments(msg, reply, false);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getUsersSubscribedToComments(@Nonnull final Message msg, @Nonnull final Message reply,
            boolean blacklisted) {
        List<User> subscribers = userService.getUsersByID(getJdbcTemplate().queryForList(
                "SELECT suser_id FROM subscr_messages WHERE message_id=? AND suser_id!=?",
                Integer.class,
                msg.getMid(), reply.getUser().getUid()));
        List<User> mentionedUsers = userService.getUsersByName(MessageUtils.getMentions(reply).stream()
                .map(u -> u.substring(1)).toList());
        List<User> users = IteratorUtils.toList(CollectionUtils.union(subscribers, mentionedUsers).iterator());
        if (!users.isEmpty()) {
            return users.stream()
                    .filter(u -> blacklisted || !u.isBanned() && !userService.isReplyToBL(u, reply))
                    .toList();
        }
        return Collections.emptyList();
    }

    @Override
    public List<User> getUsersSubscribedToUserRecommendations(final int uid, final Message msg) {
        List<String> msgTags = tagService.getMessageTags(msg.getMid()).stream().map(t -> t.getTag().getName())
                .toList();
        if (msg.getLikes() == 1) {
            return userService.getUserReaders(uid).stream()
                    .filter(u -> !u.equals(msg.getUser()))
                    .filter(u -> !userService.isInBLAny(u.getUid(), msg.getUser().getUid()))
                    .filter(u -> Collections.disjoint(tagService.getUserBLTags(u.getUid()),
                            msgTags))
                    .toList();
        }
        return Collections.emptyList();
    }

    @Transactional
    @Override
    public boolean subscribeMessage(final Message message, final User user) {
        try {
            boolean result = getJdbcTemplate().update(
                    "INSERT INTO subscr_messages(suser_id, message_id) VALUES (?, ?)",
                    user.getUid(), message.getMid()) == 1;
            messagesService.setLastReadComment(user, message.getMid(), message.getReplies());
            return result;
        } catch (DataIntegrityViolationException e) {
            return false;
        }
    }

    @Transactional
    @Override
    public boolean unSubscribeMessage(final int mid, final int vuid) {
        return getJdbcTemplate().update(
                "DELETE FROM subscr_messages WHERE message_id=? AND suser_id=?", mid, vuid) > 0;
    }

    @Transactional
    @Override
    public boolean subscribeUser(final User user, final User toUser) {
        try {
            return getJdbcTemplate().update(
                    "INSERT INTO subscr_users(user_id,suser_id) VALUES (?,?)", toUser.getUid(),
                    user.getUid()) == 1;
        } catch (DuplicateKeyException e) {
            return true;
        }
    }

    @Transactional
    @Override
    public boolean unSubscribeUser(final User user, final User fromUser) {
        return getJdbcTemplate().update(
                "DELETE FROM subscr_users WHERE suser_id=? AND user_id=?", user.getUid(),
                fromUser.getUid()) > 0;
    }

    @Transactional
    @Override
    public boolean subscribeTag(final User user, final Tag toTag) {
        try {

            return getJdbcTemplate().update(
                    "INSERT INTO subscr_tags(tag_id,suser_id) VALUES (?,?)", toTag.TID,
                    user.getUid()) == 1;
        } catch (DuplicateKeyException e) {
            return true;
        }
    }

    @Transactional
    @Override
    public boolean unSubscribeTag(final User user, final Tag toTag) {
        return getJdbcTemplate().update(
                "DELETE FROM subscr_tags WHERE tag_id=? AND suser_id=?", toTag.TID, user.getUid()) > 0;
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getSubscribedTags(User user) {
        return getJdbcTemplate().queryForList("SELECT tags.name FROM subscr_tags INNER JOIN tags " +
                "ON(tags.tag_id = subscr_tags.tag_id) " +
                "WHERE subscr_tags.suser_id=? ORDER BY tags.name", String.class, user.getUid());
    }

    @Transactional(readOnly = true)
    @Override
    public NotifyOpts getNotifyOptions(final User user) {
        List<NotifyOpts> list = getJdbcTemplate().query(
                "SELECT jnotify,subscr_notify,recommendations FROM useroptions WHERE user_id=?",
                (rs, num) -> {
                    NotifyOpts options = new NotifyOpts();
                    options.setRepliesEnabled(rs.getInt(1) > 0);
                    options.setSubscriptionsEnabled(rs.getInt(2) > 0);
                    options.setRecommendationsEnabled(rs.getInt(3) > 0);
                    return options;
                },
                user.getUid());

        return list.isEmpty() ? new NotifyOpts() : list.get(0);
    }

    @Transactional
    @Override
    public boolean setNotifyOptions(final User user, final NotifyOpts options) {
        int jnotify = getJdbcTemplate().update(
                "UPDATE useroptions SET jnotify=? WHERE user_id=?",
                options.isRepliesEnabled() ? 1 : 0,
                user.getUid());

        int subscr_notify = getJdbcTemplate().update(
                "UPDATE useroptions SET subscr_notify=? WHERE user_id=?",
                options.isSubscriptionsEnabled() ? 1 : 0,
                user.getUid());

        int recommendations = getJdbcTemplate().update(
                "UPDATE useroptions SET recommendations=? WHERE user_id=?",
                options.isRecommendationsEnabled() ? 1 : 0,
                user.getUid());

        return jnotify > 0 && subscr_notify > 0 && recommendations > 0;
    }
}