package com.xiaomai.utils;

import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class RandomStringUtil {
    /**
     * @param string_length
     * @param randomstring
     * @return
     */
    public static String randomNumber(int string_length, String randomstring) {


        String chars = "1234567890";
        int randomstring_lenth;
        if (StringUtils.isEmpty(randomstring) || null == randomstring)
            randomstring_lenth = 0;
        else
            randomstring_lenth = randomstring.length();

        if (randomstring.length() >= string_length)
            return "error:前缀字符不得长于总字符长度";

        for (int i = 0; i < string_length - randomstring_lenth; i++) {
            Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(chars.length());
            randomstring += chars.substring(randomInt, randomInt + 1);

        }
        return randomstring;
    }

    public static String getTimestamp() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        long today_date = calendar.getTime().getTime();
        return String.valueOf(today_date);
    }

    public static String getTimestampToPhone() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        long today_date = calendar.getTime().getTime();
        String today_date_str = String.valueOf(today_date);
        return today_date_str.substring(0, 11);
    }

    public static String randomMobile(String... randomstring) {
        String str = "109";
        if (randomstring.length > 0) {
            str = randomstring[0];
        }
        return randomNumber(11, str);
    }

    public static String random_name() {
        String names_first = "赵钱孙李周吴郑王杜贾谢林彭张陈何刘杨";
        String names_last = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
        String names_num = "零一二三四五六七八九十";
        String randomstring = "";

        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(names_first.length());
        randomstring += names_first.substring(randomInt, randomInt + 1);

        randomInt = randomGenerator.nextInt(names_last.length());
        randomstring += names_last.substring(randomInt, randomInt + 1);

        randomInt = randomGenerator.nextInt(names_num.length());
        randomstring += names_num.substring(randomInt, randomInt + 1);

        return randomstring;

    }

    public static String randomString(int length) {

        String str = "";
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghijklmnopqrstuvwxyz0123456789";

        for (int i = 0; i < length; i++) {
            Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(chars.length());
            str += chars.substring(randomInt, randomInt + 1);

        }
        return str;

    }
}
