package com.mandi.system.utils;

import java.util.Random;

import com.google.code.kaptcha.text.TextProducer;

public class KaptchaTextutil implements TextProducer {
	
	private static int[] numarr={0,1,2,3,4,5,6,7,8,9};
	private static String[] operchar={"+","-","×"};
	@Override
	public String getText() {
		Random rd=new Random();
		int oper_R=rd.nextInt(operchar.length);
		String retStr="";
		String ff=operchar[oper_R];
		if(oper_R==0){
			retStr=numarr[rd.nextInt(numarr.length)]+ff+numarr[rd.nextInt(numarr.length)]+"=";
		}else if(oper_R==1){
			int aa=numarr[rd.nextInt(numarr.length)];
			int bb=aa+rd.nextInt((numarr.length-aa));
			retStr=numarr[bb]+ff+numarr[aa]+"=";
		}else{
			retStr=numarr[rd.nextInt(numarr.length)]+ff+numarr[rd.nextInt(numarr.length)]+"=";
		}
		return retStr;
	}
	
	
	public static int calculateNum(String code){
		if(code==null)
			return -1;
		if(code.length()!=4)
			return -1;
		int a=Integer.parseInt(code.substring(0, 1));
		String b=code.substring(1, 2);;
		int c=Integer.parseInt(code.substring(2, 3));
		if(operchar[0].equals(b)){
			return a+c;
		}else if(operchar[1].equals(b)){
			return a-c;
		}else{
			return a*c;
		}
	}

}