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

import cn.reghao.jutil.jdk.string.IDObfuscation;
import cn.reghao.tnb.account.api.dto.AccountAvatar;
import cn.reghao.tnb.account.api.dto.AccountInfo;
import cn.reghao.tnb.account.api.dto.AuthedAccount;
import cn.reghao.tnb.account.api.dto.CrawledUser;
import cn.reghao.tnb.account.api.iface.AccountQuery;
import cn.reghao.tnb.account.app.db.repository.AccountRepository;
import cn.reghao.tnb.account.app.model.po.UserAccount;
import cn.reghao.tnb.account.app.security.form.AccountAuthToken;
import cn.reghao.tnb.account.app.service.AccountTokenService;
import cn.reghao.tnb.account.app.service.impl.AccountRegistryServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2021-11-12 17:32:01
 */
@DubboService
@Service
public class AccountQueryImpl implements AccountQuery {
    private final int pageSize = 100;
    private final AccountRepository accountRepository;
    private final AccountRegistryServiceImpl accountRegistryService;
    private final AccountTokenService accountTokenService;
    private final IDObfuscation userIdObfuscation;

    public AccountQueryImpl(AccountRepository accountRepository, AccountRegistryServiceImpl accountRegistryService,
                            AccountTokenService accountTokenService, IDObfuscation userIdObfuscation) {
        this.accountRepository = accountRepository;
        this.accountRegistryService = accountRegistryService;
        this.accountTokenService = accountTokenService;
        this.userIdObfuscation = userIdObfuscation;
    }

    @Override
    public AuthedAccount getAuthedAccount(int type, String sessId) {
        AccountInfo accountInfo = accountTokenService.getAccountInfo(type, sessId);
        if (accountInfo != null) {
            String userIdStr = accountInfo.getUserId();
            //long userId = userIdObfuscation.restore(userIdStr);
            long userId = Long.parseLong(userIdStr);
            UserAccount userAccount = accountRepository.getUserAccount(userId);
            String role = userAccount.getRole();
            return new AuthedAccount(userAccount.getUserId());
        }

        return null;
    }

    @Override
    public Long getUserId(String username) {
        return accountRepository.getUserId(username);
    }

    @Override
    public AccountInfo getAccountInfo(long userId) {
        AccountInfo accountInfo = accountRepository.getAccountInfo(userId);
        if (accountInfo != null) {
            String mobile = accountInfo.getMobile();
            if (mobile != null) {
                String mobile1 = new StringBuilder(mobile).replace(3, 7, "****").toString();
                accountInfo.setMobile(mobile1);
            }
        }

        return accountInfo;
    }

    @Override
    public List<AccountInfo> getAccountInfos(List<Long> userIds) {
        return accountRepository.getAccountInfos(userIds);
/*        return userIds.stream()
                .map(accountRepository::getAccountInfo)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());*/
    }

    @Override
    public AccountInfo getByMobile(String mobile) {
        return accountRepository.getAccountInfoByMobile(mobile);
    }

    @Override
    public AccountInfo getByUsername(String username) {
        return accountRepository.getAccountInfoByUsername(username);
    }

    @Override
    public Long createCrawledAccount(CrawledUser crawledUser) {
        String screenName = crawledUser.getScreenName();
        String username = crawledUser.getUsername();
        String avatarUrl = crawledUser.getAvatarUrl();
        Long userId = getUserId(username);
        if (userId != null) {
            return userId;
        } else {
            userId = accountRegistryService.getNextUserId();
        }

        String email = String.format("%s@tnb.cn", userId);
        UserAccount userAccount = new UserAccount(userId, email, username, screenName, avatarUrl);
        accountRepository.saveAccount(userAccount);
        return userId;
    }

    @Override
    public List<AccountAvatar> getAccountAvatars(List<Long> userIds) {
        return accountRepository.getAccountAvatars(userIds);
    }

    @Override
    public void updateAvatar(long userId, String avatarUrl) {
        accountRepository.updateUserAvatar(userId, avatarUrl);
    }

    @Override
    public String getUserIdStr(long userIdLong) {
        return userIdObfuscation.obfuscate(userIdLong);
    }

    @Override
    public long getUserIdLong(String userIdStr) {
        return userIdObfuscation.restore(userIdStr);
    }
}
