import cn.reghao.jutil.jdk.security.RandomString;
import cn.reghao.tnb.account.app.AccountApplication;
import cn.reghao.tnb.account.app.db.mapper.UserAccountMapper;
import cn.reghao.tnb.account.app.model.dto.OAuthAppDto;
import cn.reghao.tnb.account.app.model.po.UserAccount;
import cn.reghao.tnb.account.app.service.AccountRegistryService;
import cn.reghao.tnb.account.app.service.OAuthAppService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
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;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author reghao
 * @date 2022-08-11 10:14:51
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = AccountApplication.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 = 1;
        String password = "0123456789";

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

    @Test
    public void create() {
        List<UserAccount> list = userAccountMapper.findAll();

        String basePath = "";
        File baseDir = new File(basePath);
        File[] files = baseDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File srcDir = files[i];
            String dirName = srcDir.getName();

            UserAccount userAccount = list.get(i);
            long userId = userAccount.getUserId();
            //String screenName = dirName;
            String namePrefix = "";
            String screenName = String.format("%s%s", namePrefix, userId);

            String dirName1 = String.format("%s.%s", userId, screenName);
            String destPath = String.format("%s/%s", srcDir.getParent(), dirName1);
            File destDir = new File(destPath);

            try {
                renameDir(srcDir, destDir);
                userAccountMapper.updateUserScreenName(userId, screenName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean renameDir(File srcDir, File destDir) throws IOException {
        if (srcDir.exists() && !destDir.exists()) {
            FileUtils.moveDirectory(srcDir, destDir);
            return true;
        }

        return false;
    }

    @Autowired
    OAuthAppService oAuthAppService;
    @Test
    public void oauthTest() {
        OAuthAppDto authAppDto = new OAuthAppDto();
        authAppDto.setName("gateway");
        authAppDto.setHomeUrl("https://cili.reghao.cn");
        authAppDto.setCallbackUrl("https://cili.reghao.cn/callback");
        oAuthAppService.create(authAppDto);
    }
}
