package cn.hmsoft.helper;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.concurrent.ThreadLocalRandom;

/********************************
 * @note 随机数辅助类
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-07 21:19:20
 * @version 4.0.0
 */
public abstract class RandomHelper {
	/*****************************
	 * 随机数的基数
	 */
	public static String GobalRandomString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static String RandomString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	/*****************************
	 * 随机数的基数
	 */
	public static String GobalRandomStringByNumber = "0123456789";

	/*
	 * 获取随机的字符
	 */
	private static String getRandomChar(int num) {
		return getRandomChar(GobalRandomString, num);
	}

	private static String getRandomChar(String value, int num) {
		return String.valueOf(value.charAt(num));
	}

	/**************************
	 * 从默认的种子基数中获取length位数的随机数
	 */
	public static String getRandomValue(int length) {
		// 绘制随机字符
		String randomString = "";
		for (int i = 1; i <= length; i++) {
			randomString = randomString
					+ getRandomChar(ThreadLocalRandom.current().nextInt(
							GobalRandomString.length()));
		}
		return randomString;
	}
	
	public static String getRandomNum(int length) {
		String randomString = "";
		for (int i = 1; i <= length; i++) {
			randomString = randomString
					+ getRandomChar(ThreadLocalRandom.current().nextInt(
							GobalRandomStringByNumber.length()));
		}
		return randomString;
	}

	/**************************
	 * 从默认的种子基数中获取随机数，默认4位
	 */
	public static String getRandomValue() {
		return getRandomValue(4);
	}

	/**************************
	 * 从指定的种子基数获取length位数的随机数
	 */
	public static String getRandomValue(String value, Integer length) {
		// 绘制随机字符
		if (StringHelper.isEmpty(value))
			value = GobalRandomString;
		if (length == null)
			length = 4;
		String randomString = "";
		for (int i = 1; i <= length; i++) {
			randomString = randomString
					+ getRandomChar(value,
							ThreadLocalRandom.current().nextInt(value.length()));
		}
		return randomString;
	}

	/***********************************
	 * 获得随机颜色
	 */
	private static Color getRandColor(int fc, int bc) {
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + ThreadLocalRandom.current().nextInt(bc - fc - 16);
		int g = fc + ThreadLocalRandom.current().nextInt(bc - fc - 14);
		int b = fc + ThreadLocalRandom.current().nextInt(bc - fc - 18);
		return new Color(r, g, b);
	}

	/**********************
	 * 绘制字符串
	 */
	private static String drowString(Graphics g, RandomImageEntity entity, int i) {
		g.setColor(new Color(ThreadLocalRandom.current().nextInt(255),
				ThreadLocalRandom.current().nextInt(255), ThreadLocalRandom
						.current().nextInt(255)));
		String rand = String.valueOf(getRandomChar(ThreadLocalRandom.current()
				.nextInt(entity.getRandom_string().length())));
		// randomString += rand;
		g.translate(ThreadLocalRandom.current().nextInt(4), ThreadLocalRandom
				.current().nextInt(4));
		g.drawString(rand,
				entity.getMargin_left() + (i - 1) * entity.getFont_interval(),
				entity.getMargin_top());
		return rand;
	}

	/*************
	 * 绘制干扰线
	 */
	private static void drowLine(Graphics g, RandomImageEntity entity) {
		g.setColor(new Color(ThreadLocalRandom.current().nextInt(255),
				ThreadLocalRandom.current().nextInt(255), ThreadLocalRandom
						.current().nextInt(255)));
		int x = ThreadLocalRandom.current().nextInt(entity.getImg_width());
		int y = ThreadLocalRandom.current().nextInt(entity.getImg_height());
		int xl = ThreadLocalRandom.current().nextInt(entity.getImg_width());
		int yl = ThreadLocalRandom.current().nextInt(entity.getImg_height());
		g.drawLine(x, y, xl, yl);
	}

