package com.izouma.nineth.service;

import com.github.kevinsawicki.http.HttpRequest;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.config.Constants;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.UserRegister;
import com.izouma.nineth.enums.AuthorityName;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.security.Authority;
import com.izouma.nineth.service.storage.StorageService;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;

public class UserServiceTest extends ApplicationTests {

    @Autowired
    private UserService    userService;
    @Autowired
    private UserRepo       userRepo;
    @Autowired
    private StorageService storageService;

    @Test
    public void findByUsernameAndDelFalse1() {
        userService.clearCache();
        userRepo.findByUsernameAndDelFalse("admin");
        userRepo.findByUsernameAndDelFalse("admin");
    }

    @Test
    public void searchByPhone() {
        System.out.println(userService.searchByPhone("15077886171"));
    }

    @Test
    public void sss9() throws IOException {
        for (User user : userRepo.findAll()) {
            if (user.getAvatar().startsWith("https://cdn")) {
                HttpRequest request = HttpRequest.get(user.getAvatar());
                File file = File.createTempFile("gw1", "1sadga");
                request.receive(file);
                user.setAvatar(storageService.uploadFromInputStream(new FileInputStream(file), "image/" + RandomStringUtils.randomAlphabetic(12) + ".jpg"));
                userRepo.save(user);
            }
        }
    }

    @Test
    public void a() {
        for (String s : "13184195999,15226346344,13695289829,13810579045,18801102031,13670128631,18310554139,19916139888,18891557555,18289256896,18389829112,18321852532,13164376553,18600235674"
                .split(",")) {
            String name = "9th_" + RandomStringUtils.randomAlphabetic(8);
            User user = userService.create(UserRegister.builder()
                    .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
                    .username(name)
                    .nickname(name)
                    .password("123456")
                    .avatar(Constants.DEFAULT_AVATAR)
                    .phone(s)
                    .build());
        }
    }
}