import cn.reghao.jutil.jdk.security.RandomString;
import cn.reghao.tnb.auth.app.AuthApplication;
import cn.reghao.tnb.auth.app.db.mapper.UserAccountMapper;
import cn.reghao.tnb.auth.app.model.dto.OAuthAppDto;
import cn.reghao.tnb.auth.app.model.po.UserAccount;
import cn.reghao.tnb.auth.app.service.AccountRegistryService;
import cn.reghao.tnb.auth.app.service.OAuthAppService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.ActiveProfiles;

/**
 * @author reghao
 * @date 2022-08-11 10:14:51
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = AuthApplication.class)
public class UserAccountTest {
    @Autowired
    UserAccountMapper userAccountMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    AccountRegistryService accountRegisterService;
    @Test
    public void createAccountTest() {
        String password = "abcdefg1234567";
        long mobile = 13012330000L;
        while (mobile < 13012340000L) {
            accountRegisterService.createAccount(String.valueOf(mobile), password);
            log.info("{} created...", mobile);
            mobile++;
        }
    }

    @Test
    public void setUserPassword() {
        long userId = 10001;
        String password = "ecUgpUeIX2ub84EYPsiQ";

        String salt = RandomString.getSalt(64);
        String encodedPassword = passwordEncoder.encode(password + salt);
        UserAccount userAccount = userAccountMapper.findByUserId(userId);
        if (userAccount != null) {
            userAccountMapper.updateSetPassword(userId, encodedPassword, salt);
        }
    }

    @Autowired
    OAuthAppService oAuthAppService;
    @Test
    public void oauthTest() {
        OAuthAppDto authAppDto = new OAuthAppDto();
        authAppDto.setName("gateway");
        authAppDto.setHomeUrl("");
        authAppDto.setCallbackUrl("");
        oAuthAppService.create(authAppDto);
    }

    @Test
    public void updateUserMobileTest() {
    }

    private String getUserMobile(long userId) {
        String userIdStr = ""+userId;
        int len = userIdStr.length();
        StringBuilder sb = new StringBuilder();
        sb.append("086");
        sb.append("0".repeat(Math.max(0, 8 - len)));
        sb.append(userId);

        String mobile = sb.toString();
        return mobile.length() == 11 ? mobile : "";
    }
}