	/***********************************
	 * 生成随机图片
	 * 
	 * @param entity
	 * @param g
	 *            图片信息
	 * @return 随机的字符串
	 */
	public static String getRandomValue(RandomImageEntity entity) {
		// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
		if (StringHelper.isEmpty(entity.getRandom_string()))
			entity.setRandom_string(GobalRandomString);
		if (entity.getRandom_length() == null)
			entity.setRandom_length(4);
		entity.setRandom_image(new BufferedImage(entity.getImg_width(),
				entity.getImg_height(), BufferedImage.TYPE_INT_BGR));
		Graphics g = entity.getRandom_image().getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
		g.fillRect(0, 0, entity.getImg_width(), entity.getImg_height());
		String font_family = StringHelper.isEmpty(entity.getFont_family()) ? "Times New Roman"
				: entity.getFont_family();
		g.setFont(new Font(font_family, Font.ROMAN_BASELINE, entity
				.getLine_size()));
		g.setColor(getRandColor(110, 133));
		// 绘制干扰线
		for (int i = 0; i <= entity.getLine_count(); i++) {
			drowLine(g, entity);
		}
		// 设置字体
		g.setFont(new Font(entity.getFont_family(), Font.BOLD, entity
				.getFont_size()));
		// 绘制随机字符
		String randomString = "";
		for (int i = 1; i <= entity.getRandom_length(); i++) {
			randomString += drowString(g, entity, i);
		}

		g.dispose();
		return randomString;

	}

	public static class RandomImageEntity {

		/**************************
		 * 随机数种子
		 */
		private String random_string;
		/**********************
		 * 图片宽
		 */
		private Integer img_width = 80;
		/*******************
		 * 图片高度
		 */
		private Integer img_height = 26;
		/*******************
		 * 字体上面空格
		 */
		private Integer margin_top = 16;
		/*****************
		 * 字体左边空格
		 */
		private Integer margin_left = 5;
		/*******************************
		 * 字体大小
		 */
		private Integer font_size = 20;
		/************************
		 * 字体
		 */
		private String font_family = "Times New Roman";
		/***********************
		 * 随机的长度
		 */
		private Integer random_length = 4;
		/************************
		 * 干扰线数量
		 */
		private Integer line_count = 10;

		/********************
		 * 线宽度
		 */
		private Integer line_size = 18;
		/*********************
		 * 字体间隔
		 */
		private Integer font_interval = 15;

		public Integer getLine_count() {
			return line_count;
		}

		public void setLine_count(Integer line_count) {
			this.line_count = line_count;
		}

		public Integer getImg_width() {
			return img_width;
		}

		public void setImg_width(Integer img_width) {
			this.img_width = img_width;
		}

		public Integer getImg_height() {
			return img_height;
		}

		public void setImg_height(Integer img_height) {
			this.img_height = img_height;
		}

		public Integer getMargin_top() {
			return margin_top;
		}

		public void setMargin_top(Integer margin_top) {
			this.margin_top = margin_top;
		}

		public Integer getMargin_left() {
			return margin_left;
		}

		public void setMargin_left(Integer margin_left) {
			this.margin_left = margin_left;
		}

		public Integer getFont_size() {
			return font_size;
		}

		public void setFont_size(Integer font_size) {
			this.font_size = font_size;
		}

		public String getFont_family() {
			return font_family;
		}

		public void setFont_family(String font_family) {
			this.font_family = font_family;
		}

		public Integer getRandom_length() {
			return random_length;
		}

		public void setRandom_length(Integer random_length) {
			this.random_length = random_length;
		}

		public Integer getLine_size() {
			return line_size;
		}

		public void setLine_size(Integer line_size) {
			this.line_size = line_size;
		}

		public Integer getFont_interval() {
			return font_interval;
		}

		public void setFont_interval(Integer font_interval) {
			this.font_interval = font_interval;
		}

		public String getRandom_string() {
			return random_string;
		}

		public void setRandom_string(String random_string) {
			this.random_string = random_string;
		}

		private BufferedImage random_image = null;

		public BufferedImage getRandom_image() {
			return random_image;
		}

		public void setRandom_image(BufferedImage random_image) {
			this.random_image = random_image;
		}
		

	}
}
