package cn.reghao.tnb.user.app.rpc;

import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.user.api.dto.UserCard;
import cn.reghao.tnb.user.api.dto.UserMessageDto;
import cn.reghao.tnb.user.api.iface.UserService;
import cn.reghao.tnb.user.app.db.mapper.UserRecommendMapper;
import cn.reghao.tnb.user.app.model.po.UserRecommend;
import cn.reghao.tnb.user.app.service.UserMessageService;
import cn.reghao.tnb.user.app.service.UserRelationService;
import cn.reghao.tnb.user.app.service.UserProfileService;
import cn.reghao.tnb.user.app.service.UserVipService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2022-04-12 14:16:39
 */
@DubboService
@Service
public class UserServiceImpl implements UserService {
    @DubboReference(check = false, retries = 0)
    private AccountQuery accountQuery;

    private final UserRelationService userRelationService;
    private final UserVipService userVipService;
    private final UserProfileService userProfileService;
    private final UserMessageService userMessageService;
    private final UserRecommendMapper userRecommendMapper;

    public UserServiceImpl(UserRelationService userRelationService, UserVipService userVipService,
                           UserProfileService userProfileService, UserMessageService userMessageService,
                           UserRecommendMapper userRecommendMapper) {
        this.userRelationService = userRelationService;
        this.userVipService = userVipService;
        this.userProfileService = userProfileService;
        this.userMessageService = userMessageService;
        this.userRecommendMapper = userRecommendMapper;
    }

    @Override
    public UserCard getUserAvatar(long userId) {
        return userProfileService.getUserCard(userId);
    }

    @Override
    public List<Long> getFollowingIds(long userId) {
        return userRelationService.getFollowingCards(userId).stream()
                .map(userCard -> {
                    String userIdStr = userCard.getUserId();
                    return accountQuery.getUserIdLong(userIdStr);
                })
                .collect(Collectors.toList());
    }

    @Override
    public boolean isVip(long userId) {
        return userVipService.isVip(userId);
    }

    @Override
    public void sendUserMessage(UserMessageDto userMessageDto) {
        userMessageService.addMessage(userMessageDto);
    }

    @Override
    public int getRecommendMode(long userId) {
        Integer mode = userRecommendMapper.findByUserId(userId);
        if (mode == null) {
            return 0;
        }

        return mode;
    }

    @Override
    public void setRecommendMode(long userId, boolean mode) {
        int modeInt = 0;
        if (mode) {
            modeInt = 1;
        }

        Integer mode1 = userRecommendMapper.findByUserId(userId);
        if (mode1 == null) {
            UserRecommend userRecommend = new UserRecommend(userId, modeInt);
            userRecommendMapper.save(userRecommend);
        } else if (mode1 != modeInt) {
            userRecommendMapper.updateMode(userId, mode1);
        }
    }
}
