package com.xunwang.ucenter;

import com.google.gson.JsonObject;
import com.xunwang.ucenter.utils.LocationUtils;
import org.junit.Test;
import org.lionsoul.ip2region.DbMakerConfigException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class test {

    @Test
    public void testName() {

        Set<String> names = new LinkedHashSet<>();
        for (int i = 0; i < 10000; i++) {
            String str = "";
            int highPos;
            int lowPos;
            Random random = new Random();

            highPos = (176 + Math.abs(random.nextInt(39)));
            lowPos = (161 + Math.abs(random.nextInt(93)));

            byte[] b = new byte[2];
            b[0] = (Integer.valueOf(highPos)).byteValue();
            b[1] = (Integer.valueOf(lowPos)).byteValue();

            try {
                str = new String(b, "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            names.add(str);
            System.out.println(str.charAt(0));
        }
        System.out.println(names.size());
    }

    @Test
    public void testRandomName() throws UnsupportedEncodingException {
        for (int j = 0; j < 10000; j++) {
            // 判断名字是两个字还是三个字
            Random random = new Random();
            int num = random.nextInt(4);
            if (num < 3) {
                num = 2;
            }

            // 生成的姓名
            List<String> names = new ArrayList<>();
            String str = "";

            for (int i = 0; i < num; i++) {
                int highPos;
                int lowPos;

                highPos = (176 + Math.abs(random.nextInt(39)));
                lowPos = (161 + Math.abs(random.nextInt(93)));

                byte[] b = new byte[2];
                b[0] = (Integer.valueOf(highPos)).byteValue();
                b[1] = (Integer.valueOf(lowPos)).byteValue();

                str = new String(b, "GBK");

                names.add(str);
            }

            String resultName = String.join("", names);
            System.out.println(resultName);
        }
    }

    @Test
    public void testMap() {
        String code = "ffsfsf";
        String query = "code="+code;
        System.out.println(query);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("code", code);
        System.out.println(jsonObject.toString());
    }

    @Test
    public void testLocation() throws DbMakerConfigException, IOException {
        String ip = "101.204.146.11";
        String regionFromIp = LocationUtils.getRegionFromIp(ip);
        System.out.println(regionFromIp);
    }
}
