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

import cn.reghao.tnb.auth.api.dto.AccountInfo;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.user.api.dto.UserCard;
import cn.reghao.tnb.user.api.dto.UserInfo;
import cn.reghao.tnb.user.app.db.mapper.UserProfileMapper;
import cn.reghao.tnb.user.app.db.mapper.UserRelationMapper;
import cn.reghao.tnb.user.app.model.dto.UserProfileUpdate;
import cn.reghao.tnb.user.app.model.po.UserProfile;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author reghao
 * @date 2022-07-08 17:52:21
 */
@Service
public class UserProfileService {
    @DubboReference(check = false, retries = 0, timeout = 30_000)
    private AccountQuery accountQuery;

    private int pageSize = 100;
    private final UserProfileMapper userProfileMapper;
    private final UserRelationMapper userRelationMapper;
    private final UserVipService userVipService;

    public UserProfileService(UserProfileMapper userProfileMapper, UserRelationMapper userRelationMapper,
                              UserVipService userVipService) {
        this.userProfileMapper = userProfileMapper;
        this.userVipService = userVipService;
        this.userRelationMapper = userRelationMapper;
    }

    public void updateUserProfile(UserProfileUpdate userProfileUpdate) {
        long loginUser = UserContext.getUserId();
        int type = userProfileUpdate.getType();
        String content = userProfileUpdate.getContent();

        /*userProfileMapper.updateUserProfile(loginUser, userProfileUpdate.getMotto(), userProfileUpdate.getGender());
        accountQuery.updateScreenName(loginUser, userProfileUpdate.getNickname());*/
    }

    public UserInfo getUserInfo(long userId) {
        AccountInfo accountInfo = accountQuery.getAccountInfo(userId);
        UserProfile userProfile = userProfileMapper.findByUserId(userId);
        if (userProfile == null) {
            userProfile = new UserProfile(userId);
            userProfileMapper.save(userProfile);
        }

        int gender = userProfile.getGender();
        String signature = userProfile.getSignature();
        int following = userProfile.getFollowing();
        int follower = userProfile.getFollower();
        boolean vip = userVipService.isVip(userId);
        String userIdStr = accountQuery.getUserIdStr(userId);
        UserInfo userInfo = new UserInfo(accountInfo, userIdStr, gender, signature, following, follower, vip);
        String username = accountInfo.getUsername();
        if (username.startsWith("bilibili_")) {
            long biliUserId = Long.parseLong(username.replace("bilibili_", ""));
            userInfo.setBiliUserId(biliUserId);
        }

        return userInfo;
    }

    public UserCard getUserCard(long userId) {
        long loginUser = UserContext.getUserId();
        UserInfo userInfo = getUserInfo(userId);
        boolean followed = isFollowing(loginUser, userId);
        String userIdStr = accountQuery.getUserIdStr(userId);
        return new UserCard(userInfo, userIdStr, followed);
    }

    private boolean isFollowing(long loginUserId, long userId) {
        return userRelationMapper.findRelation(loginUserId, userId) != null;
    }
}
