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

import cn.reghao.jutil.jdk.db.PageList;
import cn.reghao.tnb.account.api.dto.AccountInfo;
import cn.reghao.tnb.account.api.dto.CrawledUser;
import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.content.api.iface.UserContentService;
import cn.reghao.tnb.user.api.dto.UserInfo;
import cn.reghao.tnb.user.api.dto.UserSearch;
import cn.reghao.tnb.user.api.dto.VipPlanInfo;
import cn.reghao.tnb.user.api.iface.AdminUserService;
import cn.reghao.tnb.user.app.db.mapper.UserProfileMapper;
import cn.reghao.tnb.user.app.model.po.UserProfile;
import cn.reghao.tnb.user.app.model.po.VipPlan;
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 2024-02-15 09:17:08
 */
@DubboService
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @DubboReference(check = false, retries = 0)
    private AccountQuery accountQuery;
    @DubboReference(check = false, retries = 0, timeout = 30_000)
    private UserContentService userContentService;
    private final UserVipService userVipService;
    private final UserProfileMapper userProfileMapper;
    private int pageSize = 100;

    public AdminUserServiceImpl(UserVipService userVipService, UserProfileMapper userProfileMapper) {
        this.userVipService = userVipService;
        this.userProfileMapper = userProfileMapper;
    }

    @Override
    public PageList<UserInfo> getUserList(UserSearch userSearch) {
        return PageList.empty();
    }

    public PageList<UserInfo> getUserList(int nextId) {
        int total = userProfileMapper.countAll();
        List<UserProfile> userProfileList = userProfileMapper.findAllById(pageSize, nextId);
        List<UserInfo> list = userProfileList.stream().map(this::getUserInfo).collect(Collectors.toList());
        if (!userProfileList.isEmpty()) {
            nextId = userProfileList.get(userProfileList.size()-1).getId();
        }

        return PageList.pageList(""+nextId, list);
    }

    private UserInfo getUserInfo(UserProfile userProfile) {
        long userId = userProfile.getUserId();
        AccountInfo accountInfo = accountQuery.getAccountInfo(userId);
        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;
    }

    @Override
    public List<VipPlanInfo> getVipPlans() {
        List<VipPlan> list = userVipService.getVipPlans();
        return list.stream().map(this::getVipPlanInfo).collect(Collectors.toList());
    }

    private VipPlanInfo getVipPlanInfo(VipPlan vipPlan) {
        int planId = vipPlan.getPlanId();
        String name = vipPlan.getName();
        double price = vipPlan.getPrice();
        int duration = vipPlan.getDuration();
        return new VipPlanInfo(planId, name, price, duration);
    }

    public Long getUserId(String username) {
        AccountInfo accountInfo = accountQuery.getByUsername(username);
        if (accountInfo != null) {
            long userId = accountInfo.getUserId();
            return userId;
        }

        return null;
    }

    public synchronized Long createAccount(CrawledUser crawledUser) {
        Long userId = accountQuery.createCrawledAccount(crawledUser);
        UserProfile userProfile = userProfileMapper.findByUserId(userId);
        if (userProfile == null) {
            int gender = crawledUser.getGender();
            String signature = crawledUser.getSignature();
            userProfile = new UserProfile(userId, gender, signature);
            userProfileMapper.save(userProfile);
        }

        return userId;
    }
}
