package com.tonybeltramelli.captcha;

import org.json.simple.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Tony Beltramelli www.tonybeltramelli.com
 */

public class CustomSimpleCaptcha
{
	private String _challengePath;
	private String[] _questions;
	
	public CustomSimpleCaptcha(String challengePath)
	{
		_challengePath = challengePath;
	}
	
	public String getChallenge() throws IOException
    {
        String[] questions = _getQuestions();

        int index = (int) Math.round(Math.random() * (questions.length - 1));

        String challenge = questions[index];
        challenge = challenge.substring(0, challenge.indexOf("="));

        JSONObject result = new JSONObject();
        result.put("index", index);
        result.put("challenge", challenge);

		return result.toJSONString();
    }
	
	public String checkAnswer(int index, String providedAnswer) throws IOException
	{
        String[] questions = _getQuestions();

        if(index >= questions.length) return _getStatusMessage(2);

        String expectedAnswer = questions[index];
        expectedAnswer = expectedAnswer.substring(expectedAnswer.indexOf("=") + 1, expectedAnswer.length());

        Boolean isMatched = providedAnswer.toLowerCase().equals(expectedAnswer.toLowerCase());

		return _getStatusMessage(isMatched ? 1 : 0);
	}
	
	private String[] _getQuestions() throws IOException
	{
        if(_questions != null) return _questions;

        Scanner file = new Scanner(new File(_challengePath)).useDelimiter("\n");
        ArrayList<String> questions = new ArrayList<String>();

        Boolean isFirstLine = true;
        while (file.hasNext())
        {
            if(!isFirstLine)
            {
                questions.add(file.next());
            }else{
                file.next();
                isFirstLine = false;
            }
        }
        file.close();

        _questions = questions.toArray(new String[0]);
        return _questions;
	}
	
	private String _getStatusMessage(int type)
	{
		String status = "error";
		
		switch (type)
		{
			case 0:
				status = "fail";
				break;
			case 1:
				status = "success";
				break;
			default:
				status = "error";
		}

        JSONObject result = new JSONObject();
        result.put("status", status);

		return result.toJSONString();
	}
}

@@ -478,28 +478,25 @@
 n%0A    %7B%0A    
-    
+%09
 String%5B%5D que
@@ -526,20 +526,17 @@
 );%0A%0A    
-    
+%09
 int inde
@@ -594,32 +594,26 @@
 gth - 1));%0A%0A
-        
+%09%09
 String chall
@@ -637,24 +637,18 @@
 index%5D;%0A
-        
+%09%09
 challeng
@@ -699,25 +699,21 @@
 (%22=%22));%0A
-%0A        
+%09%09%0A%09%09
 JSONObje
@@ -738,32 +738,26 @@
 ONObject();%0A
-        
+%09%09
 result.put(%22
@@ -772,24 +772,18 @@
 index);%0A
-        
+%09%09
 result.p
@@ -810,16 +810,18 @@
 lenge);%0A
+%09%09
 %0A%09%09retur
@@ -936,24 +936,18 @@
 tion%0A%09%7B%0A
-        
+%09%09
 String%5B%5D
@@ -976,25 +976,21 @@
 ions();%0A
-%0A        
+%09%09%0A%09%09
 if(index
@@ -1040,25 +1040,21 @@
 age(2);%0A
-%0A        
+%09%09%0A%09%09
 String e
@@ -1087,24 +1087,18 @@
 index%5D;%0A
-        
+%09%09
 expected
@@ -1187,32 +1187,26 @@
 length());%0A%0A
-        
+%09%09
 Boolean isMa
@@ -1280,16 +1280,18 @@
 ase());%0A
+%09%09
 %0A%09%09retur
@@ -1391,24 +1391,18 @@
 tion%0A%09%7B%0A
-        
+%09%09
 if(_ques
@@ -1435,25 +1435,21 @@
 stions;%0A
-%0A        
+%09%09%0A%09%09
 Scanner 
@@ -1513,24 +1513,18 @@
 (%22%5Cn%22);%0A
-        
+%09%09
 ArrayLis
@@ -1571,24 +1571,18 @@
 ng%3E();%0A%0A
-        
+%09%09
 Boolean 
@@ -1601,24 +1601,18 @@
 = true;%0A
-        
+%09%09
 while (f
@@ -1630,169 +1630,100 @@
 ())%0A
-        %7B%0A            if(!isFirstLine)%0A            %7B%0A                questions.add(file.next());%0A            %7Delse%7B%0A                file.next();%0A                
+%09%09%7B%0A%09%09%09if(!isFirstLine)%0A%09%09%09%7B%0A%09%09%09%09questions.add(file.next());%0A%09%09%09%7Delse%7B%0A%09%09%09%09file.next();%0A%09%09%09%09
 isFi
@@ -1743,20 +1743,11 @@
 se;%0A
-            
+%09%09%09
 %7D%0A  
@@ -2088,25 +2088,21 @@
 r%22;%0A%09%09%7D%0A
-%0A        
+%09%09%0A%09%09
 JSONObje
@@ -2131,24 +2131,18 @@
 ject();%0A
-        
+%09%09
 result.p

update php src